Documentation ¶
Overview ¶
Package gargle implements a library for command-line parsing.
Index ¶
- func IsAggregate(v Value) bool
- func IsBoolean(v Value) bool
- type Action
- type AggregateValue
- type Arg
- func (a *Arg) BoolVar(v *bool) *Arg
- func (a *Arg) DurationVar(v *time.Duration) *Arg
- func (a *Arg) Float64Var(v *float64) *Arg
- func (a *Arg) Int64Var(v *int64) *Arg
- func (a *Arg) IntVar(v *int) *Arg
- func (a *Arg) IntsVar(v *[]int) *Arg
- func (a *Arg) NegatedBoolVar(v *bool) *Arg
- func (a *Arg) Require() *Arg
- func (a *Arg) RunPre(action Action) *Arg
- func (a *Arg) StringVar(v *string) *Arg
- func (a *Arg) StringsVar(v *[]string) *Arg
- func (a *Arg) Uint64Var(v *uint64) *Arg
- func (a *Arg) UintVar(v *uint) *Arg
- func (a *Arg) Var(v Value) *Arg
- type BooleanValue
- type Command
- func (c *Command) AddArgs(args ...*Arg) *Command
- func (c *Command) AddCommands(commands ...*Command)
- func (c *Command) AddFlags(flags ...*Flag) *Command
- func (c *Command) Args() []*Arg
- func (c *Command) Commands() []*Command
- func (c *Command) Flags() []*Flag
- func (c *Command) FullFlags() []*Flag
- func (c *Command) FullName() string
- func (c *Command) NewArg(name, help string) *Arg
- func (c *Command) NewCommand(name, help string) *Command
- func (c *Command) NewFlag(name, help string) *Flag
- func (c *Command) Parent() *Command
- func (c *Command) Parse(args []string) error
- func (c *Command) Run(action Action) *Command
- func (c *Command) RunPre(action Action) *Command
- type Flag
- func (f *Flag) BoolVar(v *bool) *Flag
- func (f *Flag) DurationVar(v *time.Duration) *Flag
- func (f *Flag) Float64Var(v *float64) *Flag
- func (f *Flag) Hide() *Flag
- func (f *Flag) Int64Var(v *int64) *Flag
- func (f *Flag) IntVar(v *int) *Flag
- func (f *Flag) IntsVar(v *[]int) *Flag
- func (f *Flag) NegatedBoolVar(v *bool) *Flag
- func (f *Flag) Require() *Flag
- func (f *Flag) RunPre(action Action) *Flag
- func (f *Flag) StringVar(v *string) *Flag
- func (f *Flag) StringsVar(v *[]string) *Flag
- func (f *Flag) Uint64Var(v *uint64) *Flag
- func (f *Flag) UintVar(v *uint) *Flag
- func (f *Flag) ValueName(s string) *Flag
- func (f *Flag) Var(v Value) *Flag
- type UsageWriter
- type Value
- func BoolVar(v *bool) Value
- func DurationVar(v *time.Duration) Value
- func Float64Var(v *float64) Value
- func Int64Var(v *int64) Value
- func IntVar(v *int) Value
- func IntsVar(v *[]int) Value
- func NegatedBoolVar(v *bool) Value
- func StringVar(v *string) Value
- func StringsVar(v *[]string) Value
- func Uint64Var(v *uint64) Value
- func UintVar(v *uint) Value
- func WithDefault(v Value, s ...string) Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAggregate ¶
IsAggregate returns whether a value can be set multiple times.
Types ¶
type Action ¶
Action is a function which is invoked during or after parsing. The passed context is actively parsed command, i.e. the last encountered during parsing.
func DefaultUsage ¶
func DefaultUsage() Action
DefaultUsage returns the default usage writer as an action. This is usually attached to help flags and commands as a PreAction.
Example ¶
var b bool var i int var s []string root := &Command{Name: "root", Help: "A root command which does something."} root.AddCommands( NewHelpCommand(nil), &Command{Name: "subcommand1", Help: "The first subcommand does some things too."}, &Command{Name: "sub2", Help: "The second command has long explanatory text. Perhaps there is a complex edge case which is very important for a user to know."}, ) root.AddFlags( NewHelpFlag(nil), &Flag{Short: 'v', Help: "Show version information"}, &Flag{Short: 'a', Help: "Short flag with no long form", Value: IntVar(&i)}, &Flag{Name: "long-only", Help: "Long flag with no short form", Value: IntVar(&i)}, &Flag{Name: "bool", Short: 'b', Help: "Boolean flag", Value: BoolVar(&b)}, &Flag{Name: "hidden", Help: "A hidden flag", Hidden: true}, &Flag{ Name: "slice", Short: 's', Placeholder: "STR", Help: "Aggregate value with custom placeholder", Value: StringsVar(&s), }, ) root.Parse([]string{"help"})
Output: Usage: root [<flags>] <command> A root command which does something. Commands: help Show usage sub2 The second command has long explanatory text. Perhaps there is a complex edge case which is very important for a user to know. subcommand1 The first subcommand does some things too. Options: -a VALUE Short flag with no long form -b, --bool Boolean flag -h, --help Show usage --long-only VALUE Long flag with no short form -s, --slice STR... Aggregate value with custom placeholder -v Show version information
type AggregateValue ¶
type AggregateValue interface {
IsAggregate() bool
}
AggregateValue is an optional interface which may be implemented by aggregate types, such as slices and maps. This affects how values are displayed in help and allows positional arguments to consume multiple values.
type Arg ¶
type Arg struct { // An optional name to display in help and errors. Name string // Text describing the argument. It may be a single line or an arbitrarily // long description. Usage writers may assume the first line can serve // independently as a short-form description. Help string // Required sets the argument to generate an error when absent. Required bool // PreAction is invoked after parsing, but before values are set. All pre-actions // are executed unconditionally in the order encountered during parsing. PreAction Action // Underlying value for the argument, set during parsing. Value Value }
Arg represents a positional argument attached to a command.
func (*Arg) Float64Var ¶
func (*Arg) NegatedBoolVar ¶
func (*Arg) StringsVar ¶
type BooleanValue ¶
type BooleanValue interface {
IsBoolean() bool
}
BooleanValue is an optional interface which may be implemented by bool types.
Flags backed by boolean values are parsed differently from others. Bool flags are set to "true" if provided without an explicit value.
type Command ¶
type Command struct { // Name of the command. Name string // Text describing the command. It may be a single line or an arbitrarily // long description. Usage writers may assume the first line can serve // independently as a short-form description. Help string // Hidden sets whether the command should be omitted from usage text. Hidden bool // PreAction is invoked after parsing, but before values are set. All pre-actions // are executed unconditionally in the order encountered during parsing. PreAction Action // Action invoked after parsing and argument validation. Only the active // context, i.e. the last command parsed, is invoked. Action Action // Client-defined labels for grouping and processing commands. Labels map[string]string // contains filtered or unexported fields }
Command is a hierarchical structured argument. It can serve as an application entry point, a command group, or both. For exmple, in the command line "go test .", "go" is a root command and "test" is a subcommand of "go".
func NewHelpCommand ¶
NewHelpCommand creates a standard help command, which prints help for a given subcommand. If no arguments are passed, it prints its parent's help. This should be added to each command group.
func (*Command) AddCommands ¶
AddCommands adds any number of child commands. It is an error to add the same child command to multiple parents, or to add a command to itself.
func (*Command) AddFlags ¶
AddFlags creates a new flag under a command. Flags are inherited by subcommands unless overridden by a flag with the same name.
func (*Command) Args ¶
Args returns a command's positional arguments, not including those of its parents.
func (*Command) FullFlags ¶
FullFlags returns a command's flags along with those of its parents. Flags are ordered parent to child.
func (*Command) NewCommand ¶
NewCommand creates a new subcommand.
func (*Command) NewFlag ¶
NewFlag creates a new flag within a command. The name must take the form "[long][,short]", where short is a single character.
type Flag ¶
type Flag struct { // Name is an optional unprefixed long form of the flag. // For example, "help" would match the argument "--help". Name string // Text describing the command. It may be a single line or an arbitrarily // long description. Usage writers may assume the first line can serve // independently as a short-form description. Help string // Placeholder is an optional override for the name of a flag's value. Placeholder string // Short is an optional single-character short form for the flag. // For example, 'h' would match the argument "-h". Short rune // Hidden sets whether the flag should be omitted from usage text. Hidden bool // Required sets the flag to generate an error when absent. Required bool // PreAction is invoked after parsing, but before values are set. All pre-actions // are executed unconditionally in the order encountered during parsing. PreAction Action // Underlying value for the flag, set during parsing. Value Value }
Flag is a single named/value argument pair
func NewFlag ¶
NewFlag creates a new flag given a composite name and help string. The name must take the form "[long][,short]", where short is a single character.
Examples:
"foo,f": --foo, -f "long-only": --long (long-form only) ",s": -s (short-form only)
func NewHelpFlag ¶
NewHelpFlag creates a standard help flag which invokes the an action when parsed. This flag should be attached to the root command.
func (*Flag) Float64Var ¶
func (*Flag) NegatedBoolVar ¶
func (*Flag) StringsVar ¶
type UsageWriter ¶
type UsageWriter struct { // Indent is an indentation prefix for subsections. Indent string // Divider is a string to print between columns. Divider string // MaxFirstColumn is the maximum width of the left column in split sections. MaxFirstColumn int // MaxLineWidth overrides the maximum width of each line of text, defaults // to terminal width in TTY or 80 columns otherwise. MaxLineWidth int // Writer overrides the default writer, default os.Stdout. Writer io.Writer }
UsageWriter is a configurable usage formatter which can be used as a safe default for most applications.
func (*UsageWriter) Format ¶
func (u *UsageWriter) Format(command *Command) error
Format writes a given command's usage using the writer's configuration.
type Value ¶
Value is an interface implemented by all value types.
func DurationVar ¶
DurationVar wraps a time duration, including units.
func Float64Var ¶
Float64Var wraps a double-precision floating point.
func NegatedBoolVar ¶
NegatedBoolVar wraps a single boolean value. It sets v to the opposite of a parsed string. It's most useful when paired with a positive counterpart.
Example ¶
var value bool NegatedBoolVar(&value).Set("false") fmt.Println(value)
Output: true
func WithDefault ¶
WithDefault wraps a value with string default value(s) which will be applied after parsing if (and only if) a value is left unset.