Documentation ¶
Overview ¶
Example (TextVar) ¶
package main import ( "fmt" "net" "os" "github.com/cristalhq/flagx" ) func main() { fs := flagx.NewFlagSet("ExampleTextVar", os.Stdout) var ip net.IP fs.Text(&ip, "ip", "", net.IPv4(192, 168, 0, 100), "`IP address` to parse") err := fs.Parse([]string{"-ip", "127.0.0.1"}) if err != nil { panic(err) } fmt.Printf("{ip: %v}\n\n", ip) }
Output: {ip: 127.0.0.1}
Index ¶
- type FlagSet
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) AsStdlib() *flag.FlagSet
- func (f *FlagSet) Bool(p *bool, name, alias string, value bool, usage string)
- func (f *FlagSet) BoolSlice(p *[]bool, name, alias string, value []bool, sep, usage string)
- func (f *FlagSet) Duration(p *time.Duration, name, alias string, value time.Duration, usage string)
- func (f *FlagSet) DurationSet(p *map[time.Duration]struct{}, name, alias string, ...)
- func (f *FlagSet) DurationSlice(p *[]time.Duration, name, alias string, value []time.Duration, ...)
- func (f *FlagSet) Float64(p *float64, name, alias string, value float64, usage string)
- func (f *FlagSet) Float64Set(p *map[float64]struct{}, name, alias string, value map[float64]struct{}, ...)
- func (f *FlagSet) Float64Slice(p *[]float64, name, alias string, value []float64, sep, usage string)
- func (f *FlagSet) Func(name, alias, usage string, fn func(string) error)
- func (f *FlagSet) Int(p *int, name, alias string, value int, usage string)
- func (f *FlagSet) Int64(p *int64, name, alias string, value int64, usage string)
- func (f *FlagSet) Int64Set(p *map[int64]struct{}, name, alias string, value map[int64]struct{}, ...)
- func (f *FlagSet) Int64Slice(p *[]int64, name, alias string, value []int64, sep, usage string)
- func (f *FlagSet) IntSet(p *map[int]struct{}, name, alias string, value map[int]struct{}, usage string)
- func (f *FlagSet) IntSlice(p *[]int, name, alias string, value []int, sep, usage string)
- func (f *FlagSet) IsParsed() bool
- func (f *FlagSet) Lookup(name string) *flag.Flag
- func (f *FlagSet) NArg() int
- func (f *FlagSet) NFlag() int
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) Regexp(p *regexp.Regexp, name, alias string, value string, usage string)
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) String(p *string, name, alias, value, usage string)
- func (f *FlagSet) StringSet(p *map[string]struct{}, name, alias string, value map[string]struct{}, ...)
- func (f *FlagSet) StringSlice(p *[]string, name, alias string, value []string, sep, usage string)
- func (f *FlagSet) Text(p encoding.TextUnmarshaler, name, alias string, value encoding.TextMarshaler, ...)
- func (f *FlagSet) Uint(p *uint, name, alias string, value uint, usage string)
- func (f *FlagSet) Uint64(p *uint64, name, alias string, value uint64, usage string)
- func (f *FlagSet) Uint64Set(p *map[uint64]struct{}, name, alias string, value map[uint64]struct{}, ...)
- func (f *FlagSet) Uint64Slice(p *[]uint64, name, alias string, value []uint64, sep, usage string)
- func (f *FlagSet) UintSet(p *map[uint]struct{}, name, alias string, value map[uint]struct{}, ...)
- func (f *FlagSet) UintSlice(p *[]uint, name, alias string, value []uint, sep, usage string)
- func (f *FlagSet) Var(value flag.Value, name, alias, usage string)
- func (f *FlagSet) Visit(fn func(*flag.Flag))
- func (f *FlagSet) VisitAll(fn func(*flag.Flag))
- type Getter
- type SetOfDuration
- type SetOfFloat64
- type SetOfInt
- type SetOfInt64
- type SetOfString
- type SetOfUint
- type SetOfUint64
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet represents a set of flags. Flag names must be unique within a FlagSet. An attempt to define a flag whose name is already in use will cause a panic.
Example ¶
package main import ( "fmt" "os" "time" "github.com/cristalhq/flagx" ) func main() { args := []string{"-t", "20s"} // or os.Args[1:] var d time.Duration fset := flagx.NewFlagSet("testing", os.Stderr) fset.Duration(&d, "timeout", "t", 10*time.Second, "just a timeout") err := fset.Parse(args) if err != nil { panic(err) } fmt.Println(d) }
Output: 20s
func NewFlagSet ¶
NewFlagSet returns new FlagSet.
func (*FlagSet) AsStdlib ¶ added in v0.2.0
AsStdlib returns *flag.FlagSet with all flags. Note: aliases are duplicated.
func (*FlagSet) Bool ¶
Bool defines a bool flag with specified name, alias, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) BoolSlice ¶ added in v0.2.0
BoolSlice defines a slice of bool flag with specified name, alias, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. Empty string for alias means no alias will be created. BoolSlice panics on empty separator.
func (*FlagSet) Duration ¶
Duration defines a time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created.
func (*FlagSet) DurationSet ¶ added in v0.2.0
func (f *FlagSet) DurationSet(p *map[time.Duration]struct{}, name, alias string, value map[time.Duration]struct{}, usage string)
DurationSet defines a set of time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created.
func (*FlagSet) DurationSlice ¶ added in v0.2.0
func (f *FlagSet) DurationSlice(p *[]time.Duration, name, alias string, value []time.Duration, sep, usage string)
DurationSlice defines a slice of time.Duration flag with specified name, alias, default value, separator, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created. DurationSlice panics on empty separator.
func (*FlagSet) Float64 ¶
Float64 defines a float64 flag with specified name, alias, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Float64Set ¶ added in v0.2.0
func (f *FlagSet) Float64Set(p *map[float64]struct{}, name, alias string, value map[float64]struct{}, usage string)
Float64Set defines a set of float64 flag with specified name, alias, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Float64Slice ¶ added in v0.2.0
func (f *FlagSet) Float64Slice(p *[]float64, name, alias string, value []float64, sep, usage string)
Float64Slice defines a slice of float64 flag with specified name, alias, default value, separator, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Float64Slice panics on empty separator.
func (*FlagSet) Func ¶
Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. Empty string for alias means no alias will be created.
func (*FlagSet) Int ¶
Int defines an int flag with specified name, alias, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Int64 ¶
Int64 defines an int64 flag with specified name, alias, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Int64Set ¶ added in v0.2.0
func (f *FlagSet) Int64Set(p *map[int64]struct{}, name, alias string, value map[int64]struct{}, usage string)
Int64Set defines a set of int64 flag with specified name, alias, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Int64Slice ¶ added in v0.2.0
Int64Slice defines a slice of int64 flag with specified name, alias, default value, separator, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Int64Slice panics on empty separator.
func (*FlagSet) IntSet ¶ added in v0.2.0
func (f *FlagSet) IntSet(p *map[int]struct{}, name, alias string, value map[int]struct{}, usage string)
IntSet defines a set of int flag with specified name, alias, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) IntSlice ¶ added in v0.2.0
IntSlice defines a slice of int flag with specified name, alias, default value, separator, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created. IntSlice panics on empty separator.
func (*FlagSet) PrintDefaults ¶
func (f *FlagSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set.
func (*FlagSet) Regexp ¶ added in v0.5.0
BoolSlice defines a slice of bool flag with specified name, alias, default value, separator, and usage string. Duration defines a time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created. Until https://github.com/golang/go/issues/46159 is implemented.
func (*FlagSet) String ¶
String defines a string flag with specified name, alias, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) StringSet ¶ added in v0.2.0
func (f *FlagSet) StringSet(p *map[string]struct{}, name, alias string, value map[string]struct{}, usage string)
StringSet defines a set of string flag with specified name, alias, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) StringSlice ¶ added in v0.2.0
StringSlice defines a slice of string flag with specified name, alias, default value, separator, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created. StringSlice panics on empty separator.
func (*FlagSet) Text ¶ added in v0.2.1
func (f *FlagSet) Text(p encoding.TextUnmarshaler, name, alias string, value encoding.TextMarshaler, usage string)
Text defines a flag with the specified name and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p. Empty string for alias means no alias will be created.
func (*FlagSet) Uint ¶
Uint defines a uint flag with specified name, alias, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Uint64 ¶
Uint64 defines a uint64 flag with specified name, alias, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Uint64Set ¶ added in v0.2.0
func (f *FlagSet) Uint64Set(p *map[uint64]struct{}, name, alias string, value map[uint64]struct{}, usage string)
Uint64Set defines a set of uint64 flag with specified name, alias, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) Uint64Slice ¶ added in v0.2.0
Uint64Slice defines a slice of uint64 flag with specified name, alias, default value, separator, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Uint64Slice panics on empty separator.
func (*FlagSet) UintSet ¶ added in v0.2.0
func (f *FlagSet) UintSet(p *map[uint]struct{}, name, alias string, value map[uint]struct{}, usage string)
UintSet defines a set of uint flag with specified name, alias, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created.
func (*FlagSet) UintSlice ¶ added in v0.2.0
UintSlice defines a slice of uint flag with specified name, alias, default value, separator, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created. UintSlice panics on empty separator.
func (*FlagSet) Var ¶
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice. Empty string for alias means no alias will be created.
type SetOfDuration ¶ added in v0.2.0
func (*SetOfDuration) Set ¶ added in v0.2.0
func (sd *SetOfDuration) Set(v string) error
Set is flag.Value.Set
func (*SetOfDuration) String ¶ added in v0.2.0
func (sd *SetOfDuration) String() string
type SetOfFloat64 ¶ added in v0.2.0
type SetOfFloat64 map[float64]struct{}
func (*SetOfFloat64) Set ¶ added in v0.2.0
func (sf *SetOfFloat64) Set(v string) error
Set is flag.Value.Set
func (*SetOfFloat64) String ¶ added in v0.2.0
func (sf *SetOfFloat64) String() string
type SetOfInt ¶ added in v0.2.0
type SetOfInt map[int]struct{}
type SetOfInt64 ¶ added in v0.2.0
type SetOfInt64 map[int64]struct{}
func (*SetOfInt64) Set ¶ added in v0.2.0
func (si *SetOfInt64) Set(v string) error
Set is flag.Value.Set
func (*SetOfInt64) String ¶ added in v0.2.0
func (si *SetOfInt64) String() string
type SetOfString ¶ added in v0.2.0
type SetOfString map[string]struct{}
func (*SetOfString) Set ¶ added in v0.2.0
func (ss *SetOfString) Set(v string) error
Set is flag.Value.Set TODO(cristaloleg): how to configure separator? , \t |
func (*SetOfString) String ¶ added in v0.2.0
func (ss *SetOfString) String() string
type SetOfUint ¶ added in v0.2.0
type SetOfUint map[uint]struct{}
type SetOfUint64 ¶ added in v0.2.0
type SetOfUint64 map[uint64]struct{}
func (*SetOfUint64) Set ¶ added in v0.2.0
func (su *SetOfUint64) Set(v string) error
Set is flag.Value.Set
func (*SetOfUint64) String ¶ added in v0.2.0
func (su *SetOfUint64) String() string