Documentation
¶
Overview ¶
Package flags is for parsing execution flags.
Usage ¶
Create a struct that requires a flag and parse:
type Config struct {
ConnectionString string `flag:"cs"`
Driver string `flag:"driver"`
Args []string `flag:"*"`
}
func main() {
conf := new(Config)
if err := flags.Parse(conf); err != nil {
...
The '*' can be used to parse in all the args
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse(obj interface{}) error
Parse returns the
Example ¶
// Input: app -p 8080 -driver postgres arg1 arg2 arg3
os.Args = []string{`app`, `-p`, `8080`, `-driver`, `postgres`, `arg1`, `arg2`, `arg3`}
type Config struct {
Port int `flag:"p"`
DBConnectionString string `flag:"cs"`
DBDriver string `flag:"driver"`
Args []string `flag:"*"`
}
c := new(Config)
if err := Parse(c); err != nil {
panic(err)
}
fmt.Printf("port: %d, connection string: '%s', driver: '%s', args: %q\n", c.Port, c.DBConnectionString, c.DBDriver, c.Args)
Output: port: 8080, connection string: '', driver: 'postgres', args: ["arg1" "arg2" "arg3"]
Types ¶
Click to show internal directories.
Click to hide internal directories.