-
Notifications
You must be signed in to change notification settings - Fork 123
Subcommand and flag autocompletion support (bash + zsh) #58
Conversation
3ef040a
to
613d119
Compare
autocomplete.go
Outdated
"github.com/posener/complete/cmd/install" | ||
) | ||
|
||
// autocompleteInstaller is an interface to be implemented to peform the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
peform -> perform
cli.go
Outdated
// If both install and uninstall flags are specified, then error | ||
if c.isAutocompleteInstall && c.isAutocompleteUninstall { | ||
// TODO: Write error message | ||
return 1, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should get the error in before merging.
} | ||
|
||
// Check if it implements ComandAutocomplete. If so, setup the autocomplete | ||
if c, ok := impl.(CommandAutocomplete); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth having these as two interfaces so they can be implemented separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its unnecessary since you can easily return nil
for either so I think its cleaner to have one since the "null" implementation is so easy.
cli.go
Outdated
c.autocomplete = complete.New(c.Name, cmd) | ||
} | ||
|
||
func (c *CLI) initAutocompleteSub(prefix string) complete.Command { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth a comment that this is a recursive call to walk all the subcommands
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted to say so.
284d802
to
c0e03dd
Compare
This adds support for bash and zsh autocompletion of subcommands, flags, and arguments.
The value of autocompletion can be specified via Go callbacks. No need to write any bash.
Under the covers, this PR uses the fantastic and full-featured complete library. However, knowledge of this library is not required to use the cli lib or to get basic subcommand autocompletion. For more complex autocompletion, integration is required but is built right-in to the CLI lib.
Enabling Autocompletion
Autocompletion is opt-in. Users of the CLI lib only need to set
Autocomplete
totrue
:🎉 With that, subcommand autocompletion automatically happens.
Autocompletion Install/Uninstall
After autocompletion is enabled, the global flags
-autocomplete-install
and-autocomplete-uninstall
are added. When these are present they install or uninstall the autocompletion handler for your shell, respectively. After installation, the shell must be restarted for autocompletion to take effect.Subcommand Autocompletion
Subcommand autocompletion comes for free. No additional code required.
Terraform example, assume a tab is typed at the end of each prompt line:
Argument and Flag Autocompletion
Commands may implement the optional
CommandAutocomplete
interface. This interface allows a command to return autocompletion code for arguments and flags. This allows complex integrations. For example, Nomad uses UUIDs for referencing almost anything and argument autocompletion would look like the following:Or continuing Terraform examples, imagine the
-target
flag: