Documentation ¶
Overview ¶
Package flagx implements extensions to the standard flag package in the form of types that implement flag.Value
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolFunc ¶ added in v0.22.2
BoolFunc defines a flag with the specified name and usage string. Each time the flag is set with a truthy value, fn is called. If fn returns a non-nil error, it will be treated as a flag value parsing error.
Example ¶
package main import ( "flag" "fmt" "github.com/carlmjohnson/flagx" ) func main() { fs := flag.NewFlagSet("ExampleParseEnv", flag.PanicOnError) flagx.BoolFunc(fs, "call-me", "", func() error { fmt.Println("called!") return nil }) flagx.BoolFunc(fs, "dont-call-me", "", func() error { fmt.Println("not called!") return nil }) fs.Parse([]string{"-call-me", "-dont-call-me=false"}) }
Output: called!
func Missing ¶
Missing returns a slice of required flags missing from an error returned by MustHave.
func MustHave ¶
MustHave is a convenience function that checks that the named flags were set on fl. Missing flags are treated with the policy of fl.ErrorHandling(): ExitOnError, ContinueOnError, or PanicOnError. Returned errors can be unpacked by Missing.
If nil, fl defaults to flag.CommandLine.
Example (MissingFlag) ¶
package main import ( "flag" "fmt" "os" "github.com/carlmjohnson/flagx" ) func main() { fs := flag.NewFlagSet("ExampleMustHave", flag.ContinueOnError) fs.SetOutput(os.Stdout) fs.String("a", "", "this value must be set") fs.String("b", "", "this value must be set") fs.String("c", "", "this value is optional") fs.Parse([]string{"-a", "set"}) err := flagx.MustHave(fs, "a", "b") fmt.Println("Missing:", flagx.Missing(err)) }
Output: missing required flag: b Usage of ExampleMustHave: -a string this value must be set -b string this value must be set -c string this value is optional Missing: [b]
Example (NoMissingFlag) ¶
package main import ( "flag" "github.com/carlmjohnson/flagx" ) func main() { fs := flag.NewFlagSet("ExampleMustHave", flag.PanicOnError) fs.String("a", "", "this value must be set") fs.String("b", "", "this value must be set") fs.String("c", "", "this value is optional") fs.Parse([]string{"-a", "set", "-b", "set"}) flagx.MustHave(fs, "a", "b") }
Output:
func MustHaveArgs ¶
MustHaveArgs is a convenience function that checks that fl.NArg() is within the bounds min and max (inclusive). Use max -1 to indicate no maximum value. MustHaveArgs uses the policy of fl.ErrorHandling(): ExitOnError, ContinueOnError, or PanicOnError.
If nil, fl defaults to flag.CommandLine.
Example (CorrectNumber) ¶
package main import ( "flag" "github.com/carlmjohnson/flagx" ) func main() { fs := flag.NewFlagSet("ExampleMustHave", flag.PanicOnError) fs.String("a", "", "an option") fs.Parse([]string{"--", "-a", "-b", "-c"}) flagx.MustHaveArgs(fs, 3, 3) }
Output:
Example (WrongNumber) ¶
package main import ( "flag" "fmt" "strings" "github.com/carlmjohnson/flagx" ) func main() { var buf strings.Builder defer func() { recover() fmt.Println(buf.String()) }() fs := flag.NewFlagSet("ExampleMustHaveArgs", flag.PanicOnError) fs.SetOutput(&buf) fs.Usage = func() { fmt.Fprintf(fs.Output(), "Usage:\n\tExampleMustHaveArgs [optional arg]") } fs.Parse([]string{"--", "one", "two"}) flagx.MustHaveArgs(fs, 0, 1) }
Output: must have between 0 and 1 args; got 2 Usage: ExampleMustHaveArgs [optional arg]
func ParseEnv ¶
ParseEnv lists any unset flags, checks whether a corresponding environment variable exists, and if so calls Set with its value. Flag names are prefixed and converted to SCREAMING_SNAKE_CASE when looking up environment variables.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/carlmjohnson/flagx" ) func main() { fs := flag.NewFlagSet("ExampleParseEnv", flag.PanicOnError) a := fs.Int("a", 0, "") b := fs.Int("b", 0, "") fs.Parse([]string{"-a", "1"}) os.Setenv("TEST_ENV_A", "2") os.Setenv("TEST_ENV_B", "3") flagx.ParseEnv(fs, "test-env") // Does not override existing values fmt.Println("a", *a) // Does get new values from env fmt.Println("b", *b) }
Output: a 1 b 3
Types ¶
This section is empty.