flagx extends the standard library flag package, it provide more convinient flag types and environment variables support.
- support parse flag values from command line arguments and environment variables.
- more rich flag types: bytes, duration, text, array of string, array of int.
- support mark flag as required.
- string
- bool
- int
- duration
- bytes
- array of string
- array of int
import (
"github.com/chenyunfei/flagx"
)
var (
logFile = flagx.NewString("log.file", "", "the log file")
logLevel = flagx.NewBool("log.level", false, "the log level, default is bool")
serverHost = flagx.NewString("server.addr", ":7070", "the address http will serve on")
serverPort = flagx.NewInt("server.port", 8080, "the tcp port will listen on")
secs = flagx.NewDuration("secs", "30s", "the duration flag, 30 secodns")
bytes = flagx.NewBytes("bytes", 128, "the bytes flag, 128 Byte")
kbs = flagx.NewArrayString("domain", "the domain array flag, default is empty")
)
func main() {
flagx.Parse()
}-h or --help is builtin flag, when it is set, flagx will print usage and exit.
user can specify some options for each flag. such as:
logFile := flagx.NewString("api.listen", "", "the log file", flagx.Env("LISTEN"), flagx.Required())customize environment variable name for flag. default is upper case of flag name and replace . with _.
such as log.file will be LOG_FILE in environment variables.
mark flag as required. if flag is not set, it will print usage and exit.
mark flag as secret.
declare a string flag:
flagx.NewString("log.file", "", "the log file")more details see flag.go
declare a int flag:
flagx.NewInt("port", 8080, "the tcp port will listen on")more details see flag.go
declare a int64 flag:
flagx.NewInt64("bytes", 0, "the bytes flag, 0 Byte")more details see flag.go
declare a float flag:
flagx.NewFloat("float", 0.0, "the float flag, 0.0")more details see flag.go
declare a bool flag:
flagx.NewBool("log.color", false, "the log color, default is false")more details see flag.go
declare a bytes flag. It supports the following optional suffixes for values: KB, MB, GB, TB, KiB, MiB, GiB, TiB.
flagx.NewBytes("bytes", 0, "the bytes flag, 0 Byte")more details see bytes.go
declare a duration flag. It supports the following optional suffixes for values: s, m, h, d, w, M, y.
flagx.NewDuration("retention", "30d", "the retention policy")more details see duration.go
Text is a flag that used to hold complex content, such as json. the passed value should be base64-encoded string, it will be decoded before assignment.
flagx.NewText("text", "", "the text flag, default is empty")more details see text.go
you can define a array of string flag, which let user use -flag value1 -flag value2 or -flag value1,value2 to pass multiple values.
declare a array of string flag:
flagx.NewArrayString("domain", "the domain array flag, default is empty")user can pass multiple values for array of string flag:
--domain example.com --domain example.org
declare a array of int flag:
flagx.NewArrayInt("port", "the port array flag, default is empty")user can pass multiple values for array of int flag:
--port 8080 --port 8081
declare a array of duration flag. It supports the following optional suffixes for values: s, m, h, d, w, M, y.
flagx.NewArrayDuration("retention", "the retention policy")declare a array of bytes flag. It supports the following optional suffixes for values: KB, MB, GB, TB, KiB, MiB, GiB, TiB.
flagx.NewArrayBytes("bytes", "the bytes flag, 0 Byte")user can pass multiple values for array of bytes flag:
--bytes 128 --bytes 64KB
declare a array of bool flag:
flagx.NewArrayBool("bool", "the bool flag, false")user can pass multiple values for array of bool flag:
--bool true --bool false
declare a array of text flag:
flagx.NewArrayText("text", "the text flag, default is empty")user can pass multiple values for array of text flag:
--text aGVsbG8= --text aGVsbG93b3JsZQ==
visit flags that has been set.
import (
"fmt"
"flag"
)
flagx.Visit(func(f *flag.Flag) {
fmt.Printf("%s: %v\n", f.Name, f.Value)
})visit all flags, no matter it is set or not.
import (
"fmt"
"flag"
)
flagx.VisitAll(func(f *flag.Flag) {
fmt.Printf("%s: %v\n", f.Name, f.Value)
})