Home > go-pkg-optarg

go-pkg-optarg

Go-pkg-optarg is a project mainly written in ..., based on the Zlib license.

A commandline parser in go. Single and multi-character option names with - and -- switches. Decent formatted Usage() output.

OPTARG - A simple commandline options parser.

  • Allows options with single and multi character names according to the traditional unix way of doing things. eg: -o versus --option

  • Exposes a channel based iterator which returns parsed options from the optarg.Parse() function. Note that it only yields Options which are actually present in the commandline arguments (os.Args). Call as a for loop:

    for opt := range optarg.Parse() { // ... parse option }

  • Standard switch tokens are - and --. Can be modified by changing the vars optarg.ShortSwitch and optarg.LongSwitch.

  • Any arguments not associated with an option will be available in the optarg.Remainder slice after optarg.Parse() has been run.

  • Boolean flags require no value. The precense or absence of the flag is the value by itself. eg: flag '-n' is false if it's not found in os.Args, true if it is.

  • Exposes a Usage() function which prints options with their description and default values to the standard output. As opposed to the flag package, this outputs neatly formatted text. It prints output like the following. Note that this is the Usage() output of the options listed in optarg_test.go. It uses my own sexy multilineWrap() routine (see string.go):


Usage: ./6.out [options]:

--source, -s: Path to the source folder. Here is some added description information which is completely useless, but it makes sure we can pimp our sexy Usage() output when dealing with lenghty, multi -line description texts. --bin, -b: Path to the binary folder. --arch, -a: Target architecture. (defaults to: amd64) --noproc, -n: Skip pre/post processing. (defaults to: false) --purge, -p: Clean compiled packages after linking is complete. (defaults to: false)

  • Specify arguments to options using whitespace.

    This is not accepted: -f=bar -fBar

    This is accepted: -f bar -f "bar"

  • We support chaining shortform options together like this: -mnopq This considers each letter as a separate option. The last one can even have an argument if it needs one: -mnopq "foo"