Documentation ¶
Overview ¶
Package gli is a CLI parsing and mapping library.
type globalCmd struct { Name string Age int } func (g *globalCmd) Run() error { // : } app := gli.NewWith(&globalCmd{}) err := app.Run(os.Args)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotDefined means "an option or a subcommand is not defined in the passed struct". ErrNotDefined = errors.New("not defined") // ErrNotRunnable means "Run method is not defined for the passed struct". ErrNotRunnable = fmt.Errorf("command not runnable") // ErrOptCanNotBeSet is a reflect related error. ErrOptCanNotBeSet = fmt.Errorf("option can not be set") )
Functions ¶
func RegisterTypeDecoder ¶
func RegisterTypeDecoder(typ interface{}, dec TypeDecoder)
See TypeDecoder.
Types ¶
type App ¶
type App struct { // Name is the app name. default: the name of the executable file Name string // Desc is a description of the app // {{Name}} - {{Desc}} Desc string // Usage is a long(multiline) usage text Usage string // Version numbers Version string // Copyright the app author has Copyright string // CliTag is a tag key. default: `cli` CliTag string // HelpTag is a tag key. default: `help` HelpTag string // UsageTag is a tag key. default: `usage` UsageTag string // DefaultTag is a tag key. default: `default` DefaultTag string // DefDescTag is a tag key. default: `defdesc` DefDescTag string // EnvTag is a tag key. default: `env` EnvTag string // RequiredTag is a tag key. default: `required` RequiredTag string // DecTypeTag is a tag key that is used to lookup decoder. default: `type` DecTypeTag string // MyCommandABC => false(default): "mycommandabc" , true: "my-command-abc" HyphenedCommandName bool // MyOptionABC => false(default): "myoptionabc" , true: "my-option-abc" HyphenedOptionName bool // OptionsGrouped(default: true) allows -abc may be treated as -a -b -c. OptionsGrouped bool DoubleHyphen bool // SuppressErrorOutput is an option to suppresses on cli parsing error. SuppressErrorOutput bool Stdout, Stderr *os.File // true(default): bool options have --no-xxx options. // AutoNoBoolOptions also appends --no-xxx descriptions in help doc if . // // Options: // opt1, o1 A bool option // opt2, o2 A bool option (default: true) // --no-opt2 // opt1, o1 A bool option AutoNoBoolOptions bool // contains filtered or unexported fields }
App contains parsing and parsed data.
func New ¶
func New() App
New makes main gli instance to parse and invoke hooks.
App bridges between application logic code and CLI definition (your struct).
Next, call Bind() to set the CLI up.
func (*App) AddExtraCommand ¶
AddExtraCommand adds a sub command.
type Separator ¶ added in v2.3.0
type Separator string
type MyCommand struct { Separator1 gli.Separator Separator2 string `type:"Separator"` }
type SeparatorRune ¶ added in v2.3.0
type SeparatorRune rune
type MyCommand struct { Separator1 gli.SeparatorRune Separator2 string `type:"SeparatorRune"` }
type TypeDecoder ¶
TypeDecoder is used to convert command-line arguments to optional values. It decodes a string to another type of value.
To be decoded ¶
If you declare your cli interface like:
type myCLIRootCommand struct { Opt1 []string `cli:"opt1"` }
And end-user gives a command-line like:
my.exe --opt1 a,b,c
The library gli finds that the opt1 is of type []string, and tries to decode the string "a,b,c" into []string.
gli finds a decoder by calling LookupTypeDecoder. And then gli calls the decoder function to decode "a,b,c" into []string{"a","b","c"}. Finally, gli assigns []string{"a","b","c"} to opt1.
Implemented types (enabled by default) ¶
- (built-in types of golang)
- time.Time (local time; --opt yyyy/mm/dd or --opt yyyy-mm-dd)
- time.Duration
- []string (--opt a,b,c)
- []int (--opt 1,2,3)
- map[string]string (--opt key:value,key:value)
- gli.Range{Min,Max string} (--opt min:max)
User defined types ¶
- Define a decoder function as TypeDecoder
- Call gli.RegisterTypeDecoder(reflect.TypeOf(anyValueOfTheType), decoderFunc)
TypeDecoder ¶
s is a string to decode.
tag is a StructTag of the option.
If firstTime is true, the function should reset v and then decode s into v. Otherwise, it simply decodes s into v. This parameter is useful when appending the contents of a value.
func LookupTypeDecoder ¶
func LookupTypeDecoder(typ interface{}) TypeDecoder