import "github.com/fd/options"
Self documenting CLI options parser for Go
type Options struct {
Command string
Args []string
// contains filtered or unexported fields
}
func (opts *Options) Get(option string) string
func (opts *Options) GetBool(option string) bool
func (opts *Options) GetInt(option string) int
type Spec struct {
// contains filtered or unexported fields
}
func MustParse(spec_string string) *Spec
MustParse() is a wrapper for Parse() for assigning global variables. When an error occures this function will panic.
func Parse(desc string) (spec *Spec, err error)
Parse a spec string and return a Spec object.
The spec string must have the following format:
usage: example-tool
A short description of the command
--
flag --flag,-f,FLAG A description for this flag
option= --option=,-o=,OPTION= A description for this option
the description continues here
!required= --required,-r=,REQUIRED= A required option
--
env_var= ENV_VAR= An environment variable
--
help help,h Show this help message
run run Run some function
--
More freestyle text
Code:
spec, err := Parse(`
usage: example-tool
A short description of the command
--
flag --flag,-f,FLAG A description for this flag
option= --option=,-o=,OPTION= A description for this option
the description continues here
!required= --required,-r=,REQUIRED= A required option
--
env_var= ENV_VAR= An environment variable
--
help help,h Show this help message
run run Run some function
--
More freestyle text
`)
if err != nil {
spec.PrintUsageWithError(err)
}
opts, err := spec.Interpret([]string{"example-tool", "--required", "hello world"}, []string{})
if err != nil {
spec.PrintUsageWithError(err)
}
fmt.Printf("required: %s", opts.Get("required"))
Output:
required: hello world
func (spec *Spec) Interpret(args []string, environ []string) (o *Options, err error)
func (this *Spec) MustInterpret(args []string, environ []string) *Options
func (spec *Spec) PrintUsage()
func (spec *Spec) PrintUsageAndExit()
func (spec *Spec) PrintUsageWithError(err error)