Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FixSubcommands ¶
func FixSubcommands(args []string, isCommand IsCommandFn) []string
FixSubcommands rearanges an `args []string` command line to enable flag arguments and positional arguments to be mixed, which makes some invocations look more natural.
Taking cipd as an example, compare:
- Default: cipd set-ref -ref=abc -version=def package/name
- Improved: cipd set-ref package/name -ref=abc -version=def
Much better.
Pass a function isCommand which should return true if the tokens in `toks` indicate a subcommand. As long as this function returns true, FixSubcommands will keep these tokens at the beginning of the returned slice. As soon as it returns false, the remaining tokens will be sorted into flags and positional arguments, and the positional arguments will be moved to the end of the slice. Passing `nil` means that no tokens will be treated as subcommands (do this if you're not using a subcommand-parsing library).
Types ¶
type IsCommandFn ¶
IsCommandFn will be invoked with successive groups of tokens in order to determine what portion of the command line is a 'subcommand'. E.g. for the command line:
prog show cool subcommand option -flag 1
This would be invoked with:
- ['show']
- ['show', 'cool']
- ['show', 'cool', 'subcommand']
- ['show', 'cool', 'subcommand', 'option']
And the function would return `true` for the first three and `false` for the fourth. This way FixSubcommands could know that 'option' is a positional argument and not a subcommand.
func MaruelSubcommandsFn ¶
func MaruelSubcommandsFn(a subcommands.Application) IsCommandFn
MaruelSubcommandsFn returns an IsCommandFn which is compatible with "maruel/subcommands".Application objects.