One of the most useful features of this package is the built-in autocompletion capabilities. Simply by providing a Completer
option to your arguments/flags, you can have dynamic autocompletion to your CLIs. See below for specific examples.
The simplest completer is one that autocompletes a set of hardcoded options:
var (
myArg = commander.Arg[string]("CHARACTER", "Choose a character", command.SimpleCompleter("Mario", "Kirby", "Link")
)
This completer is identical to the SimpleCompleter
except it won't include items that are included earlier in the list.
var (
// `cli Mario [tab]` will only suggest "Kirby" and "Link".
myArgs = commander.ListArg[string]("CHARACTERS", "Choose two characters", 2, 0 command.SimpleCompleter("Mario", "Kirby", "Link")
// Also works for list flags.
myFlags = command.ListFlags[string]("TEAMMATE", "Choose two characters", 2, 0 command.SimpleCompleter("Luigi", "Metaknight", "Zelda")
)
This struct completes file/directory names. See the go doc for more info.
This completer runs the provided function and uses the Completion/error
returned from that function as the completion object.
This function is most useful for writing your own completion logic:
var (
myArg = commander.Arg[string]("ARG", "Description", commander.CompleterFromFunc(func(s string, d *command.Data) (*command.Completion, error) {
var sl []string
// Run whatever logic you want
return &command.Completion{
Suggestions: sl,
}, nil
}))
)