跳到主要内容

goflags

[!Tip]

本篇文章参考如下,如有侵权,请联系我删除。

License Go version Release Checks

An extension of the go flag library that adds convenience functions and functionalities like config file, better usage, short and long flag support, custom types for string slices and maps etc.

Features

  • In-built YAML Configuration file support.
  • Better usage instructions
  • Short and long flags support
  • Custom String Slice types with different options (comma-separated,normalized,etc)
  • Custom Map type
  • Flags grouping support (CreateGroup,SetGroup)

Usage

The following types are supported by the goflags library. The <name>P suffix means that the flag supports both a long and a short flag for the option.

Flag Types

FunctionDescription
BoolVarBoolean value with long name
BoolVarPBoolean value with long short name
DurationVarTime Duration value with long name
DurationVarPTime Duration value with long short name
IntVarInteger value with long name
IntVarPInteger value with long short name
PortVarPort value with long name
PortVarPPort value with long short name
RuntimeMapVarMap value with long name
RuntimeMapVarPMap value with long short name
StringSliceVarString Slice value with long name and options
StringSliceVarConfigOnlyString Slice value with long name read from config file only
StringSliceVarPString slice value with long short name and options
StringVarString value with long name
StringVarEnvString value with long short name read from environment
StringVarPString value with long short name
VarCustom value with long name implementing flag.Value interface
VarPCustom value with long short name implementing flag.Value interface
EnumVarEnum value with long name
EnumVarPEnum value with long short name

String Slice Options

String Slice OptionTokenizationNormalizationDescription
StringSliceOptionsNoneNoneDefault String Slice
CommaSeparatedStringSliceOptionsCommaNoneComma-separated string slice
FileCommaSeparatedStringSliceOptionsCommaNoneComma-separated items from file/cli
NormalizedOriginalStringSliceOptionsNoneStandardList of normalized string slice
FileNormalizedStringSliceOptionsCommaStandardList of normalized string slice from file/cli
FileStringSliceOptionsStandardStandardList of string slice from file
NormalizedStringSliceOptionsCommaStandardList of normalized string slice

Example

An example showing various options of the library is specified below.

package main

import (
"fmt"
"log"

"github.com/projectdiscovery/goflags"
)

type options struct {
silent bool
inputs goflags.StringSlice
config string
values goflags.RuntimeMap
}

const (
Nil goflags.EnumVariable = iota
Type1
Type2
)

func main() {
enumAllowedTypes := goflags.AllowdTypes{"type1": Type1, "type2": Type2}
opt := &options{}

flagSet := goflags.NewFlagSet()
flagSet.SetDescription("Test program to demonstrate goflags options")

flagSet.EnumVarP(&options.Type, "enum-type", "et", Nil, "Variable Type (type1/type2)", enumAllowedTypes)
flagSet.BoolVar(&opt.silent, "silent", true, "show silent output")
flagSet.StringSliceVarP(&opt.inputs, "inputs", "i", nil, "list of inputs (file,comma-separated)", goflags.FileCommaSeparatedStringSliceOptions)

// Group example
flagSet.CreateGroup("config", "Configuration",
flagSet.StringVar(&opt.config, "config", "", "file to read config from"),
flagSet.RuntimeMapVar(&opt.values, "values", nil, "key-value runtime values"),
)
if err := flagSet.Parse(); err != nil {
log.Fatalf("Could not parse flags: %s\n", err)
}
if opt.config != "" {
if err := flagSet.MergeConfigFile(opt.config); err != nil {
log.Fatalf("Could not merge config file: %s\n", err)
}
}
fmt.Printf("silent: %v inputs: %v config: %v values: %v\n", opt.silent, opt.inputs, opt.config, opt.values)
}

Thanks

  1. spf13/cobra - For the very nice usage template for the command line.
  2. nmap/nmap - For the service-port mapping and top-ports list.