errors.go raw

   1  package pflag
   2  
   3  import "fmt"
   4  
   5  // notExistErrorMessageType specifies which flavor of "flag does not exist"
   6  // is printed by NotExistError. This allows the related errors to be grouped
   7  // under a single NotExistError struct without making a breaking change to
   8  // the error message text.
   9  type notExistErrorMessageType int
  10  
  11  const (
  12  	flagNotExistMessage notExistErrorMessageType = iota
  13  	flagNotDefinedMessage
  14  	flagNoSuchFlagMessage
  15  	flagUnknownFlagMessage
  16  	flagUnknownShorthandFlagMessage
  17  )
  18  
  19  // NotExistError is the error returned when trying to access a flag that
  20  // does not exist in the FlagSet.
  21  type NotExistError struct {
  22  	name                string
  23  	specifiedShorthands string
  24  	messageType         notExistErrorMessageType
  25  }
  26  
  27  // Error implements error.
  28  func (e *NotExistError) Error() string {
  29  	switch e.messageType {
  30  	case flagNotExistMessage:
  31  		return fmt.Sprintf("flag %q does not exist", e.name)
  32  
  33  	case flagNotDefinedMessage:
  34  		return fmt.Sprintf("flag accessed but not defined: %s", e.name)
  35  
  36  	case flagNoSuchFlagMessage:
  37  		return fmt.Sprintf("no such flag -%v", e.name)
  38  
  39  	case flagUnknownFlagMessage:
  40  		return fmt.Sprintf("unknown flag: --%s", e.name)
  41  
  42  	case flagUnknownShorthandFlagMessage:
  43  		c := rune(e.name[0])
  44  		return fmt.Sprintf("unknown shorthand flag: %q in -%s", c, e.specifiedShorthands)
  45  	}
  46  
  47  	panic(fmt.Errorf("unknown flagNotExistErrorMessageType: %v", e.messageType))
  48  }
  49  
  50  // GetSpecifiedName returns the name of the flag (without dashes) as it
  51  // appeared in the parsed arguments.
  52  func (e *NotExistError) GetSpecifiedName() string {
  53  	return e.name
  54  }
  55  
  56  // GetSpecifiedShortnames returns the group of shorthand arguments
  57  // (without dashes) that the flag appeared within. If the flag was not in a
  58  // shorthand group, this will return an empty string.
  59  func (e *NotExistError) GetSpecifiedShortnames() string {
  60  	return e.specifiedShorthands
  61  }
  62  
  63  // ValueRequiredError is the error returned when a flag needs an argument but
  64  // no argument was provided.
  65  type ValueRequiredError struct {
  66  	flag                *Flag
  67  	specifiedName       string
  68  	specifiedShorthands string
  69  }
  70  
  71  // Error implements error.
  72  func (e *ValueRequiredError) Error() string {
  73  	if len(e.specifiedShorthands) > 0 {
  74  		c := rune(e.specifiedName[0])
  75  		return fmt.Sprintf("flag needs an argument: %q in -%s", c, e.specifiedShorthands)
  76  	}
  77  
  78  	return fmt.Sprintf("flag needs an argument: --%s", e.specifiedName)
  79  }
  80  
  81  // GetFlag returns the flag for which the error occurred.
  82  func (e *ValueRequiredError) GetFlag() *Flag {
  83  	return e.flag
  84  }
  85  
  86  // GetSpecifiedName returns the name of the flag (without dashes) as it
  87  // appeared in the parsed arguments.
  88  func (e *ValueRequiredError) GetSpecifiedName() string {
  89  	return e.specifiedName
  90  }
  91  
  92  // GetSpecifiedShortnames returns the group of shorthand arguments
  93  // (without dashes) that the flag appeared within. If the flag was not in a
  94  // shorthand group, this will return an empty string.
  95  func (e *ValueRequiredError) GetSpecifiedShortnames() string {
  96  	return e.specifiedShorthands
  97  }
  98  
  99  // InvalidValueError is the error returned when an invalid value is used
 100  // for a flag.
 101  type InvalidValueError struct {
 102  	flag  *Flag
 103  	value string
 104  	cause error
 105  }
 106  
 107  // Error implements error.
 108  func (e *InvalidValueError) Error() string {
 109  	flag := e.flag
 110  	var flagName string
 111  	if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
 112  		flagName = fmt.Sprintf("-%s, --%s", flag.Shorthand, flag.Name)
 113  	} else {
 114  		flagName = fmt.Sprintf("--%s", flag.Name)
 115  	}
 116  	return fmt.Sprintf("invalid argument %q for %q flag: %v", e.value, flagName, e.cause)
 117  }
 118  
 119  // Unwrap implements errors.Unwrap.
 120  func (e *InvalidValueError) Unwrap() error {
 121  	return e.cause
 122  }
 123  
 124  // GetFlag returns the flag for which the error occurred.
 125  func (e *InvalidValueError) GetFlag() *Flag {
 126  	return e.flag
 127  }
 128  
 129  // GetValue returns the invalid value that was provided.
 130  func (e *InvalidValueError) GetValue() string {
 131  	return e.value
 132  }
 133  
 134  // InvalidSyntaxError is the error returned when a bad flag name is passed on
 135  // the command line.
 136  type InvalidSyntaxError struct {
 137  	specifiedFlag string
 138  }
 139  
 140  // Error implements error.
 141  func (e *InvalidSyntaxError) Error() string {
 142  	return fmt.Sprintf("bad flag syntax: %s", e.specifiedFlag)
 143  }
 144  
 145  // GetSpecifiedName returns the exact flag (with dashes) as it
 146  // appeared in the parsed arguments.
 147  func (e *InvalidSyntaxError) GetSpecifiedFlag() string {
 148  	return e.specifiedFlag
 149  }
 150