Documentation
¶
Index ¶
- Variables
- func AddArg[T any, U translators.Translater[T]](val *T, builder *ArgBuilder, longName string, opts *opts[T, U])
- func AddComputedArg[T any, U computers.Computer[T]](val *T, builder *ArgBuilder, computer U)
- func AddEnum[E enum.Value, EP enum.Pntr[E]](val *E, builder *ArgBuilder, longName string, ...)
- func AddFlag(val *bool, builder *ArgBuilder, longName string, ...)
- func AddFlagCntr[T mathBasic.Int | mathBasic.Uint](val *T, builder *ArgBuilder, longName string, ...)
- func AddListArg[T any, U translators.Translater[T], W widgets.BaseInterface[T]](val *[]T, builder *ArgBuilder, longName string, ...)
- func AddSelector[T any, U translators.Translater[T], W widgets.BaseInterface[T]](val *T, builder *ArgBuilder, longName string, ...)
- func NewFlag() flag
- func NewOpts[T any, U translators.Translater[T]]() *opts[T, U]
- func NewTokenType() tokenType
- type Arg
- type ArgBuilder
- type ArgType
- func (_ *ArgType) Eq(l *ArgType, r *ArgType) bool
- func (o *ArgType) FromString(s string) error
- func (_ *ArgType) Hash(other *ArgType) hash.Hash
- func (o ArgType) MarshalJSON() ([]byte, error)
- func (o ArgType) String() string
- func (o *ArgType) UnmarshalJSON(b []byte) error
- func (o ArgType) Valid() error
- func (_ *ArgType) Zero(other *ArgType)
- type ArgvIter
- type ComputedArg
- type Parser
Constants ¶
This section is empty.
Variables ¶
var ( InvalidArgType = errors.New("Invalid ArgType") ARG_TYPE []ArgType = []ArgType{ UnknownArgType, ValueArgType, MultiValueArgType, FlagArgType, MultiFlagArgType, } )
var ( ParserConfigErr = errors.New("An error occurred setting up the parser") ReservedShortNameErr = errors.New("Reserved short name used") ReservedLongNameErr = errors.New("Reserved long name used") DuplicateShortNameErr = errors.New("Duplicate short name") DuplicateLongNameErr = errors.New("Duplicate long name") LongNameToShortErr = errors.New("Long name must be more than one char") ParserCombinationErr = errors.New("Could not combine parsers") ParsingErr = errors.New("An error occurred parsing the supplied arguments") ExpectedArgumentErr = errors.New("Expected an argument (short or long)") ExpectedValueErr = errors.New("Expected a value") UnrecognizedShortArgErr = errors.New("Unrecognized short argument") UnrecognizedLongArgErr = errors.New("Unrecognized long argument") EndOfTokenStreamErr = errors.New("The end of the token stream was reached") ArgumentPassedMultipleTimesErr = errors.New("Argument was passed multiple times but was expected only once") ParserConfigFileErr = errors.New("An error occurred parsing a parser config file") ParserConfigFileSyntaxErr = errors.New("Syntax error") ArgumentTranslationErr = errors.New("An error occurred translating the supplied argument") MissingRequiredArgErr = errors.New("Required argument missing") ComputedArgumentErr = errors.New("An error occurred calculating a computed argument") // The error returned when the help menu is displayed, indicating that the // parsing the arguments did not end in a "true" error but also did not // completely finish. HelpErr = errors.New("Help flag specified. Stopping.") )
var ( InvalidFlag = errors.New("Invalid flag") FLAG []flag = []flag{ unknownFlag, shortSpaceFlag, shortEqualsFlag, longSpaceFlag, longEqualsFlag, configEqualsFileFlag, configSpaceFileFlag, } )
var ( InvalidTokenType = errors.New("Invalid tokenType") TOKEN_TYPE []tokenType = []tokenType{ unknownTokenType, shortFlagToken, longFlagToken, valueToken, } )
Functions ¶
func AddArg ¶
func AddArg[T any, U translators.Translater[T]]( val *T, builder *ArgBuilder, longName string, opts *opts[T, U], )
Appends an argument to the supplied builder without performing any validation of the argument or builder as a whole.
If opts is Nil then the opts will be populated with the default values from calling NewOpts.
func AddComputedArg ¶
func AddComputedArg[T any, U computers.Computer[T]]( val *T, builder *ArgBuilder, computer U, )
Appends a computed argument to the supplied builder without performing any validation of computation of the argument or builder as a whole.
func AddEnum ¶ added in v0.0.2
func AddEnum[E enum.Value, EP enum.Pntr[E]]( val *E, builder *ArgBuilder, longName string, opts *opts[E, translators.Enum[E, EP]], )
Appends a enum selector to the supplied builder without performing any validation of the argument or builder as a whole. Enum selector arguments accept a value that must map to a valid enum value of the supplied enum type.
func AddFlag ¶
func AddFlag( val *bool, builder *ArgBuilder, longName string, opts *opts[bool, translators.Flag], )
Appends a flag argument to the supplied builder without performing any validation of the argument or builder as a whole. Counter flags can only be supplied once.
The arg type of the opts struct will be set to FlagArgType.
If opts is Nil then the opts will be populated with the default values from calling NewOpts.
func AddFlagCntr ¶
func AddFlagCntr[T mathBasic.Int | mathBasic.Uint]( val *T, builder *ArgBuilder, longName string, opts *opts[T, *translators.FlagCntr[T]], )
Appends a flag counter argument to the supplied builder without performing any validation of the argument or builder as a whole. Counter flags can be supplied many times. The counter will represent the total number of times the flag was supplied.
The arg type of the opts struct will be set to MultiFlagArgType.
The translator value in the opts struct will be initialized to a zero-valued [translator.FlagCntr].
If opts is Nil then the opts will be populated with the default values from calling NewOpts.
func AddListArg ¶
func AddListArg[T any, U translators.Translater[T], W widgets.BaseInterface[T]]( val *[]T, builder *ArgBuilder, longName string, opts *opts[[]T, *translators.ListValues[T, U, W]], )
Appends a list argument to the supplied builder without performing any validation of the argument or builder as a whole. List arguments accept many values for a single flag and will return a slice of all the translated values.
The arg type of the opts struct will be set to MultiFlagArgType.
If opts is Nil then the opts will be populated with the default values from calling NewOpts.
func AddSelector ¶
func AddSelector[T any, U translators.Translater[T], W widgets.BaseInterface[T]]( val *T, builder *ArgBuilder, longName string, opts *opts[T, translators.Selector[T, U, W]], )
Appends a selector argument to the supplied builder without performing any validation of the argument or builder as a whole. Selector arguments accept one value that must be one of a predefined set of values.
The arg type of the opts struct will be set to ValueArgType.
If opts is Nil then the opts will be populated with the default values from calling NewOpts.
func NewOpts ¶
func NewOpts[T any, U translators.Translater[T]]() *opts[T, U]
Returns a new opts struct initialized with the default values.
func NewTokenType ¶
func NewTokenType() tokenType
Types ¶
type Arg ¶
type Arg struct {
// contains filtered or unexported fields
}
Represents a single argument from the cmd line interface and all the options associated with it.
type ArgBuilder ¶
type ArgBuilder struct {
// contains filtered or unexported fields
}
Used to create a list of arguments that are then used to build the parser.
func (*ArgBuilder) ToParser ¶
func (b *ArgBuilder) ToParser(progName string, progDesc string) (Parser, error)
Creates a parser using the arg builder. Note that the arg builder will be modified and should not be used again after calling ToParser. The previously added arguments will be validated. Validation can return one of the below errors, all warpped in a top level ParserConfigErr:
type ArgType ¶
type ArgType int
const ( //gen:enum string UnknownArgType UnknownArgType ArgType = iota // Represents a flag type that must accept a value as an argument and must // only be supplied once. //gen:enum string ValueArgType ValueArgType // Represents a flag type that can accept many values as an argument and // must only be supplied once. At least one argument must be supplied. //gen:enum string MultiValueArgType MultiValueArgType // Represents a flag type that must not accept a value and must only be // supplied once. //gen:enum string FlagArgType FlagArgType // Represents a flag type that must not accept a value and may be supplied // many times. //gen:enum string MultiFlagArgType MultiFlagArgType )
func NewArgType ¶
func NewArgType() ArgType
func (*ArgType) Eq ¶
Returns true if l equals r. Uses the Eq operator provided by the widgets.BuiltinInt widget internally.
func (*ArgType) FromString ¶
func (*ArgType) Hash ¶
Returns a hash to represent other. The hash that is returned will be supplied by the widgets.BuiltinInt widget internally.
func (ArgType) MarshalJSON ¶
func (*ArgType) UnmarshalJSON ¶
type ArgvIter ¶
Represents an sequence of strings that can be translated into a sequence of tokens.
func ArgvIterFromSlice ¶
type ComputedArg ¶
type ComputedArg struct {
// contains filtered or unexported fields
}
Represents an argument value that is computed from other arguments rather than being supplied on the cmd line interface.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
The type that will take a token stream generated from a sequence of strings and perform all translating and computing tasks. See the [examples] package and README.md for more in depth information.
func NewHelpParser ¶
func NewHelpParser() *Parser
Creates a parser that will display the help menu when either -h or --help are supplied.
func NewVerbosityParser ¶
Creates a parser that has -v and --verbose flags. These flags can be supplied many times and the total count of the number of times the argument was supplied will be placed in val.
func (*Parser) AddSubParsers ¶
Adds sub-parsers to the current parser. All arguments are placed in a global namespace and must be unique. No long or short names can collide. All computed args are added to a tree like data structure, which is used to maintain the desired bottom up order of execution for computed arguments.
func (*Parser) Parse ¶
Parses the token stream given to it. This is a two step process. The steps are as follows:
- Consume the tokens and translate all received values, saving the results
to the desired locations.
- Compute all computed arguments in a bottom-up, left-right fashion.
If an error occurs in this process it will be returned wrapped in a top level ParsingErr. The only exception to this will be the HelpErr, which will stop all further parsing, print the help menu, and return. Any tokens that were present before the help flag will be translated.