commander

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package commander contains a simple command-line argument parser that supports flags, switches and free-form trailing (and mid-stream) arguments, and provides methods for their retrieval.

Commander provides similar functionality to the flags package, but also supports 'schemaless' argument parsing, where the structure of arguments might not be known up front. It supports user-defined, key-value style arguments — for example, as 'myapp -key1=value1 --key2 value2', where key1 and key2 could be anything.

Index

Constants

View Source
const FreeForm = "__free_form"

FreeForm is a key in PartsMap for free-form (trailing) values.

Variables

This section is empty.

Functions

This section is empty.

Types

type Part

type Part struct {
	Name, Value string
}

Part is a tuple of parsed arguments.

For named arguments — for example, the argument pair '-key=value' or '-key value', the Name field is 'key' (i.e. without the leading dash or pair of dashes) and the Value field is simply 'value'. For free-form (a.k.a. trailing) arguments — for example, 'file1.txt', the Name field is a blank string and the Value field contains the argument.

func (Part) IsFreeForm

func (p Part) IsFreeForm() bool

IsFreeForm return true if this is a free-form part.

func (Part) String

func (p Part) String() string

String returns a textual representation of the part.

type Parts

type Parts []Part

Parts is a slice of Part tuples.

func Parse

func Parse(cmdArgs []string) Parts

Parse processes the given cmdArgs into a Parts slice. No error is returned as parsing is schemaless; the parser extracts all flags, switches and free-form values that may be present.

func (Parts) Mappify

func (parts Parts) Mappify() PartsMap

Mappify transforms the parsed Parts into a PartsMap for convenient retrieval of argument values.

type PartsMap

type PartsMap map[string][]string

PartsMap keys unique part names to the values. For free-form values, the key is FreeForm.

func (PartsMap) Value

func (pm PartsMap) Value(name string, def string) (string, error)

Value obtains a single value for the given name, returning the default value if none exist. If the map contains two or more values for the given name, the first value is returned as well as an error.

Jump to

Keyboard shortcuts

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