Documentation
¶
Index ¶
- type Option
- type Parser
- func (p *Parser) AddCommand(name string, description string, config *ParserConfig) *Parser
- func (p *Parser) Flag(short, full string, opts *Option) *bool
- func (p *Parser) Float(short, full string, opts *Option) *float64
- func (p *Parser) Floats(short, full string, opts *Option) *[]float64
- func (p *Parser) FormatCompletionScript() string
- func (p *Parser) FormatHelp() string
- func (p *Parser) Int(short, full string, opts *Option) *int
- func (p *Parser) Ints(short, full string, opts *Option) *[]int
- func (p *Parser) Parse(args []string) error
- func (p *Parser) PrintHelp()
- func (p *Parser) String(short, full string, opts *Option) *string
- func (p *Parser) Strings(short, full string, opts *Option) *[]string
- type ParserConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option struct {
Meta string // meta value for help/usage generate
Default string // default argument value if not given
Required bool // require to be set
Positional bool // is positional argument
HideEntry bool // hide usage & help display
Help string // help message
Group string // argument group info, default to be no group
Action func(args []string) error // bind actions when the match is found, 'args' can be nil to be a flag
Choices []interface{} // input argument must be one/some of the choice
Validate func(arg string) error // customize function to check argument validation
Formatter func(arg string) (interface{}, error) // format input arguments by the given method
// contains filtered or unexported fields
}
Option is the only type to config when creating argument
type Parser ¶
type Parser struct {
Invoked bool // whether the parser is invoked
InvokeAction func(bool) // execute after parse
// contains filtered or unexported fields
}
Parser is the top level struct
it's the only interface to interact with user input, parse & bind each `arg` value
func NewParser ¶
func NewParser(name string, description string, config *ParserConfig) *Parser
NewParser create the parser object with optional name & description & ParserConfig
func (*Parser) AddCommand ¶
func (p *Parser) AddCommand(name string, description string, config *ParserConfig) *Parser
AddCommand will add sub command entry parser
Return a new pointer to sub command parser
func (*Parser) Flag ¶
Flag create flag argument, Return a "*bool" point to the parse result
python version is like add_argument("-s", "--full", action="store_true")
Flag Argument can only be used as an OptionalArguments
func (*Parser) Float ¶
Float create float argument, return a *float64 point to the parse result
mostly like *Parser.String(), except the return type
python version is like add_argument("-s", "--full", type=double) or add_argument("s", "full", type=double)
func (*Parser) Floats ¶
Floats create float list argument, return a *[]float64 point to the parse result
mostly like *Parser.Float()
python version is like add_argument("-s", "--full", type=double, nargs="*") or add_argument("s", "full", type=double, nargs="*")
func (*Parser) FormatCompletionScript ¶
FormatCompletionScript generate simple shell complete script, which support bash & zsh for completion
func (*Parser) FormatHelp ¶
FormatHelp only format help message for manual use, for example: decide when to print help message
func (*Parser) Int ¶
Int create int argument, return a *int point to the parse result
mostly like *Parser.String(), except the return type
python version is like add_argument("s", "full", type=int) or add_argument("-s", "--full", type=int)
func (*Parser) Ints ¶
Ints create int list argument, return a *[]int point to the parse result
mostly like *Parser.Int()
python version is like add_argument("s", "full", type=int, nargs="*") or add_argument("-s", "--full", type=int, nargs="*")
func (*Parser) Parse ¶
Parse will parse given args to bind to any registered arguments
args: set nil to use os.Args[1:] by default
func (*Parser) String ¶
String create string argument, return a "*string" point to the parse result
String Argument can be used as Optional or Positional Arguments, default to be Optional, then it's like add_argument("-s", "--full") in python
set Option.Positional = true to use as Positional Argument, then it's like add_argument("s", "full") in python
type ParserConfig ¶
type ParserConfig struct {
Usage string // manual usage display
EpiLog string // message after help
DisableHelp bool // disable help entry register [-h/--help]
ContinueOnHelp bool // set true to: continue program after default help is printed
DisableDefaultShowHelp bool // set false to: default show help when there is no args to parse (default action)
DefaultAction func() // set default action to replace default help action
AddShellCompletion bool // set true to register shell completion entry [--completion]
}
ParserConfig is the only type to config `Parser`, programmers only need to use this type to control `Parser` action
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
any-type-action
command
this is show case for argument action argument action will be executed when user input has a match to the binding argument
|
this is show case for argument action argument action will be executed when user input has a match to the binding argument |
|
basic
command
this is show case for most simple use of argparse with a String Optional Argument created binding to variable 'name' and default help support
|
this is show case for most simple use of argparse with a String Optional Argument created binding to variable 'name' and default help support |
|
basic-bias
command
this is show case for a little bit complex than 'basic' which disabled the default help menu entry '-h' or '--help', instead, it use '-help' and '--help-me' as help menu entry and handle help print manually also, it pass 'os.Args[1:]' manually to 'Parse' method, which is the same as pass 'nil'
|
this is show case for a little bit complex than 'basic' which disabled the default help menu entry '-h' or '--help', instead, it use '-help' and '--help-me' as help menu entry and handle help print manually also, it pass 'os.Args[1:]' manually to 'Parse' method, which is the same as pass 'nil' |
|
customzed-types
command
this is show case for some advanced use use Option.Validate to check if the user input is valid use Option.Formatter to pre-format the user input, before binding to the variable 'host' if there's more free-type argument need, check out another example 'any-type-action'
|
this is show case for some advanced use use Option.Validate to check if the user input is valid use Option.Formatter to pre-format the user input, before binding to the variable 'host' if there's more free-type argument need, check out another example 'any-type-action' |
|
expand-blob
command
this is show case for expand * for positional arguments run the code like "go run main.go ~" or "go run main.go ~/*"
|
this is show case for expand * for positional arguments run the code like "go run main.go ~" or "go run main.go ~/*" |
|
hide-help-entry
command
this is show case for Hide entry you won't see argument 'greet', but you can still use the entry
|
this is show case for Hide entry you won't see argument 'greet', but you can still use the entry |
|
multi-parser
command
this is show case for multi parser in Action it act like sub command, but with less restriction
|
this is show case for multi parser in Action it act like sub command, but with less restriction |
|
npm-install-xxx
command
this is show case like npm install xxx xx with sub-command and its positional argument, you can run the code like "go run main.go install express vue"
|
this is show case like npm install xxx xx with sub-command and its positional argument, you can run the code like "go run main.go install express vue" |
|
parse-action
command
this is show case for default parse action when no user input is given, also has effect on sub command run code like "go run main.go" or "go run main.go test"
|
this is show case for default parse action when no user input is given, also has effect on sub command run code like "go run main.go" or "go run main.go test" |
|
parser-config
command
this show case is for 'ParserConfig', showing how the config affect your parsing progress set ParserConfig.Usage will change your usage line, which can sometime be too complex for user to read set ParserConfig.EpiLog will append a message after help message, which is usually for contact info set ParserConfig.DisableHelp will prevent the default help entry injection, with ParserConfig.DisableHelp, you won't see '-h' or '--help' entry set ParserConfig.DisableDefaultShowHelp will farther prevent default help output when there is no user input set ParserConfig.ContinueOnHelp will keep program going on when the original help action is done the configs about 'Help' is not often used, most programmer may not care about it
|
this show case is for 'ParserConfig', showing how the config affect your parsing progress set ParserConfig.Usage will change your usage line, which can sometime be too complex for user to read set ParserConfig.EpiLog will append a message after help message, which is usually for contact info set ParserConfig.DisableHelp will prevent the default help entry injection, with ParserConfig.DisableHelp, you won't see '-h' or '--help' entry set ParserConfig.DisableDefaultShowHelp will farther prevent default help output when there is no user input set ParserConfig.ContinueOnHelp will keep program going on when the original help action is done the configs about 'Help' is not often used, most programmer may not care about it |
|
shell-completion
command
this is show case for most simple use of argparse with a String Optional Argument created binding to variable 'name' and default help support
|
this is show case for most simple use of argparse with a String Optional Argument created binding to variable 'name' and default help support |
|
sub-command
command
this show case is for sub command sub command is created by AddCommand, which returns a *Parser for programmer to bind arguments sub command has different parse context from main parser (created by NewParser) mainly use sub command to help user understand your program step by step
|
this show case is for sub command sub command is created by AddCommand, which returns a *Parser for programmer to bind arguments sub command has different parse context from main parser (created by NewParser) mainly use sub command to help user understand your program step by step |
|
yt-download
command
this show case is for Argument Groups group arguments in different group in help message but if there is too many argument, it's better to change ParserConfig.Usage to a simpler string Argument Group is for user to better understand your program group by group you should also checkout SubCommand for reference, they are quit like each other
|
this show case is for Argument Groups group arguments in different group in help message but if there is too many argument, it's better to change ParserConfig.Usage to a simpler string Argument Group is for user to better understand your program group by group you should also checkout SubCommand for reference, they are quit like each other |