2
0

errors.go 4.3 KB

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