- Notifications
You must be signed in to change notification settings - Fork38
Generate flags by parsing structures
License
urfave/sflags
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The sflags package uses structs, reflection and struct field tagsto allow you specify command line options. It supportsdifferent types andfeatures.
An example:
typeHTTPConfigstruct {Hoststring`desc:"HTTP host"`Portint`flag:"port p" desc:"some port"`SSLbool`env:"HTTP_SSL_VALUE"`Timeout time.Duration`flag:",deprecated,hidden"`}typeConfigstruct {HTTPHTTPConfigStatsStatsConfig}
And you can use your favorite flag or cli library!
Hidden | Deprecated | Short | Env | Required | ||
---|---|---|---|---|---|---|
example | - | - | - | - | - | |
example | ||||||
example | - | - | ||||
example | - | - | ||||
example | - |
- - feature is supported and implemented
-
- feature can't be implemented for this cli library
- Set environment name
- Set usage
- Long and short forms
- Skip field
- Required
- Placeholders (by
name
) - Deprecated and hidden options
- Multiple ENV names
- Interface for user types.
- Validation (usinggovalidator package)
- Anonymous nested structure support (anonymous structures flatten by default)
int
,int8
,int16
,int32
,int64
uint
,uint8
,uint16
,uint32
,uint64
float32
,float64
- slices for all previous numeric types (e.g.
[]int
,[]float64
) bool
[]bool
string
[]string
- nested structures
- net.TCPAddr
- net.IP
- time.Duration
- regexp.Regexp
- map for all previous types (e.g.
map[int64]bool
,map[string]float64
)
HexBytes
count
ipmask
enum values
enum list values
file
file list
url
url list
units (bytes 1kb = 1024b, speed, etc)
The code below shows how to usesflags
with theflag library. Examples for otherflag libraries are available from./examples dir.
package mainimport ("flag""log""time""github.com/urfave/sflags/gen/gflag")typehttpConfigstruct {Hoststring`desc:"HTTP host"`PortintSSLboolTimeout time.Duration}typeconfigstruct {HTTPhttpConfig}funcmain() {cfg:=&config{HTTP:httpConfig{Host:"127.0.0.1",Port:6000,SSL:false,Timeout:15*time.Second,},}err:=gflag.ParseToDef(cfg)iferr!=nil {log.Fatalf("err: %v",err)}flag.Parse()}
That code generates next output:
$ go run ./main.go --helpUsage of _obj/exe/main: -http-host value HTTP host (default 127.0.0.1) -http-port value (default 6000) -http-ssl -http-timeout value (default 15s)exit status 2
The flag default key string is the struct field name but can be specified in the struct field's tag value.The "flag" key in the struct field's tag value is the key name, followed by an optional comma and options. Examples:
// Field is ignored by this package.Fieldint`flag:"-"`// Field appears in flags as "myName".Fieldint`flag:"myName"`// If this field is from nested struct, prefix from parent struct will be ingored.Fieldint`flag:"~myName"`// You can set short name for flags by providing it's value after a space// Prefixes will not be applied for short names.Fieldint`flag:"myName a"`// this field will be removed from generated help text.Fieldint`flag:",hidden"`// this field will be marked as deprecated in generated help textFieldint`flag:",deprecated"`
If you specify description in description tag (desc
by default) it will be used in USAGE section.
Addrstring`desc:"HTTP host"````shthis description produces something like:
-addr valueHTTP host (default 127.0.0.1)
## Options for env tag## Options for Parse function:```golang// DescTag sets custom description tag. It is "desc" by default.func DescTag(val string)// FlagTag sets custom flag tag. It is "flag" be default.func FlagTag(val string)// Prefix sets prefix that will be applied for all flags (if they are not marked as ~).func Prefix(val string)// EnvPrefix sets prefix that will be applied for all environment variables (if they are not marked as ~).func EnvPrefix(val string)// FlagDivider sets custom divider for flags. It is dash by default. e.g. "flag-name".func FlagDivider(val string)// EnvDivider sets custom divider for environment variables.// It is underscore by default. e.g. "ENV_NAME".func EnvDivider(val string)// Validator sets validator function for flags.// Check existed validators in sflags/validator package.func Validator(val ValidateFunc)// Set to false if you don't want anonymous structure fields to be flatten.func Flatten(val bool)
- kingpin doesn't pass value for boolean arguments. Counter can't get initial value from arguments.
About
Generate flags by parsing structures