Documentation
¶
Overview ¶
Package gflag provide command line options and arguments binding, parse, management.
Index ¶
- Constants
- Variables
- func UnquoteUsage(flag *Flag) (name string, usage string)
- type Argument
- type Arguments
- type Booleans
- type CliArg
- func (a *CliArg) Array() (ss []string)
- func (a *CliArg) GetValue() any
- func (a *CliArg) HasValue() bool
- func (a *CliArg) HelpName() string
- func (a *CliArg) Index() int
- func (a *CliArg) Init() *CliArg
- func (a *CliArg) SetArrayed() *CliArg
- func (a *CliArg) SetValue(val any) error
- func (a *CliArg) WithAfterFn(fn func(a *CliArg) error) *CliArg
- func (a *CliArg) WithArrayed() *CliArg
- func (a *CliArg) WithDefault(val any) *CliArg
- func (a *CliArg) WithFn(fn func(arg *CliArg)) *CliArg
- func (a *CliArg) WithValidator(fn func(any) (any, error)) *CliArg
- func (a *CliArg) WithValue(val any) *CliArg
- type CliArgs
- func (ags *CliArgs) AddArg(name, desc string, requiredAndArrayed ...bool) *CliArg
- func (ags *CliArgs) AddArgByRule(name, rule string) *CliArg
- func (ags *CliArgs) AddArgument(arg *CliArg) *CliArg
- func (ags *CliArgs) Arg(name string) *CliArg
- func (ags *CliArgs) ArgByIndex(i int) *CliArg
- func (ags *CliArgs) Args() []*CliArg
- func (ags *CliArgs) BindArg(arg *CliArg) *CliArg
- func (ags *CliArgs) BuildArgsHelp() string
- func (ags *CliArgs) ExtraArgs() []string
- func (ags *CliArgs) HasArg(name string) bool
- func (ags *CliArgs) HasArgs() bool
- func (ags *CliArgs) HasArguments() bool
- func (ags *CliArgs) ParseArgs(args []string) (err error)
- func (ags *CliArgs) SetName(name string)
- func (ags *CliArgs) SetValidateNum(validateNum bool)
- func (ags *CliArgs) String() string
- type CliOpt
- func (m *CliOpt) DValue() *structs.Value
- func (m *CliOpt) Flag() *Flag
- func (m *CliOpt) HelpName() string
- func (m *CliOpt) Shorts2String(sep ...string) string
- func (m *CliOpt) ShortsString(sep ...string) string
- func (m *CliOpt) Validate(val string) error
- func (m *CliOpt) WithOptFns(fns ...CliOptFn) *CliOpt
- type CliOptFn
- type CliOpts
- func (co *CliOpts) Bool(name, shorts string, defVal bool, desc string) *bool
- func (co *CliOpts) BoolOpt(ptr *bool, name, shorts string, defVal bool, desc string)
- func (co *CliOpts) BoolOpt2(p *bool, nameAndShorts, desc string, setFns ...CliOptFn)
- func (co *CliOpts) BoolVar(ptr *bool, opt *CliOpt)
- func (co *CliOpts) FSet() *FlagSet
- func (co *CliOpts) Float64Opt(p *float64, name, shorts string, defVal float64, desc string)
- func (co *CliOpts) Float64Var(ptr *float64, opt *CliOpt)
- func (co *CliOpts) FuncOpt(nameAndShorts, desc string, fn FuncOptFn, setFns ...CliOptFn)
- func (co *CliOpts) HasOption(name string) bool
- func (co *CliOpts) InitFlagSet()
- func (co *CliOpts) Int(name, shorts string, defVal int, desc string) *int
- func (co *CliOpts) Int64(name, shorts string, defVal int64, desc string) *int64
- func (co *CliOpts) Int64Opt(ptr *int64, name, shorts string, defValue int64, desc string)
- func (co *CliOpts) Int64Var(ptr *int64, opt *CliOpt)
- func (co *CliOpts) IntOpt(p *int, name, shorts string, defVal int, desc string)
- func (co *CliOpts) IntOpt2(p *int, nameAndShorts, desc string, setFns ...CliOptFn)
- func (co *CliOpts) IntVar(p *int, opt *CliOpt)
- func (co *CliOpts) IsOption(name string) bool
- func (co *CliOpts) IsShortName(short string) bool
- func (co *CliOpts) IsShortOpt(short string) bool
- func (co *CliOpts) IterAll(fn func(f *flag.Flag, opt *CliOpt))
- func (co *CliOpts) LookupFlag(name string) *Flag
- func (co *CliOpts) Opt(name string) *CliOpt
- func (co *CliOpts) Opts() map[string]*CliOpt
- func (co *CliOpts) ParseOpts(args []string) (err error)
- func (co *CliOpts) SetFlagSet(fSet *FlagSet)
- func (co *CliOpts) SetName(name string)
- func (co *CliOpts) ShortNames(name string) (ss []string)
- func (co *CliOpts) Str(name, shorts string, defVal, desc string) *string
- func (co *CliOpts) StrOpt(p *string, name, shorts string, defValAndDesc ...string)
- func (co *CliOpts) StrOpt2(p *string, nameAndShorts, desc string, setFns ...CliOptFn)
- func (co *CliOpts) StrVar(p *string, opt *CliOpt)
- func (co *CliOpts) Uint(name, shorts string, defVal uint, desc string) *uint
- func (co *CliOpts) Uint64(name, shorts string, defVal uint64, desc string) *uint64
- func (co *CliOpts) Uint64Opt(ptr *uint64, name, shorts string, defVal uint64, desc string)
- func (co *CliOpts) Uint64Var(ptr *uint64, opt *CliOpt)
- func (co *CliOpts) UintOpt(ptr *uint, name, shorts string, defValue uint, desc string)
- func (co *CliOpts) UintVar(ptr *uint, opt *CliOpt)
- func (co *CliOpts) Var(ptr flag.Value, opt *CliOpt)
- func (co *CliOpts) VarOpt(v flag.Value, name, shorts, desc string)
- func (co *CliOpts) VarOpt2(v flag.Value, nameAndShorts, desc string, setFns ...CliOptFn)
- type ConfString
- type Config
- type ConfigFunc
- type EnumString
- type Flag
- type FlagSet
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) Bool(name string, value bool, usage string) *bool
- func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) *Flag
- func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) *Flag
- func (f *FlagSet) Float64(name string, value float64, usage string) *float64
- func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) *Flag
- func (f *FlagSet) Func(name, usage string, fn func(string) error) *Flag
- func (f *FlagSet) Int(name string, value int, usage string) *int
- func (f *FlagSet) Int64(name string, value int64, usage string) *int64
- func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) *Flag
- func (f *FlagSet) IntVar(p *int, name string, value int, usage string) *Flag
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) NFlag() int
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) String(name, value, usage string) *string
- func (f *FlagSet) StringVar(p *string, name, value, usage string) *Flag
- func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...) *Flag
- func (f *FlagSet) Uint(name string, value uint, usage string) *uint
- func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64
- func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) *Flag
- func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) *Flag
- func (f *FlagSet) Var(value Value, name string, usage string) *Flag
- func (f *FlagSet) VisitAll(fn func(*Flag))
- type Flags
- type FuncOptFn
- type HandleFunc
- type Ints
- type IntsString
- type KVString
- type OptCategory
- type Parser
- func (p *Parser) BuildHelp() string
- func (p *Parser) BuildOptsHelp() string
- func (p *Parser) FSetArgs() []string
- func (p *Parser) FlagNames() map[string]int
- func (p *Parser) FromStruct(ptr any, ruleType ...uint8) (err error)
- func (p *Parser) Init(name string)
- func (p *Parser) Len() int
- func (p *Parser) MustFromStruct(ptr any, ruleType ...uint8)
- func (p *Parser) Name() string
- func (p *Parser) Parse(args []string) (err error)
- func (p *Parser) ParserCfg() *Config
- func (p *Parser) PrintHelpPanel()
- func (p *Parser) RawArg(i int) string
- func (p *Parser) RawArgs() []string
- func (p *Parser) Required(names ...string)
- func (p *Parser) Run(args []string)
- func (p *Parser) SetConfig(opt *Config)
- func (p *Parser) SetHandle(fn HandleFunc) *Flags
- func (p *Parser) SetHelpRender(fn func())
- func (p *Parser) SetName(name string)
- func (p *Parser) SetOutput(out io.Writer)
- func (p *Parser) SetRuleType(rt uint8) *Parser
- func (p *Parser) String() string
- func (p *Parser) UseSimpleRule() *Parser
- func (p *Parser) WithConfigFn(fns ...ConfigFunc) *Parser
- type String
- type Strings
- type ValidValue
- type Value
Constants ¶
const ( // AlignLeft Align right, padding left AlignLeft = strutil.PosRight // AlignRight Align left, padding right AlignRight = strutil.PosLeft )
const ( // TagRuleNamed struct tag use named k-v rule. // // eg: // `flag:"name=int0;shorts=i;required=true;desc=int option message"` // // name contains short name // `flag:"name=int0,i;required=true;desc=int option message"` TagRuleNamed uint8 = iota // TagRuleSimple struct tag use simple rule. // format: "desc;required;default;shorts" // // eg: `flag:"int option message;required;;i"` TagRuleSimple // TagRuleField struct tag use field name as flag setting name. TODO // // eg: `flag:"name,n" desc:"int option message" required:"true" default:"0"` TagRuleField )
Variables ¶
var DefaultOptWidth = 20
DefaultOptWidth for render help
var FlagTagName = "flag"
FlagTagName default tag name on struct
Functions ¶
func UnquoteUsage ¶ added in v3.2.4
UnquoteUsage extracts a back-quoted name from the usage string for a flag and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the flag's value, or the empty string if the flag is boolean.
Note: from go flag.UnquoteUsage()
Types ¶
type CliArg ¶
type CliArg struct { *structs.Value // Name argument name. it's required Name string // Desc argument description message Desc string // ShowName is a name for display help. default is equals to Name. ShowName string // Required arg is required Required bool // Arrayed if is array, can allow to accept multi values, and must in last. Arrayed bool // Handler custom argument value handler on call GetValue() Handler func(val any) any // Validator you can add a validator, will call it on binding argument value Validator func(val any) (any, error) // AfterFn after bind value listen func AfterFn func(a *CliArg) error // contains filtered or unexported fields }
CliArg a command argument definition
func NewArgument ¶
NewArgument quick create a new command argument
func (*CliArg) WithAfterFn ¶ added in v3.2.1
WithAfterFn a func to the argument
func (*CliArg) WithArrayed ¶ added in v3.2.2
WithArrayed for the argument
func (*CliArg) WithDefault ¶ added in v3.2.0
WithDefault value to the argument
func (*CliArg) WithValidator ¶
WithValidator set a value validator of the argument
type CliArgs ¶
type CliArgs struct {
// contains filtered or unexported fields
}
CliArgs definition
func (*CliArgs) AddArg ¶
AddArg binding a named argument for the command.
Notice:
- Required argument cannot be defined after optional argument
- Only one array parameter is allowed
- The (array) argument of multiple values can only be defined at the end
Usage:
cmd.AddArg("name", "description") cmd.AddArg("name", "description", true) // required cmd.AddArg("names", "description", true, true) // required and is arrayed
func (*CliArgs) AddArgByRule ¶
AddArgByRule add an arg by simple string rule.
Format: desc;required;default
func (*CliArgs) AddArgument ¶
AddArgument binding a named argument for the command.
Notice:
- Required argument cannot be defined after optional argument
- Only one array parameter is allowed
- The (array) argument of multiple values can only be defined at the end
func (*CliArgs) Arg ¶
Arg get arg by defined name.
Usage:
intVal := ags.Arg("name").Int() strVal := ags.Arg("name").String() arrVal := ags.Arg("names").Array()
func (*CliArgs) ArgByIndex ¶
ArgByIndex get named arg by index
func (*CliArgs) SetValidateNum ¶
SetValidateNum check
type CliOpt ¶
type CliOpt struct {
// Name of flag and description
Name, Desc string
// default value for the flag option
DefVal any
// Shorts shorthand/alias names. eg: ["o", "a"]
Shorts []string
// EnvVar allow set flag value from ENV var
EnvVar string
// Hidden the option on help
Hidden bool
// Required the option is required
Required bool
// Validator support validate the option flag value
Validator func(val string) error
// TODO interactive question for collect value
Question string
// contains filtered or unexported fields
}
CliOpt define for a flag option
func (*CliOpt) Shorts2String ¶
Shorts2String join shorts to a string
func (*CliOpt) ShortsString ¶ added in v3.2.0
ShortsString join shorts to a string
func (*CliOpt) WithOptFns ¶ added in v3.2.0
WithOptFns set for current option
type CliOptFn ¶ added in v3.2.0
type CliOptFn func(opt *CliOpt)
CliOptFn opt config func type
func WithDefault ¶ added in v3.2.0
WithDefault value setting for option
func WithShortcut ¶ added in v3.2.0
WithShortcut setting for option, multi shortcut use comma separated
func WithShorts ¶ added in v3.2.0
WithShorts setting for option
func WithValidator ¶ added in v3.2.0
WithValidator setting for option
type CliOpts ¶
type CliOpts struct {
// contains filtered or unexported fields
}
CliOpts cli options management
func (*CliOpts) BoolOpt2 ¶ added in v3.2.0
BoolOpt2 binding a bool option, and allow with CliOptFn for config option.
func (*CliOpts) Float64Opt ¶
Float64Opt binding a float64 option
func (*CliOpts) Float64Var ¶
Float64Var binding an float64 option flag
func (*CliOpts) FuncOpt ¶ added in v3.2.4
FuncOpt binding a func option flag
Usage:
cmd.FuncOpt("name", "description ...", func(s string) error { // do something ... return nil })
func (*CliOpts) InitFlagSet ¶
func (co *CliOpts) InitFlagSet()
InitFlagSet create and init flag.FlagSet
func (*CliOpts) IsShortName ¶
IsShortName check it is a shortcut name
func (*CliOpts) IsShortOpt ¶
IsShortOpt alias of the IsShortcut()
func (*CliOpts) LookupFlag ¶
LookupFlag get Flag by name
func (*CliOpts) SetFlagSet ¶ added in v3.2.4
SetFlagSet set the raw *FlagSet
func (*CliOpts) ShortNames ¶
ShortNames get all short-names of the option
func (*CliOpts) StrOpt ¶
StrOpt binding a string option.
If defValAndDesc only one elem, will as desc message.
func (*CliOpts) StrOpt2 ¶ added in v3.2.0
StrOpt2 binding a string option, and allow with CliOptFn for config option.
type ConfString ¶ added in v3.2.1
type ConfString = cflag.ConfString
ConfString The config-string flag, INI format, like nginx-config.
type Config ¶
type Config struct { // WithoutType don't display flag data type on print help WithoutType bool // DescNewline flag desc at new line on print help DescNewline bool // Alignment flag name align left or right. default is: left Alignment strutil.PosFlag // TagName on struct. default is FlagTagName TagName string // TagRuleType for struct tag value. default is TagRuleNamed TagRuleType uint8 // DisableArg disable binding arguments. DisableArg bool // IndentLongOpt indent long option name on print help IndentLongOpt bool // EnhanceShort enhance short option parse. TODO // // 0 - none // 1 - multi short bool options. eg: `-aux` = `-a -u -x` // 2 - allow name with value as one node. eg: `-Ostdout` = `-O stdout` EnhanceShort uint8 }
Config for render help information
func (*Config) GetTagName ¶ added in v3.2.2
GetTagName get tag name, default is FlagTagName
type ConfigFunc ¶ added in v3.2.2
type ConfigFunc func(cfg *Config)
ConfigFunc config func for parser
func WithIndentLongOpt ¶ added in v3.2.2
func WithIndentLongOpt(yes bool) ConfigFunc
WithIndentLongOpt on print help
type EnumString ¶
type EnumString = cflag.EnumString
EnumString The string flag list, implemented flag.Value interface
type FlagSet ¶ added in v3.2.4
type FlagSet struct { Usage func() // contains filtered or unexported fields }
FlagSet custom implement flag set, like flag.FlagSet. But support short names for options.
func NewFlagSet ¶ added in v3.2.4
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet
NewFlagSet create a new FlagSet
func (*FlagSet) Arg ¶ added in v3.2.4
Arg returns the i'th argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist.
func (*FlagSet) Bool ¶ added in v3.2.4
Bool defines a bool flag with specified name, default value, and usage string. from the go flag.Bool()
func (*FlagSet) BoolVar ¶ added in v3.2.4
BoolVar defines a bool flag with specified name, default value, and usage string. from the go flag.BoolVar()
func (*FlagSet) Duration ¶ added in v3.2.4
Duration defines a time.Duration flag with specified name, default value, and usage string. from the go flag.Duration()
func (*FlagSet) DurationVar ¶ added in v3.2.4
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) *Flag
DurationVar defines a time.Duration flag with specified name, default value, and usage string. from the go flag.DurationVar()
func (*FlagSet) Float64 ¶ added in v3.2.4
Float64 defines a float64 flag with specified name, default value, and usage string. from the go flag.Float64()
func (*FlagSet) Float64Var ¶ added in v3.2.4
Float64Var defines a float64 flag with specified name, default value, and usage string. from the go flag.Float64Var()
func (*FlagSet) Func ¶ added in v3.2.4
Func defines a flag with the specified name and usage string. from the go flag.Func()
func (*FlagSet) Int ¶ added in v3.2.4
Int defines an int flag with specified name, default value, and usage string. from the go flag.Int()
func (*FlagSet) Int64 ¶ added in v3.2.4
Int64 defines an int64 flag with specified name, default value, and usage string. from the go flag.Int64()
func (*FlagSet) Int64Var ¶ added in v3.2.4
Int64Var defines an int64 flag with specified name, default value, and usage string. from the go flag.Int64Var()
func (*FlagSet) IntVar ¶ added in v3.2.4
IntVar defines an int flag with specified name, default value, and usage string. from the go flag.IntVar()
func (*FlagSet) Lookup ¶ added in v3.2.4
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) Parse ¶ added in v3.2.4
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program.
The return value will be ErrHelp if -help or -h were set but not defined.
NOTE: refer from flag.FlagSet#Parse()
func (*FlagSet) String ¶ added in v3.2.4
String defines a string flag with specified name, default value, and usage string. from the go flag.String()
func (*FlagSet) StringVar ¶ added in v3.2.4
StringVar defines a string flag with specified name, default value, and usage string. from the go flag.StringVar()
func (*FlagSet) TextVar ¶ added in v3.2.4
func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) *Flag
TextVar defines a text flag with specified name, default value, and usage string. from the go flag.TextVar()
func (*FlagSet) Uint ¶ added in v3.2.4
Uint defines a uint flag with specified name, default value, and usage string. from the go flag.Uint()
func (*FlagSet) Uint64 ¶ added in v3.2.4
Uint64 defines a uint64 flag with specified name, default value, and usage string. from the go flag.Uint64()
func (*FlagSet) Uint64Var ¶ added in v3.2.4
Uint64Var defines a uint64 flag with specified name, default value, and usage string. from the go flag.Uint64Var()
func (*FlagSet) UintVar ¶ added in v3.2.4
UintVar defines a uint flag with specified name, default value, and usage string. from the go flag.UintVar()
type Flags ¶
type Flags struct { // --- cli options --- CliOpts // --- cli arguments --- CliArgs // Desc message Desc string // AfterParse options hook AfterParse func(fs *Flags) error // HandleFunc handle func, will call on Run() HandleFunc HandleFunc // contains filtered or unexported fields }
Flags cli flag options and arguments management, parsing, and binding.
type IntsString ¶ added in v3.2.2
type IntsString = cflag.IntsString
IntsString implemented flag.Value interface
type OptCategory ¶
OptCategory struct
type Parser ¶
type Parser = Flags
Parser type, alias of Flags type
func (*Parser) FSetArgs ¶
FSetArgs get all raw arguments. alias of the RawArgs() if have been called parse, the return is remaining args.
func (*Parser) FromStruct ¶
FromStruct from struct tag binding options
## Named rule(default)
// tag format: name=val0;shorts=i;required=true;desc=a message type UserCmdOpts struct { Name string `flag:"name=name;shorts=n;required=true;desc=input username"` Age int `flag:"name=age;shorts=a;required=true;desc=input user age"` } opt := &UserCmdOpts{} p.FromStruct(opt)
## Simple rule
// tag format1: name;desc;required;default;shorts // tag format2: desc;required;default;shorts type UserCmdOpts struct { Name string `flag:"input username;true;;n"` Age int `flag:"age;input user age;true;;o"` } opt := &UserCmdOpts{} p.FromStruct(opt, gflag.TagRuleSimple)
func (*Parser) MustFromStruct ¶ added in v3.2.2
MustFromStruct from struct tag binding options, panic if error
more see FromStruct()
func (*Parser) Parse ¶
Parse given arguments
Usage:
gf := gflag.New() gf.BoolOpt(&debug, "debug", "", defDebug, "open debug mode") gf.UintOpt(&port, "port", "p", 18081, "the http server port") err := gf.Parse(os.Args[1:])
func (*Parser) PrintHelpPanel ¶
func (p *Parser) PrintHelpPanel()
PrintHelpPanel for all options to the gf.out
func (*Parser) RawArgs ¶
RawArgs get all raw arguments. if have been called parse, the return is remaining args.
func (*Parser) Run ¶
Run parse options and arguments, and HandleFunc help render
Usage:
gf := gflag.New() ... // OR: gf.Run(nil) gf.Run(os.Args)
func (*Parser) SetHelpRender ¶
func (p *Parser) SetHelpRender(fn func())
SetHelpRender set the raw *flag.FlagSet.Usage
func (*Parser) SetRuleType ¶ added in v3.2.2
SetRuleType for the parse tag value rule string.
func (*Parser) UseSimpleRule ¶
UseSimpleRule for the parse tag value rule string. see TagRuleSimple
func (*Parser) WithConfigFn ¶
func (p *Parser) WithConfigFn(fns ...ConfigFunc) *Parser
WithConfigFn for the object.
type ValidValue ¶ added in v3.2.4
ValidValue is a flag.Value with a validation function.
func (*ValidValue) Set ¶ added in v3.2.4
func (v *ValidValue) Set(s string) error
Set value and validate