options: github.com/fd/options Index | Examples | Files

package options

import "github.com/fd/options"

Self documenting CLI options parser for Go

Index

Examples

Package Files

options.go

type Options

type Options struct {
    Command string
    Args    []string
    // contains filtered or unexported fields
}

func (*Options) Get

func (opts *Options) Get(option string) string

func (*Options) GetBool

func (opts *Options) GetBool(option string) bool

func (*Options) GetInt

func (opts *Options) GetInt(option string) int

type Spec

type Spec struct {
    // contains filtered or unexported fields
}

func MustParse

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

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

func (spec *Spec) Interpret(args []string, environ []string) (o *Options, err error)

func (*Spec) MustInterpret

func (this *Spec) MustInterpret(args []string, environ []string) *Options

func (*Spec) PrintUsage

func (spec *Spec) PrintUsage()

func (*Spec) PrintUsageAndExit

func (spec *Spec) PrintUsageAndExit()

func (*Spec) PrintUsageWithError

func (spec *Spec) PrintUsageWithError(err error)

Package options imports 4 packages (graph) and is imported by 2 packages. Updated 2013-03-21. Refresh now. Tools for package owners.