Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

go command line option parser

branch: master

Merge pull request #70 from zimmski/subcommand-optional

subcommands-optional is an optional flag
latest commit cee4db96ae
Jesse van den Kieboom authored
Octocat-spinner-32 examples error strings should not be capitalized March 19, 2014
Octocat-spinner-32 LICENSE Added license August 31, 2012
Octocat-spinner-32 README.md correct grammar errors and typos March 12, 2014
Octocat-spinner-32 assert_test.go Return remaining args after error occurred January 10, 2014
Octocat-spinner-32 check_crosscompile.sh Added darwin and freebsd in checking cross compile March 06, 2013
Octocat-spinner-32 closest.go use increment instead of assignment operator March 19, 2014
Octocat-spinner-32 command.go rename SubCommand uniformly to Subcommand March 12, 2014
Octocat-spinner-32 command_private.go rename SubCommand uniformly to Subcommand March 12, 2014
Octocat-spinner-32 command_test.go Return remaining args after error occurred January 10, 2014
Octocat-spinner-32 convert.go Fix converting map to string December 19, 2013
Octocat-spinner-32 convert_test.go Added more tests December 19, 2013
Octocat-spinner-32 error.go Implement Stringer interface for ErrorType March 19, 2014
Octocat-spinner-32 example_test.go Remove import of "os" December 19, 2013
Octocat-spinner-32 flags.go subcommands-optional is an optional flag March 26, 2014
Octocat-spinner-32 group.go correct grammar errors and typos March 12, 2014
Octocat-spinner-32 group_private.go default values of maps must be copied March 17, 2014
Octocat-spinner-32 group_test.go Fix spelling error December 19, 2013
Octocat-spinner-32 help.go correct grammar errors and typos March 12, 2014
Octocat-spinner-32 help_test.go add more defaults for more precise testing March 14, 2014
Octocat-spinner-32 ini.go Implement ini option IniCommentDefaults March 14, 2014
Octocat-spinner-32 ini_private.go Fix comparing value with defaultValue for IniIncludeDefaults March 17, 2014
Octocat-spinner-32 ini_test.go Be more verbose on TestWriteIni errors March 17, 2014
Octocat-spinner-32 long_test.go Move tests into the flags package December 18, 2013
Octocat-spinner-32 man.go empty descriptions should not leave any output March 13, 2014
Octocat-spinner-32 marshal_test.go Added more tests December 19, 2013
Octocat-spinner-32 multitag.go Explicitly check for newline errors in tag values December 18, 2013
Octocat-spinner-32 option.go correct grammar errors and typos March 12, 2014
Octocat-spinner-32 option_private.go Remove unreachable code December 19, 2013
Octocat-spinner-32 options_test.go Move tests into the flags package December 18, 2013
Octocat-spinner-32 optstyle_other.go Do not show builtin help for all groups January 10, 2014
Octocat-spinner-32 optstyle_windows.go Do not show builtin help for all groups January 10, 2014
Octocat-spinner-32 parser.go correct grammar errors and typos March 12, 2014
Octocat-spinner-32 parser_private.go Added ErrUnknownCommand January 10, 2014
Octocat-spinner-32 pointer_test.go Move tests into the flags package December 18, 2013
Octocat-spinner-32 short_test.go Move tests into the flags package December 18, 2013
Octocat-spinner-32 tag_test.go Run go fmt December 19, 2013
Octocat-spinner-32 termsize.go Added solaris to systems not supporting sysioctl February 26, 2014
Octocat-spinner-32 termsize_linux.go Make TIOCGWINSZ a private const November 21, 2013
Octocat-spinner-32 termsize_nosysioctl.go Added solaris to systems not supporting sysioctl February 26, 2014
Octocat-spinner-32 termsize_other.go Make TIOCGWINSZ a private const November 21, 2013
Octocat-spinner-32 termsize_unix.go Make TIOCGWINSZ a private const November 21, 2013
Octocat-spinner-32 unknown_test.go Move tests into the flags package December 18, 2013
README.md

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

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

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 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",
    "-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)
    os.Exit(1)
}

fmt.Printf("Verbosity: %v\n", opts.Verbose)
fmt.Printf("Offset: %d\n", opts.Offset)
fmt.Printf("Name: %s\n", opts.Name)
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

Something went wrong with that request. Please try again.