flags

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 8 Imported by: 0

README

flags

Command line parser for Go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Main = NewProgram()

Main is the main program.

Functions

func Add

func Add(name, desc string, cmd Command)

Add a command to the main program.

func Args

func Args() (*Positional, *Optional)

Args creates a pair of empty positional and optional argument definitions.

func Help

func Help(pos *Positional, opt *Optional) string

Help creaes a help string for the given argument definitions.

func ListCommands

func ListCommands(prog Program) string

ListCommands lists the commands registered to the given program.

func Run

func Run(name, desc string, cmd Command) int

Run the given command using os.Args.

func Usage

func Usage(pos *Positional, opt *Optional) string

Usage creates a usage string for the given argument definitions.

Types

type Argument

type Argument struct {
	Value Value
	Usage string
}

Argument represents a value-usages pair.

type ArgumentType

type ArgumentType int

ArgumentType represents the type of argument.

const (
	// LongType represents a long flag argument.
	LongType ArgumentType = iota

	// ShortType represents a short flag argument.
	ShortType

	// ValueType represents a plain value argument.
	ValueType
)

func TypeOf

func TypeOf(s string) ArgumentType

TypeOf returns the type of the given argument.

type Arguments

type Arguments map[string]Argument

Arguments is a map of names and arguments.

func (*Arguments) Has

func (args *Arguments) Has(name string) bool

Has tests if the given name is registered as an argument.

type BoolValue

type BoolValue bool

BoolValue represents a boolean argument value.

func NewBoolValue

func NewBoolValue(init bool) *BoolValue

NewBoolValue creates a new BoolValue.

func (*BoolValue) Set

func (p *BoolValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (BoolValue) String

func (p BoolValue) String() string

String satisfies the fmt.Stringer interface.

type Command

type Command func(*Context) error

Command represents a executable command.

func Compile

func Compile() Command

Compile the main program.

type CommandDescription

type CommandDescription struct {
	Desc string
	Cmd  Command
}

CommandDescription carries a command and its description.

type Context

type Context struct {
	Name string
	Desc string
	Args []string
}

Context carries the name, description, and arguments given to a command.

func (*Context) Parse

func (ctx *Context) Parse(pos *Positional, opt *Optional) error

Parse the context arguments using the positional and optional argument definitions given.

type CreateValue

type CreateValue os.File

CreateValue represents a file argument value for creating.

func NewCreateValue

func NewCreateValue(init *os.File) *CreateValue

NewCreateValue creates a new CreateValue.

func (*CreateValue) Set

func (p *CreateValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (*CreateValue) String

func (p *CreateValue) String() string

String satisfies the fmt.Stringer interface.

type FloatValue

type FloatValue float64

FloatValue represents a float argument value.

func NewFloatValue

func NewFloatValue(init float64) *FloatValue

NewFloatValue creates a new FloatValue.

func (*FloatValue) Set

func (p *FloatValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (FloatValue) String

func (p FloatValue) String() string

String satisfies the fmt.Stringer interface.

type IntValue

type IntValue int

IntValue represents a integer argument value.

func NewIntValue

func NewIntValue(init int) *IntValue

NewIntValue creates a new IntValue.

func (*IntValue) Set

func (p *IntValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (IntValue) String

func (p IntValue) String() string

String satisfies the fmt.Stringer interface.

type OpenSliceValue added in v1.1.0

type OpenSliceValue []*os.File

OpenSliceValue represents a variable number open argument value.

func NewOpenSliceValue added in v1.1.0

func NewOpenSliceValue(init []*os.File) *OpenSliceValue

NewOpenSliceValue creates a new OpenSliceValue.

func (OpenSliceValue) Len added in v1.1.0

func (v OpenSliceValue) Len() int

Len will return the length of the slice value.

func (*OpenSliceValue) Set added in v1.1.0

func (p *OpenSliceValue) Set(s string) error

Set will set attempt to convert and append the given string to the slice.

func (OpenSliceValue) String added in v1.1.0

func (v OpenSliceValue) String() string

String satisfies the fmt.Stringer interface.

type OpenValue

type OpenValue os.File

OpenValue represents a file argument value for opening.

func NewOpenValue

func NewOpenValue(init *os.File) *OpenValue

NewOpenValue creates a new OpenValue.

func (*OpenValue) Set

func (p *OpenValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (*OpenValue) String

func (p *OpenValue) String() string

String satisfies the fmt.Stringer interface.

type Optional

type Optional struct {
	Args  Arguments
	Alias map[rune]string
}

Optional represents the optional command line arguments.

func (*Optional) Create added in v1.2.0

func (opt *Optional) Create(short rune, long string, init *os.File, usage string) *os.File

Create adds a file for writing to the positional argument list.

func (*Optional) Float

func (opt *Optional) Float(short rune, long string, init float64, usage string) *float64

Float adds an float flag to the optional argument list.

func (*Optional) Int

func (opt *Optional) Int(short rune, long string, init int, usage string) *int

Int adds an integer flag to the optional argument list.

func (*Optional) Open added in v1.2.0

func (opt *Optional) Open(short rune, long string, init *os.File, usage string) *os.File

Open adds a file for reading to the optional argument list.

func (*Optional) OpenSlice added in v1.1.0

func (opt *Optional) OpenSlice(short rune, long string, init []*os.File, usage string) *[]*os.File

OpenSlice adds a string slice flag to the optional argument list.

func (*Optional) Register

func (opt *Optional) Register(short rune, long string, value Value, usage string)

Optional represents the optional command line arguments.

func (*Optional) String

func (opt *Optional) String(short rune, long, init, usage string) *string

String adds a string flag to the optional argument list.

func (*Optional) StringSlice

func (opt *Optional) StringSlice(short rune, long string, init []string, usage string) *[]string

StringSlice adds a string slice flag to the optional argument list.

func (*Optional) Switch

func (opt *Optional) Switch(short rune, long string, usage string) *bool

Switch adds a command line switch to the optional argument list.

type Parser

type Parser struct {
	Pos *Positional
	Opt *Optional
}

Parser will parse a list of arguments with the given Positional and Optional argument definitions.

func NewParser

func NewParser(pos *Positional, opt *Optional) Parser

NewParser returns a new Parser.

func (Parser) Parse

func (parser Parser) Parse(args []string) error

Parse the given arguments using the argument definitions.

type Positional

type Positional struct {
	Order []string
	Args  Arguments
	In    *Argument
	Out   *Argument
}

Positional represents the positional command line arguments.

func (*Positional) Bool

func (pos *Positional) Bool(name, usage string) *bool

Bool adds a string value to the positional argument list.

func (*Positional) Create

func (pos *Positional) Create(name, usage string) *os.File

Create adds a file for writing to the positional argument list.

func (*Positional) Input

func (pos *Positional) Input(usage string) *os.File

Input adds a file which when omitted will read from os.Stdin.

func (*Positional) Int

func (pos *Positional) Int(name, usage string) *int

Int adds a string value to the positional argument list.

func (Positional) Len

func (pos Positional) Len() int

Len returns the number of positional arguments.

func (*Positional) Open

func (pos *Positional) Open(name, usage string) *os.File

Open adds a file for reading to the positional argument list.

func (*Positional) Output

func (pos *Positional) Output(usage string) *os.File

Output adds a file which when omitted will read from os.Stdout.

func (*Positional) Register

func (pos *Positional) Register(name string, value Value, usage string)

Regsiter the name with the given value and usage.

func (*Positional) String

func (pos *Positional) String(name, usage string) *string

String adds a string value to the positional argument list.

type Program

type Program struct {
	Map map[string]CommandDescription
}

Program represents a list of named commands.

func NewProgram

func NewProgram() *Program

NewProgram creates a new Program.

func (*Program) Add

func (prog *Program) Add(name, desc string, cmd Command)

Add a Command with the given name and description.

func (Program) Compile

func (prog Program) Compile() Command

Compile the subcommands into a single command.

type SliceValue

type SliceValue interface {
	Value
	Len() int
}

SliceValue represents a variable length command line argument value.

type StringSliceValue

type StringSliceValue []string

StringSliceValue represents a variable number string argument value.

func NewStringSliceValue

func NewStringSliceValue(init []string) *StringSliceValue

NewStringSliceValue creates a new StringSliceValue.

func (StringSliceValue) Len

func (v StringSliceValue) Len() int

Len will return the length of the slice value.

func (*StringSliceValue) Set

func (p *StringSliceValue) Set(s string) error

Set will set attempt to convert and append the given string to the slice.

func (StringSliceValue) String

func (p StringSliceValue) String() string

String satisfies the fmt.Stringer interface.

type StringValue

type StringValue string

StringValue represents a string argument value.

func NewStringValue

func NewStringValue(init string) *StringValue

NewStringValue creates a new StringValue.

func (*StringValue) Set

func (p *StringValue) Set(s string) error

Set will set attempt to convert the given string to a value.

func (StringValue) String

func (p StringValue) String() string

String satisfies the fmt.Stringer interface.

type Value

type Value interface {
	Set(value string) error
	String() string
}

Value represents a command line argument value.

Jump to

Keyboard shortcuts

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