flagstruct

package module
v0.0.0-...-347706b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2014 License: BSD-2-Clause Imports: 10 Imported by: 0

README

#flagstruct GoDoc Package flagstruct allows flags to be defined by struct tags and automatically filled into a struct.

Download:

go get github.com/jimmyfrasche/flagstruct

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 :. 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.


Automatically generated by autoreadme on 2014.09.27

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

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

func Extend(fs *flag.FlagSet, v interface{}) (*Parser, error)

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 New

func New(name string, v interface{}) (*Parser, error)

New creates a Parser named name with the type in v.

func (*Parser) Arg

func (p *Parser) Arg(i int) string

Arg is as defined on FlagSet.

func (*Parser) Args

func (p *Parser) Args() []string

Args is as defined on FlagSet.

func (*Parser) Lookup

func (p *Parser) Lookup(name string) *flag.Flag

Lookup is as defined on FlagSet.

func (*Parser) NArg

func (p *Parser) NArg() int

NArg is as defined on FlagSet.

func (*Parser) NFlag

func (p *Parser) NFlag() int

NFlag is as defined on FlagSet.

func (*Parser) Parse

func (p *Parser) Parse(args []string) error

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) Parsed

func (p *Parser) Parsed() bool

Parsed is as defined on FlagSet.

func (*Parser) PrintDefaults

func (p *Parser) PrintDefaults(w io.Writer)

PrintDefaults is like FlagSet.PrintDefaults to an io.Writer.

func (*Parser) Visit

func (p *Parser) Visit(fn func(*flag.Flag))

Visit is as defined on FlagSet.

func (*Parser) VisitAll

func (p *Parser) VisitAll(fn func(*flag.Flag))

VisitAll is as defined on FlagSet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL