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 SanitizeExplodeShorts(args []string) []string
- func SanitizeSplitAssignmets(args []string) []string
- func SanitizerSkipLeadingValues(args []string) []string
- 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 ArgParserExt
- type ArgType
- type ArgValue
- type ArgumentSanitizer
- type CommandRequiredError
- type FormatterFn
- type HelpFormatting
- type HelpItem
- type MandatoryParameterError
- type ParseError
- type ParsedTags
- type SanitizerFn
- type StructFieldProcessor
- type Tag
- type TagMap
- 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 SanitizeExplodeShorts ¶ added in v1.1.3
SanitizeExplodeShorts splits combined short flags into separate arguments (iE -abc -> -a -b -c).
func SanitizeSplitAssignmets ¶ added in v1.1.3
SanitizeSplitAssignmets splits an argument into its key and value if present (iE --foo=bar -> --foo bar).
func SanitizerSkipLeadingValues ¶ added in v1.1.3
SanitizerSkipLeadingValues removes prefixed values that can not be assigned to any argument.
func StringReflect ¶
func UsageHelp ¶ added in v1.1.0
func UsageHelp(tags ParsedTags) (string, bool)
Types ¶
type ArgParserExt ¶ added in v1.1.3
type ArgParserExt struct {
Args []ArgValue
}
func NewArgParserExt ¶ added in v1.1.3
func NewArgParserExt(args []string) *ArgParserExt
func (*ArgParserExt) Consume ¶ added in v1.1.3
func (ext *ArgParserExt) Consume(key string, argType ArgType, n int) *ArgParserExt
func (*ArgParserExt) ConsumeTrailing ¶ added in v1.1.3
func (ext *ArgParserExt) ConsumeTrailing(n int) *ArgParserExt
func (*ArgParserExt) Get ¶ added in v1.1.3
func (ext *ArgParserExt) Get(key string, argType ArgType) (values []string, ok bool)
func (*ArgParserExt) GetTrailing ¶ added in v1.1.3
func (ext *ArgParserExt) GetTrailing() []string
type ArgumentSanitizer ¶ added in v1.1.3
type ArgumentSanitizer struct {
// contains filtered or unexported fields
}
ArgumentSanitizer sanitizes a slice of strings `.With()` given sanitizer functions applied on `.Get()`.
func NewArgumentSanitizer ¶ added in v1.1.3
func NewArgumentSanitizer(args []string) *ArgumentSanitizer
NewDefaultArgumentSanitizer returns a new ArgumentSanitizer without any actual sanitizers.
func NewDefaultArgumentSanitizer ¶ added in v1.1.3
func NewDefaultArgumentSanitizer(args []string) *ArgumentSanitizer
ExplodeShortsSanitizer retruns a sanitizer with all sanitizers enabled.
func (*ArgumentSanitizer) Get ¶ added in v1.1.3
func (s *ArgumentSanitizer) Get() []string
Get returns the sanitized arguments after applying all sanitizers.
func (*ArgumentSanitizer) With ¶ added in v1.1.3
func (s *ArgumentSanitizer) With(fn SanitizerFn) *ArgumentSanitizer
With adds a sanitizer function to the ArgumentSanitizer.
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
ParsedTags are a map of tags for each field in a struct at a given struct field index. Not each struct-field may have set a clapper tag.
type SanitizerFn ¶ added in v1.1.3
SanitizerFn is a function that sanitizes a slice of strings.
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 *ArgParserExt) *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 (iE long, short, ...)
func (*Tag) ArgumentName ¶ added in v1.1.3
ArgumentName returns the name of command line argument for this tag. Overrides like `long=foo-bar` are handled here.
func (*Tag) DeriveName ¶
type TagMap ¶ added in v1.1.3
TagMap represents all tags in a single struct fields tag line.
func (TagMap) HasInputTag ¶ added in v1.1.3
HasInputTag returns true if the TagMap contains a tag that can be filled by command line arguments.
func (TagMap) HasTagType ¶ added in v1.1.3
func (TagMap) InputArgument ¶ added in v1.1.3
InputArgument returns the name of the command line argument. Long names take precedence over short names. If there is no input tag, it returns "<unknown>".
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