Documentation ¶
Overview ¶
Package gflag provide command line options and arguments binding, parse, management.
Index ¶
- Constants
- Variables
- 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.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 (ops *CliOpts) Bool(name, shorts string, defVal bool, desc string) *bool
- func (ops *CliOpts) BoolOpt(ptr *bool, name, shorts string, defVal bool, desc string)
- func (ops *CliOpts) BoolOpt2(p *bool, nameAndShorts, desc string, setFns ...CliOptFn)
- func (ops *CliOpts) BoolVar(ptr *bool, opt *CliOpt)
- func (ops *CliOpts) Float64Opt(p *float64, name, shorts string, defVal float64, desc string)
- func (ops *CliOpts) Float64Var(ptr *float64, opt *CliOpt)
- func (ops *CliOpts) HasOption(name string) bool
- func (ops *CliOpts) InitFlagSet(name string)
- func (ops *CliOpts) Int(name, shorts string, defVal int, desc string) *int
- func (ops *CliOpts) Int64(name, shorts string, defVal int64, desc string) *int64
- func (ops *CliOpts) Int64Opt(ptr *int64, name, shorts string, defValue int64, desc string)
- func (ops *CliOpts) Int64Var(ptr *int64, opt *CliOpt)
- func (ops *CliOpts) IntOpt(p *int, name, shorts string, defVal int, desc string)
- func (ops *CliOpts) IntOpt2(p *int, nameAndShorts, desc string, setFns ...CliOptFn)
- func (ops *CliOpts) IntVar(p *int, opt *CliOpt)
- func (ops *CliOpts) IsOption(name string) bool
- func (ops *CliOpts) IsShortName(short string) bool
- func (ops *CliOpts) IsShortOpt(short string) bool
- func (ops *CliOpts) IterAll(fn func(f *flag.Flag, opt *CliOpt))
- func (ops *CliOpts) LookupFlag(name string) *flag.Flag
- func (ops *CliOpts) Opt(name string) *CliOpt
- func (ops *CliOpts) Opts() map[string]*CliOpt
- func (ops *CliOpts) SetName(name string)
- func (ops *CliOpts) ShortNames(name string) (ss []string)
- func (ops *CliOpts) Str(name, shorts string, defVal, desc string) *string
- func (ops *CliOpts) StrOpt(p *string, name, shorts string, defValAndDesc ...string)
- func (ops *CliOpts) StrOpt2(p *string, nameAndShorts, desc string, setFns ...CliOptFn)
- func (ops *CliOpts) StrVar(p *string, opt *CliOpt)
- func (ops *CliOpts) Uint(name, shorts string, defVal uint, desc string) *uint
- func (ops *CliOpts) Uint64(name, shorts string, defVal uint64, desc string) *uint64
- func (ops *CliOpts) Uint64Opt(ptr *uint64, name, shorts string, defVal uint64, desc string)
- func (ops *CliOpts) Uint64Var(ptr *uint64, opt *CliOpt)
- func (ops *CliOpts) UintOpt(ptr *uint, name, shorts string, defValue uint, desc string)
- func (ops *CliOpts) UintVar(ptr *uint, opt *CliOpt)
- func (ops *CliOpts) Var(ptr flag.Value, opt *CliOpt)
- func (ops *CliOpts) VarOpt(v flag.Value, name, shorts, desc string)
- func (ops *CliOpts) VarOpt2(v flag.Value, nameAndShorts, desc string, setFns ...CliOptFn)
- type ConfString
- type Config
- type ConfigFunc
- type EnumString
- type Flags
- 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) FSet() *flag.FlagSet
- 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) SetFlagSet(fSet *flag.FlagSet)
- func (p *Parser) SetHandle(fn HandleFunc) *Parser
- 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
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 ¶
This section is empty.
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
// short 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
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) 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.Flag by name
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 }
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 IntsString ¶ added in v3.2.2
type IntsString = cflag.IntsString
IntsString implemented flag.Value interface
type OptCategory ¶
OptCategory struct
type Parser ¶
type Parser struct { // --- cli options --- CliOpts // --- cli arguments --- CliArgs // Desc message Desc string // AfterParse options hook AfterParse func(fs *Parser) error // contains filtered or unexported fields }
Parser cli flag options and arguments binding management and parsing.
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:
// 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 flags parse and handle help render
Usage:
gf := gflag.New() ... // OR: gf.Run(nil) gf.Run(os.Args)
func (*Parser) SetFlagSet ¶
SetFlagSet set the raw *flag.FlagSet
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.