options

package module
v0.0.0-...-4ef8b56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2014 License: MIT Imports: 4 Imported by: 0

README

Options Build Status

Read the documentation here.

Documentation

Overview

Self documenting CLI options parser for Go

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL