go-flags cmd or ini parser

Jesse van den Kieboom 3927b71304 Merge pull request #342 from sv99/help-pos-required 6 months ago
examples 206428b03a Implement error interface 2 years ago
.travis.yml 8884f75389 Run travis on ppc 8 months ago
LICENSE 462a1605a2 Added license 9 years ago
README.md 594cb3b98d Update README.md 3 years ago
arg.go 1fb249163b Add specification of required rest arg range 5 years ago
arg_test.go 1fb249163b Add specification of required rest arg range 5 years ago
assert_test.go 3688adccfd Clarify that an error was expected but did not occur 3 years ago
check_crosscompile.sh 249bd42be6 Cross compile solaris/amd64 2 years ago
closest.go 627db099e9 Use dynamic programming for Levenshtein distance. 7 years ago
command.go d761e615ee Rename canCli to showInHelp 3 years ago
command_test.go e7ac6cd8ef move tests for PassAfterNonOption with commands to command_test 3 years ago
completion.go c0b9c5121d Cleanup from linting 8 months ago
completion_test.go a740d3cf59 completion: do not complete hidden commands 2 years ago
convert.go dfeb63c261 ini: fix IniParser.write() for zero values 1 year ago
convert_test.go 6e4f4168f1 Make wrapText newline aware 6 years ago
error.go 206428b03a Implement error interface 2 years ago
example_test.go b350cb1dee Fix various linting warnings 5 years ago
flags.go 19532f25ea Explain how to use choices 3 years ago
go.mod 0fb31d3686 Use modules 8 months ago
go.sum 0fb31d3686 Use modules 8 months ago
group.go 2442d95fb2 use an interface{} instead of reflect.Value 2 years ago
group_test.go 2442d95fb2 use an interface{} instead of reflect.Value 2 years ago
help.go 3927b71304 Merge pull request #342 from sv99/help-pos-required 6 months ago
help_test.go 3927b71304 Merge pull request #342 from sv99/help-pos-required 6 months ago
ini.go b29baf28fc Make option.set public 8 months ago
ini_test.go 1878de2732 Clear reference values (maps and slices) when first set 8 months ago
long_test.go db299b2529 Move tests into the flags package 8 years ago
man.go be6ab81d9e Do not quote new lines in description for man pages 8 months ago
marshal_test.go 6a7aff698c Run go fmt 5 years ago
multitag.go 1ff3faf492 Explicitly check for newline errors in tag values 8 years ago
option.go b29baf28fc Make option.set public 8 months ago
options_test.go 3c0b5a69cd Fix PassAfterNonOption not working with positional args 5 years ago
optstyle_other.go d2c1a59016 Allow forcing posix style flags on Windows 5 years ago
optstyle_windows.go d2c1a59016 Allow forcing posix style flags on Windows 5 years ago
parser.go b29baf28fc Make option.set public 8 months ago
parser_test.go a34a5e386e include flag name when erroring on env default 1 year ago
pointer_test.go f0ccfa98d0 Initialize custom marshaler pointer as needed 4 years ago
short_test.go f654ab1443 Make falsy tag strings behave intuitively 4 years ago
tag_test.go c0b9c5121d Cleanup from linting 8 months ago
termsize.go c6f86cb83a Use golang.org/x/sys/unix to determine terminal width to support aix/ppc64 2 years ago
termsize_nosysioctl.go c6f86cb83a Use golang.org/x/sys/unix to determine terminal width to support aix/ppc64 2 years ago
termsize_windows.go b87a73dcfd Support windows console width detection 3 years ago
unknown_test.go db299b2529 Move tests into the flags package 8 years ago

README.md

go-flags: a go library for parsing command line arguments

GoDoc Build Status Coverage Status

This library provides similar functionality to the builtin flag library of go, but provides much more functionality and nicer formatting. From the documentation:

Package flags provides an extensive command line option parser. The flags package is similar in functionality to the go builtin flag package but provides more options and uses reflection to provide a convenient and succinct way of specifying command line options.

Supported features:

  • Options with short names (-v)
  • Options with long names (--verbose)
  • Options with and without arguments (bool v.s. other type)
  • Options with optional arguments and default values
  • Multiple option groups each containing a set of options
  • Generate and print well-formatted help message
  • Passing remaining command line arguments after -- (optional)
  • Ignoring unknown command line options (optional)
  • Supports -I/usr/include -I=/usr/include -I /usr/include option argument specification
  • Supports multiple short options -aux
  • Supports all primitive go types (string, int{8..64}, uint{8..64}, float)
  • Supports same option multiple times (can store in slice or last option counts)
  • Supports maps
  • Supports function callbacks
  • Supports namespaces for (nested) option groups

The flags package uses structs, reflection and struct field tags to allow users to specify command line options. This results in very simple and concise specification of your application options. For example:

type Options struct {
	Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
}

This specifies one option with a short name -v and a long name --verbose. When either -v or --verbose is found on the command line, a 'true' value will be appended to the Verbose field. e.g. when specifying -vvv, the resulting value of Verbose will be {[true, true, true]}.

Example:

var opts struct {
	// Slice of bool will append 'true' each time the option
	// is encountered (can be set multiple times, like -vvv)
	Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`

	// Example of automatic marshalling to desired type (uint)
	Offset uint `long:"offset" description:"Offset"`

	// Example of a callback, called each time the option is found.
	Call func(string) `short:"c" description:"Call phone number"`

	// Example of a required flag
	Name string `short:"n" long:"name" description:"A name" required:"true"`

	// Example of a flag restricted to a pre-defined set of strings
	Animal string `long:"animal" choice:"cat" choice:"dog"`

	// Example of a value name
	File string `short:"f" long:"file" description:"A file" value-name:"FILE"`

	// Example of a pointer
	Ptr *int `short:"p" description:"A pointer to an integer"`

	// Example of a slice of strings
	StringSlice []string `short:"s" description:"A slice of strings"`

	// Example of a slice of pointers
	PtrSlice []*string `long:"ptrslice" description:"A slice of pointers to string"`

	// Example of a map
	IntMap map[string]int `long:"intmap" description:"A map from string to int"`
}

// Callback which will invoke callto:<argument> to call a number.
// Note that this works just on OS X (and probably only with
// Skype) but it shows the idea.
opts.Call = func(num string) {
	cmd := exec.Command("open", "callto:"+num)
	cmd.Start()
	cmd.Process.Release()
}

// Make some fake arguments to parse.
args := []string{
	"-vv",
	"--offset=5",
	"-n", "Me",
	"--animal", "dog", // anything other than "cat" or "dog" will raise an error
	"-p", "3",
	"-s", "hello",
	"-s", "world",
	"--ptrslice", "hello",
	"--ptrslice", "world",
	"--intmap", "a:1",
	"--intmap", "b:5",
	"arg1",
	"arg2",
	"arg3",
}

// Parse flags from `args'. Note that here we use flags.ParseArgs for
// the sake of making a working example. Normally, you would simply use
// flags.Parse(&opts) which uses os.Args
args, err := flags.ParseArgs(&opts, args)

if err != nil {
	panic(err)
}

fmt.Printf("Verbosity: %v\n", opts.Verbose)
fmt.Printf("Offset: %d\n", opts.Offset)
fmt.Printf("Name: %s\n", opts.Name)
fmt.Printf("Animal: %s\n", opts.Animal)
fmt.Printf("Ptr: %d\n", *opts.Ptr)
fmt.Printf("StringSlice: %v\n", opts.StringSlice)
fmt.Printf("PtrSlice: [%v %v]\n", *opts.PtrSlice[0], *opts.PtrSlice[1])
fmt.Printf("IntMap: [a:%v b:%v]\n", opts.IntMap["a"], opts.IntMap["b"])
fmt.Printf("Remaining args: %s\n", strings.Join(args, " "))

// Output: Verbosity: [true true]
// Offset: 5
// Name: Me
// Ptr: 3
// StringSlice: [hello world]
// PtrSlice: [hello world]
// IntMap: [a:1 b:5]
// Remaining args: arg1 arg2 arg3

More information can be found in the godocs: http://godoc.org/github.com/jessevdk/go-flags