Commandant
Commandant copied to clipboard
💥 Command Aliases
Implements #153.
Opening this PR as a draft for discussion. I've got a simple/dumb implementation of aliases working in mas and you can see the behavior below. The install
command was given an alias of i
, but the help
output lists the install
command twice.
Available commands:
account Prints the primary account Apple ID
help Display general or command-specific help
home Opens MAS Preview app page in a browser
info Display app information from the Mac App Store
install Install from the Mac App Store
install Install from the Mac App Store
...
↪ sudo mas uninstall 803453959
Password:
==> App moved to trash: /private/var/root/.Trash/Slack 20-31-11-388.app
↪ mas i 803453959
==> Downloading Slack
#####################################################------- 88.0% Installing
This was achieved by:
- Adding
CommandProtocol.aliases
- Registering commands again, once for each alias name
Modifying the CommandProtocol
is a breaking change and each user of this library would need to add the alias
property to every command, even if the command has no aliases. The 2nd item causes the command duplication but could be improved by extending the idea of an alias into the registry and auto-generating the help description with something like:
Available commands:
install Install from the Mac App Store
i Alias for install
Perhaps a better approach is to register aliases differently instead of modifying the model. Going to think some more about this but welcome any feedback or suggestions.
Rebased onto master so compare view is much cleaner now.
and other solution would be a Wrapper for a command -
AliasCommandWrapper
/ Alias
:
public struct Alias<ClientError: Error>: CommandProtocol {
// [...]
fileprivate init<C: CommandProtocol>(_ command: C, alias: String) where C.ClientError == ClientError {
verb = alias
function = "alias for '${command.verb}': {$command.function}"
run = command.run
usage = command.usage
}
}
usage:
registry.register(Alias(ListCommand(), alias: "L"))
drawback: the alias strings are not defined at the command class
Modifying the CommandProtocol is a breaking change and each user of this library would need to add the alias property to every command, even if the command has no aliases.
is it not possible to make the alias as optional, so that it not need to add and it is not a breaking change?