Documentation ¶
Overview ¶
Package flagstruct allows flags to be defined by struct tags and automatically filled into a struct.
For a flag to be registered the field must be exported, of a supported type, and have a flag struct tag.
The package example covers most variations.
Flag struct tag format ¶
The flag struct tag format is
flag:"name,default-value,description"
If name is omitted, the field's name is used, lowercased, with _ replaced by -.
If default-value is omitted, the appropriate zero value is used.
If description is omitted, the empty string is used.
If all are omitted the tag may be just:
flag:""
If you need to use commas in the default-value, you can change the separator used by <rune>:<specification>. For example,
flag:"|:name|a,b,c|description" flag:"::name:a,b,c:description" flag:"⊕:name⊕a,b,c⊕description"
Supported Types ¶
Supported types are:
string bool int int64 uint uint64 float64
Or any type whose underlying type is one of the above. A type whose underlying type is one of the above is treated as that type, with the sole exception of a time.Duration which is handled as a time.Duration.
Types whose field is exported are scanned for flags as well.
Example ¶
flags := struct { //the rune: rest pattern redefines the split character from , to rune Name string `flag:"|:name|Johnson, Rick|your name"` Simple_flag bool `flag:""` //flag will be named simple-flag Sub struct { On bool `flag:"on,,activate?"` //empty default value is zero Skip string //explicit tag required } }{} //this will be overwritten by flag parsing, if the flag is set flags.Name = "Angus" flags.Sub.Skip = "this is not set by flagstruct" //create a new flagstruct parser named example that fills flags. parser, err := New("example", &flags) if err != nil { log.Fatalln(err) } //this sets the passed in struct after normal flag parsing err = parser.Parse([]string{"-on", "-name", "John", "foo"}) if err != nil { log.Fatalln(err) } fmt.Println("Name:", flags.Name) fmt.Println("Simple_flag:", flags.Simple_flag) fmt.Println("On:", flags.Sub.On) fmt.Println("Skip:", flags.Sub.Skip) fmt.Println("Args:", parser.Args())
Output: Name: John Simple_flag: false On: true Skip: this is not set by flagstruct Args: [foo]
Index ¶
- type Parser
- func (p *Parser) Arg(i int) string
- func (p *Parser) Args() []string
- func (p *Parser) Lookup(name string) *flag.Flag
- func (p *Parser) NArg() int
- func (p *Parser) NFlag() int
- func (p *Parser) Parse(args []string) error
- func (p *Parser) Parsed() bool
- func (p *Parser) PrintDefaults(w io.Writer)
- func (p *Parser) Visit(fn func(*flag.Flag))
- func (p *Parser) VisitAll(fn func(*flag.Flag))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
A Parser wraps a FlagSet with the information necessary to fill a struct, as passed to New or Extend, in with the parsed flags
func Extend ¶
Extend fs with the flags defined in v.
When extending an existing flagset, Parse must be called on the returned parser even if the flagset has since been parsed.
It is up to the caller to ensure there are no naming colisions.
func (*Parser) Parse ¶
Parse parses flag definitions from the argument list, which should not include the command name.
The return value will be ErrHelp if -help was set but not defined.
This will populate the *struct passed to New.
If a FlagSet was extended with Extend this will parse it but it is safe to call this regardless.
func (*Parser) PrintDefaults ¶
PrintDefaults is like FlagSet.PrintDefaults to an io.Writer.