Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultHelpFormatter(item *HelpItem, formatting *HelpFormatting) string
- func Help[T any](target *T, formatter FormatterFn) (string, error)
- func HelpDefault[T any](target *T) (string, error)
- func Parse[T any](target *T, rawArgs ...string) (trailing []string, err error)
- func StringReflect(field reflect.StructField, fieldValue reflect.Value, values []string) (int, error)
- func UsageHelp(tags ParsedTags) (string, bool)
- func ValueFromString(fieldType reflect.Type, inputs []string) (*reflect.Value, int, error)
- type ArgsParser
- func (pa *ArgsParser) Add(arg string) error
- func (pa *ArgsParser) Parse(args []string) (*ArgsParser, error)
- func (pa *ArgsParser) PopTrailing(args ...string) *ArgsParser
- func (pa *ArgsParser) ValuesEqualWith(other *ArgsParser) bool
- func (pa *ArgsParser) With(modifier func(args *ArgsParser)) *ArgsParser
- type ArgsState
- type CommandRequiredError
- type FormatterFn
- type HelpFormatting
- type HelpItem
- type MandatoryParameterError
- type ParseError
- type ParsedTags
- type StructFieldProcessor
- type Tag
- type TagType
- type UnexpectedInputFormatError
- type UnknownTagTypeError
- type UnsupportedReflectTypeError
Constants ¶
const (
TagName = "clapper"
)
Variables ¶
var ( ErrNoStruct = errors.New("target is not a struct") ErrEmptyArgument = errors.New("empty argument") ErrUnexpectedValue = errors.New("expected flag not value") ErrFieldCanNotBeSet = errors.New("field can't be set") ErrNoFlagSpecifier = errors.New("no flag specified for struct field") ErrShortOverrideCanOnlyBeOneLetter = errors.New("short override can only be one letter") ErrLongMustBeMoreThanOne = errors.New("long name must be more than one character") ErrCommandCanNotHaveValue = errors.New("command can't have a value") ErrDuplicateCommandTag = errors.New("duplicate command tag found") ErrNoDefaultValue = errors.New("default spcified but no default value given") )
Functions ¶
func DefaultHelpFormatter ¶
func DefaultHelpFormatter(item *HelpItem, formatting *HelpFormatting) string
func HelpDefault ¶
func Parse ¶
Parse tries to evaluate the given `rawArgs` towards the provided struct `target` (which must include `clapper`-Tags). If no `rawArgs` were provided, it defaults to `os.Args[1:]` (all command line arguments without the programm name).
func StringReflect ¶
func UsageHelp ¶ added in v1.1.0
func UsageHelp(tags ParsedTags) (string, bool)
Types ¶
type ArgsParser ¶
type ArgsParser struct {
Params map[string][]string
Trailing []string
// contains filtered or unexported fields
}
func NewArgsParser ¶
func NewArgsParser() *ArgsParser
func (*ArgsParser) Add ¶
func (pa *ArgsParser) Add(arg string) error
func (*ArgsParser) Parse ¶
func (pa *ArgsParser) Parse(args []string) (*ArgsParser, error)
func (*ArgsParser) PopTrailing ¶
func (pa *ArgsParser) PopTrailing(args ...string) *ArgsParser
func (*ArgsParser) ValuesEqualWith ¶
func (pa *ArgsParser) ValuesEqualWith(other *ArgsParser) bool
func (*ArgsParser) With ¶
func (pa *ArgsParser) With(modifier func(args *ArgsParser)) *ArgsParser
type CommandRequiredError ¶ added in v1.1.0
type CommandRequiredError struct {
// contains filtered or unexported fields
}
CommandRequiredError will be thrown when a command tag is required but no command is provided. A `help` given in the tag will enrich the error message with the given help message.
func NewCommandRequiredError ¶ added in v1.1.0
func NewCommandRequiredError(help string) CommandRequiredError
func (CommandRequiredError) Error ¶ added in v1.1.0
func (e CommandRequiredError) Error() string
type FormatterFn ¶
type FormatterFn = func(item *HelpItem, formatting *HelpFormatting) string
type HelpFormatting ¶
func DefaultHelpFormatting ¶
func DefaultHelpFormatting() *HelpFormatting
func (*HelpFormatting) Update ¶
func (h *HelpFormatting) Update(item *HelpItem) *HelpFormatting
type HelpItem ¶
func HelpItemFromTags ¶
HelpItemFromTags creates a HelpItem from the given tags or retruns nil if the tags represent an informational tag line only.
func (*HelpItem) Display ¶
func (h *HelpItem) Display(formatting HelpFormatting) string
type MandatoryParameterError ¶
type MandatoryParameterError struct {
Name string
}
MandatoryParameterError will be thrown when a mandatory parameter is missing and no default value is provided.
func NewMandatoryParameterError ¶
func NewMandatoryParameterError(name string) MandatoryParameterError
func (MandatoryParameterError) Error ¶
func (e MandatoryParameterError) Error() string
type ParseError ¶
type ParseError struct {
Index int
Name string
TagLine string
// contains filtered or unexported fields
}
ParseError will be thrown when an error occurs during parsing.
func NewParseError ¶
func NewParseError(from error, index int, name string, tagLine string) ParseError
func (ParseError) Error ¶
func (e ParseError) Error() string
func (ParseError) Underlying ¶
func (e ParseError) Underlying() error
type ParsedTags ¶ added in v1.1.0
type StructFieldProcessor ¶ added in v1.1.0
type StructFieldProcessor struct {
// contains filtered or unexported fields
}
func NewStructFieldProcessor ¶ added in v1.1.0
func NewStructFieldProcessor(target reflect.Type, value reflect.Value, tags ParsedTags, args *ArgsParser) *StructFieldProcessor
func (*StructFieldProcessor) EOF ¶ added in v1.1.0
func (f *StructFieldProcessor) EOF() bool
func (*StructFieldProcessor) Finalize ¶ added in v1.1.0
func (f *StructFieldProcessor) Finalize() error
func (*StructFieldProcessor) GetTrailing ¶ added in v1.1.0
func (f *StructFieldProcessor) GetTrailing() []string
func (*StructFieldProcessor) HasCommand ¶ added in v1.1.0
func (f *StructFieldProcessor) HasCommand() bool
func (*StructFieldProcessor) Next ¶ added in v1.1.0
func (f *StructFieldProcessor) Next() error
func (*StructFieldProcessor) ProcessCommand ¶ added in v1.1.0
func (f *StructFieldProcessor) ProcessCommand() error
type Tag ¶
type Tag struct {
// Type of the tag.
Type TagType
// Name gets derived from the struct field name if the tag is Short or Long and is pure computational.
Name string
// Value is an optional value given to the tag if an assignment operator is given. `short=s`
Value string
// Index of the tag found in the tag line.
Index int
}
Tag is one property of a struct tag.
func (*Tag) DeriveName ¶
type UnexpectedInputFormatError ¶ added in v1.1.0
UnsupportedReflectTypeError will be thrown when a struct field has a type that can not be set with the provided value. For example givving a string to a field of type int.
func NewUnexpectedInputFormatError ¶ added in v1.1.0
func NewUnexpectedInputFormatError(input string, expected reflect.Type) UnexpectedInputFormatError
func (UnexpectedInputFormatError) Error ¶ added in v1.1.0
func (e UnexpectedInputFormatError) Error() string
type UnknownTagTypeError ¶ added in v1.1.0
type UnknownTagTypeError struct {
TagName string
}
UnsupportedReflectTypeError will be thrown when a struct tag given is unknown to claper.
func NewUnknownTagTypeError ¶ added in v1.1.0
func NewUnknownTagTypeError(tagName string) UnknownTagTypeError
func (UnknownTagTypeError) Error ¶ added in v1.1.0
func (e UnknownTagTypeError) Error() string
type UnsupportedReflectTypeError ¶
type UnsupportedReflectTypeError struct {
Type string
}
UnknownTagTypeError will be thrown when a struct field type is unsupported by claper.
func NewUnsupportedReflectTypeError ¶
func NewUnsupportedReflectTypeError(t string) UnsupportedReflectTypeError
func (UnsupportedReflectTypeError) Error ¶
func (e UnsupportedReflectTypeError) Error() string