luci: Index | Files

package fixflagpos

import ""


Package Files


func Fix Uses

func Fix(args []string) []string

Fix is a shorthand for `FixSubcommands(args, nil)`.

func FixSubcommands Uses

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

type IsCommandFn Uses

type IsCommandFn func(toks []string) bool

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 Uses

func MaruelSubcommandsFn(a subcommands.Application) IsCommandFn

MaruelSubcommandsFn returns an IsCommandFn which is compatible with "maruel/subcommands".Application objects.

Package fixflagpos imports 2 packages (graph) and is imported by 4 packages. Updated 2018-08-19. Refresh now. Tools for package owners.