Command Line Option Parser

Last May, after I finished my config package, I ended up with a nice decoder and a struct traversal routine that I knew would work perfectly for a command line option parser. I was not satisfied using the flag package in the standard library, and I really disliked my first attempt at a command line option parser. So I wrote a new command line parser that is more simple to use than the standard flag package, and I would say a bit more usable.

The following example defines a simple option struct and supplies option.New with the address of the struct variable. Command line flags and keywords are automatically determined from the field names and data types:

var opt struct {
  Answer   int
  Ask      string
  Babel    bool
}
obj,err := option.New(&opt)

Either one of the command line options shown below would populate the struct with data. Notice that the gnu-style keywords are the names of the fields. Also, the second instance of a field with a duplicate first character will require an upper-case option key:

mycommand -b -a 42 -A "What is the answer to the ultimate question?"
mycommand --answer=42 --babel --ask="What kind of a question is that?"

 

Arguments

To read command line arguments, simply define an argument slice and supply it as the second argument for the option.New method.

var opt struct {
  Answer int    `Provide your answer`
  Ask    string `k:Ask any question`
  Babel  bool   `translate:Enable the babel-fish translator`
}
var args []string
obj,err := option.New(&opt,&args)

 

Help

Providing help for the user is simple as most of the information has already been prepared for you:

// Display help text
obj.Help()

Output:

SYNOPSIS
    mycommand [OPTIONS] [string]...

OPTIONS
    -a int, --answer=int

    -A string, --ask=string

    -b, --babel

 

Additional Help

Using tags in your struct will provide additional help text and allows you to override the automatic key names. Calling the Section method will insert additional headings and paragraphs into the help text. NAME, DESCRIPTION and OPTION sections are special as they will be automatically inserted at the appropriate places. Other sections will be placed following the options section.

var opt struct {
  Answer int    `Provide your answer`
  Ask    string `k:Ask any question`
  Babel  bool   `translate:Enable the babel-fish translator`
}
var args []string 
obj,err := option.New(&opt,&args)
Obj.Section("NAME", "Deep Thought")
Obj.Section("OPTIONS", "Optional univers parameters providing "+
    "Deep Thought transformative disposition balls.")
Obj.Section("DESCRIPTION", "Lorem Ipsum Hitchhiker invited spontaneous "+
    "universe hallucinations generating synthesized improbability.")
Obj.Section("COPYRIGHT", "Copyright (c) 2018 Mark K Mueller, markmueller.com\n"+
    "Use of this source code is governed by an MIT-style\n"+
    "license that can be found in the LICENSE file.")
obj.Help()

Output:

NAME
    Deep Thought

SYNOPSIS
    mycommand [OPTIONS] [string]...

DESCRIPTION
    Lorem Ipsum Hitchhiker invited spontaneous universe hallucinations
    generating synthesized improbability.

OPTIONS
    Optional universe parameters providing Deep Thought transformative
    disposition balls.

    -a int, --answer=int Provide your answer

    -k string            Ask any question

    --translate          Enable the babel-fish translator

COPYRIGHT
    Copyright (c) 2018 Mark K Mueller, markmueller.com
    Use of this source code is governed by an MIT-style
    license that can be found in the LICENSE file.

 

 

More documentation may be found at GoDoc: godoc.org/github.com/mkmueller/option

The repository for this Go package is at Github: github.com/mkmueller/option

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *