Path: blob/main/vendor/github.com/spf13/pflag/errors.go
2875 views
package pflag12import "fmt"34// notExistErrorMessageType specifies which flavor of "flag does not exist"5// is printed by NotExistError. This allows the related errors to be grouped6// under a single NotExistError struct without making a breaking change to7// the error message text.8type notExistErrorMessageType int910const (11flagNotExistMessage notExistErrorMessageType = iota12flagNotDefinedMessage13flagNoSuchFlagMessage14flagUnknownFlagMessage15flagUnknownShorthandFlagMessage16)1718// NotExistError is the error returned when trying to access a flag that19// does not exist in the FlagSet.20type NotExistError struct {21name string22specifiedShorthands string23messageType notExistErrorMessageType24}2526// Error implements error.27func (e *NotExistError) Error() string {28switch e.messageType {29case flagNotExistMessage:30return fmt.Sprintf("flag %q does not exist", e.name)3132case flagNotDefinedMessage:33return fmt.Sprintf("flag accessed but not defined: %s", e.name)3435case flagNoSuchFlagMessage:36return fmt.Sprintf("no such flag -%v", e.name)3738case flagUnknownFlagMessage:39return fmt.Sprintf("unknown flag: --%s", e.name)4041case flagUnknownShorthandFlagMessage:42c := rune(e.name[0])43return fmt.Sprintf("unknown shorthand flag: %q in -%s", c, e.specifiedShorthands)44}4546panic(fmt.Errorf("unknown flagNotExistErrorMessageType: %v", e.messageType))47}4849// GetSpecifiedName returns the name of the flag (without dashes) as it50// appeared in the parsed arguments.51func (e *NotExistError) GetSpecifiedName() string {52return e.name53}5455// GetSpecifiedShortnames returns the group of shorthand arguments56// (without dashes) that the flag appeared within. If the flag was not in a57// shorthand group, this will return an empty string.58func (e *NotExistError) GetSpecifiedShortnames() string {59return e.specifiedShorthands60}6162// ValueRequiredError is the error returned when a flag needs an argument but63// no argument was provided.64type ValueRequiredError struct {65flag *Flag66specifiedName string67specifiedShorthands string68}6970// Error implements error.71func (e *ValueRequiredError) Error() string {72if len(e.specifiedShorthands) > 0 {73c := rune(e.specifiedName[0])74return fmt.Sprintf("flag needs an argument: %q in -%s", c, e.specifiedShorthands)75}7677return fmt.Sprintf("flag needs an argument: --%s", e.specifiedName)78}7980// GetFlag returns the flag for which the error occurred.81func (e *ValueRequiredError) GetFlag() *Flag {82return e.flag83}8485// GetSpecifiedName returns the name of the flag (without dashes) as it86// appeared in the parsed arguments.87func (e *ValueRequiredError) GetSpecifiedName() string {88return e.specifiedName89}9091// GetSpecifiedShortnames returns the group of shorthand arguments92// (without dashes) that the flag appeared within. If the flag was not in a93// shorthand group, this will return an empty string.94func (e *ValueRequiredError) GetSpecifiedShortnames() string {95return e.specifiedShorthands96}9798// InvalidValueError is the error returned when an invalid value is used99// for a flag.100type InvalidValueError struct {101flag *Flag102value string103cause error104}105106// Error implements error.107func (e *InvalidValueError) Error() string {108flag := e.flag109var flagName string110if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {111flagName = fmt.Sprintf("-%s, --%s", flag.Shorthand, flag.Name)112} else {113flagName = fmt.Sprintf("--%s", flag.Name)114}115return fmt.Sprintf("invalid argument %q for %q flag: %v", e.value, flagName, e.cause)116}117118// Unwrap implements errors.Unwrap.119func (e *InvalidValueError) Unwrap() error {120return e.cause121}122123// GetFlag returns the flag for which the error occurred.124func (e *InvalidValueError) GetFlag() *Flag {125return e.flag126}127128// GetValue returns the invalid value that was provided.129func (e *InvalidValueError) GetValue() string {130return e.value131}132133// InvalidSyntaxError is the error returned when a bad flag name is passed on134// the command line.135type InvalidSyntaxError struct {136specifiedFlag string137}138139// Error implements error.140func (e *InvalidSyntaxError) Error() string {141return fmt.Sprintf("bad flag syntax: %s", e.specifiedFlag)142}143144// GetSpecifiedName returns the exact flag (with dashes) as it145// appeared in the parsed arguments.146func (e *InvalidSyntaxError) GetSpecifiedFlag() string {147return e.specifiedFlag148}149150151