fixflagpos

package
v0.0.0-...-1643519 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2024 License: Apache-2.0 Imports: 2 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fix

func Fix(args []string) []string

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

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

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

func MaruelSubcommandsFn(a subcommands.Application) IsCommandFn

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL