A commandline parser in go. Single and multi-character option names with - and -- switches. Decent formatted Usage() output.
Go
Pull request Compare This branch is 1 commit behind jteeuwen:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README
optarg.go
optarg_test.go
string.go

README

 ================================================================================
 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
 - Allows multi character only named options, eg: --verbose without -v
 - 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() {
   	  switch opt.ShortName {
   	  case "h":
   	  	// ...
   	  case "v":
   	  	// ...
   	  }
   }

 - 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)
--------------------------------------------------------------------------------

 - We can organize options into 'sections', by interspersing their definition
   with header titles.
   
      optarg.Header("General options")
      optarg.Add("h", "help", "Displays this help.", false)
      optarg.Add("v", "version", "Displays version information.", false)

      optarg.Header("Find synonyms")
      optarg.Add("l", "list", "List all synonyms for the supplied words.", false)
      optarg.Add("d", "database", "Name of the thesaurus database to use.", v.DictDB)
      optarg.Add("s", "server", "Address of the thesaurus database to use.", v.DictServer)

   Since all options are printed in Usage() in the same order they are defined,
   this allows us to split the definitions up into useful and clearly defined
   categories. Making it easy for the reader to see which options are related to
   eachother. The standard Header output format "\n[%s]", which can be modified by
   optargs.HeaderFmt. This output of the example above is as follows:

   [General options]
        --help, -h: Displays this help.
     --version, -v: Displays version information.

   [Find synonyms]
        --list, -l: List all synonyms for the supplied words.
    --database, -d: Name of the thesaurus database to use. (defaults to: wn)
      --server, -s: Address of the thesaurus database to use. (defaults to:
                    dict.org:2628)


 - 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"

================================================================================
 USAGE
================================================================================

 Refer to optarg_test.go for a usage example.