argparser

package
v0.40.4 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package argparser provides command line argument parsing for implementing commands that behave like the standard unix shell.

Index

Constants

This section is empty.

Variables

View Source
var ErrHelp = errors.New("Help")

Functions

This section is empty.

Types

type ArgParseResults

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

func (*ArgParseResults) AnyFlagsEqualTo

func (res *ArgParseResults) AnyFlagsEqualTo(val bool) *set.StrSet

func (*ArgParseResults) Arg

func (res *ArgParseResults) Arg(idx int) string

func (*ArgParseResults) Contains

func (res *ArgParseResults) Contains(name string) bool

func (*ArgParseResults) ContainsAll

func (res *ArgParseResults) ContainsAll(names ...string) bool

func (*ArgParseResults) ContainsAny

func (res *ArgParseResults) ContainsAny(names ...string) bool

func (*ArgParseResults) ContainsArg

func (res *ArgParseResults) ContainsArg(name string) bool

func (*ArgParseResults) ContainsMany

func (res *ArgParseResults) ContainsMany(names ...string) []string

func (*ArgParseResults) Equals

func (res *ArgParseResults) Equals(other *ArgParseResults) bool

func (*ArgParseResults) FlagsEqualTo

func (res *ArgParseResults) FlagsEqualTo(names []string, val bool) *set.StrSet

func (*ArgParseResults) GetInt

func (res *ArgParseResults) GetInt(name string) (int, bool)

func (*ArgParseResults) GetIntOrDefault

func (res *ArgParseResults) GetIntOrDefault(name string, defVal int) int

func (*ArgParseResults) GetUint

func (res *ArgParseResults) GetUint(name string) (uint64, bool)

func (*ArgParseResults) GetValue

func (res *ArgParseResults) GetValue(name string) (string, bool)

func (*ArgParseResults) GetValueOrDefault

func (res *ArgParseResults) GetValueOrDefault(name, defVal string) string

func (*ArgParseResults) GetValues

func (res *ArgParseResults) GetValues(names ...string) map[string]string

func (*ArgParseResults) MustGetValue

func (res *ArgParseResults) MustGetValue(name string) string

func (*ArgParseResults) NArg

func (res *ArgParseResults) NArg() int

type ArgParser

type ArgParser struct {
	Supported         []*Option
	NameOrAbbrevToOpt map[string]*Option
	ArgListHelp       [][2]string
}

func NewArgParser

func NewArgParser() *ArgParser

func (*ArgParser) Parse

func (ap *ArgParser) Parse(args []string) (*ArgParseResults, error)

Parses the string args given using the configuration previously specified with calls to the various Supports* methods. Any unrecognized arguments or incorrect types will result in an appropriate error being returned. If the universal --help or -h flag is found, an ErrHelp error is returned.

func (*ArgParser) SupportOption

func (ap *ArgParser) SupportOption(opt *Option)

Adds support for a new argument with the option given. Options must have a unique name and abbreviated name.

func (*ArgParser) SupportsFlag

func (ap *ArgParser) SupportsFlag(name, abbrev, desc string) *ArgParser

Adds support for a new flag (argument with no value). See SupportOpt for details on params.

func (*ArgParser) SupportsInt

func (ap *ArgParser) SupportsInt(name, abbrev, valDesc, desc string) *ArgParser

Adds support for a new int argument with the description given. See SupportOpt for details on params.

func (*ArgParser) SupportsString

func (ap *ArgParser) SupportsString(name, abbrev, valDesc, desc string) *ArgParser

Adds support for a new string argument with the description given. See SupportOpt for details on params.

func (*ArgParser) SupportsUint

func (ap *ArgParser) SupportsUint(name, abbrev, valDesc, desc string) *ArgParser

Adds support for a new uint argument with the description given. See SupportOpt for details on params.

func (*ArgParser) SupportsValidatedString

func (ap *ArgParser) SupportsValidatedString(name, abbrev, valDesc, desc string, validator ValidationFunc) *ArgParser

type Option

type Option struct {
	// Long name for this Option, specified on the command line with --Name. Required.
	Name string
	// Abbreviated name for this Option, specified on the command line with -Abbrev. Optional.
	Abbrev string
	// Brief description of the Option.
	ValDesc string
	// The type of this option, either a flag or a value.
	OptType OptionType
	// Longer help text for the option.
	Desc string
	// Function to validate an Option after parsing, returning any error.
	Validator ValidationFunc
}

An Option encapsulates all the information necessary to represent and parse a command line argument.

type OptionType

type OptionType int
const (
	OptionalFlag OptionType = iota
	OptionalValue
)

type UnknownArgumentParam

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

func (UnknownArgumentParam) Error

func (unkn UnknownArgumentParam) Error() string

type ValidationFunc

type ValidationFunc func(string) error

func ValidatorFromStrList

func ValidatorFromStrList(paramName string, validStrList []string) ValidationFunc

Jump to

Keyboard shortcuts

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