Documentation
¶
Index ¶
- type BreakAfterHelp
- type BreakAfterShellScript
- 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 BreakAfterHelp ¶ added in v1.5.0
type BreakAfterHelp struct { }
BreakAfterHelp will be thrown after help showed
func (BreakAfterHelp) Error ¶ added in v1.5.0
func (b BreakAfterHelp) Error() string
type BreakAfterShellScript ¶ added in v1.5.0
type BreakAfterShellScript struct { }
BreakAfterShellScript will be thrown after shell script showed
func (BreakAfterShellScript) Error ¶ added in v1.5.0
func (b BreakAfterShellScript) Error() string
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 NoHint bool // disable hint info for the argument (usable when ParserConfig.WithHint = true) HintInfo string // specified hint info suffixed after help message, when ParserConfig.WithHint = true Group string // argument group info, default to be no group Inheritable bool // sub parsers after this argument can inherit it 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. Don't use it directly, use NewParser to create one
it's the only struct 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 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 ¶ added in v0.4.0
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] WithHint bool // argument help message with argument default value hint MaxHeaderLength int // max argument header length in help menu, help info will start at new line if argument meta info is too long }
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
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 |
argument-groups
this is show case for creating argument groups
|
this is show case for creating argument groups |
basic
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
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' |
batch-create-arguments
this is show case for creating arguments by batch
|
this is show case for creating arguments by batch |
change-help
example on how to change help args' position in help message note that the order of your argument is the same as the order when you add them, then you can decide the order of your arguments' display
|
example on how to change help args' position in help message note that the order of your argument is the same as the order when you add them, then you can decide the order of your arguments' display |
customzed-types
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
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
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 |
long-args
example on how to deal with very long args (if it's very necessary) MaxHeaderLength is recommended to be around 20 ~ 30
|
example on how to deal with very long args (if it's very necessary) MaxHeaderLength is recommended to be around 20 ~ 30 |
multi-parser
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
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" |
only-shorts-fix
show case for testing multi arguments with only short tags
|
show case for testing multi arguments with only short tags |
parse-action
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
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
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 show case shows how to handle after help or shell script showed, but it's ok not to handle them separately, because their error msg is empty, only one more blank line will be added in the tail output
|
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 show case shows how to handle after help or shell script showed, but it's ok not to handle them separately, because their error msg is empty, only one more blank line will be added in the tail output |
sub-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
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 |