pflag

repository·master·Indexed 25 days ago

https://github.com/spf13/pflag

A drop-in replacement for Go's standard flag package that implements POSIX/GNU-style --flags, allowing for long flags and single-dash shorthands.

Tokens
17.8K
Snippets
16
Records
169
Agent score
81%

What's inside pflag

  1. Use pflag as a drop-in replacement for Go's flag package

    master

    You can use pflag as a replacement for the standard Go flag package by importing it under the name flag. Most code will function without changes.

    Exception: If you directly instantiate the Flag struct, you must manually set the Shorthand field.

    import flag "github.com/spf13/pflag"
  2. Use pflag with go test

    master

    When using pflag in TestMain, pflag.Parse() might skip over go test's built-in shorthand flags (those starting with -test.). To ensure these are parsed correctly, use ParseSkippedFlags to parse the standard Go flags separately.

    package main
    
    import (
    	goflag "flag"
        "os"
    	flag "github.com/spf13/pflag"
    )
    
    var ip = flag.Int("flagname", 1234, "help message for flagname")
    
    func main() {
    	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    	flag.ParseSkippedFlags(os.Args[1:], goflag.CommandLine)
    	flag.Parse()
    }
  3. Disable flag sorting in help output

    master

    By default, pflag sorts flags in the help/usage message. You can disable this by setting the SortFlags field to false on a FlagSet. To disable it for the global command line, use pflag.CommandLine.SortFlags = false.

    flags := pflag.NewFlagSet("example", pflag.ContinueOnError)
    flags.BoolP("verbose", "v", false, "verbose output")
    flags.SortFlags = false
    flags.PrintDefaults()
  4. Define flags with shorthands

    master

    To provide one-letter shorthands for flags, use the functions that end with P. These allow users to use single-dash shorthand syntax (e.g., -f).

    var ip = flag.IntP("flagname", "f", 1234, "help message")
    var flagvar bool
    func init() {
        flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
    }
    flag.VarP(&flagVal, "varname", "v", "help message")
  5. Set no option default values for flags

    master
    By setting NoOptDefVal on a flag, you can change its behavior when the flag is present on the command line without an explicit value. If the flag is provided without an option, it will be set to the NoOptDefVal instead of its standard default.
  6. Normalize flag names

    master

    You can implement a custom normalization function using SetNormalizeFunc on a FlagSet. This allows you to treat different flag names (e.g., using -, _, or .) as the same flag during comparison.

    func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
    	from := []string{"-", "_"}
    	to := "."
    	for _, sep := range from {
    		name = strings.Replace(name, sep, to, -1)
    	}
    	return pflag.NormalizedName(name)
    }
    
    myFlagSet.SetNormalizeFunc(wordSepNormalizeFunc)
  7. Support Go's native flags in pflag

    master

    To support flags defined by the standard flag package (often used by third-party dependencies), add the Go flag set to your pflag flag set using AddGoFlagSet.

    package main
    
    import (
    	goflag "flag"
    	flag "github.com/spf13/pflag"
    )
    
    var ip = flag.Int("flagname", 1234, "help message for flagname")
    
    func main() {
    	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
    	flag.Parse()
    }
  8. Deprecate flags or shorthands

    master
    Use MarkDeprecated to deprecate a full flag name or MarkShorthandDeprecated to deprecate only its shorthand. Deprecated flags are hidden from help text and trigger a usage message when used.
  9. Define and parse flags

    master

    Define flags using standard types like String(), Bool(), Int(), etc. You can also bind flags to existing variables using Var() functions (e.g., IntVar()). After defining all flags, call flag.Parse() to process command-line arguments.

    var ip *int = flag.Int("flagname", 1234, "help message for flagname")
    
    var flagvar int
    func init() {
        flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
    }
    
    func main() {
        flag.Parse()
        fmt.Println("ip has value ", *ip)
        fmt.Println("flagvar has value ", flagvar)
    }