Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.7 KB

autocompletion.md

File metadata and controls

51 lines (35 loc) · 1.7 KB

Autocompletion

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.

command.SimpleCompleter

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")
)

command.SimpleDistincCompleter (for ListArg/ListFlag)

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")
)

commander.FileCompleter

This struct completes file/directory names. See the go doc for more info.

Writing Your Own Completer (commander.CompleterFromFunc)

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
  }))
)