diff options
| author | Ethan P. <[email protected]> | 2025-04-21 21:47:13 -0700 |
|---|---|---|
| committer | Ethan P. <[email protected]> | 2025-04-21 22:06:34 -0700 |
| commit | ca5cf963c30751583422d6264a5427e9682f1a1a (patch) | |
| tree | 7588ff2fe2ddd1d0eecacec60f04fd019b2d6114 | |
| parent | 8d771585bd8aed7c04b9d409f30599f3c7ed39c7 (diff) | |
feat: Add getters to error structs
| -rw-r--r-- | errors.go | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -47,6 +47,19 @@ func (e *NotExistError) Error() string { panic(fmt.Errorf("unknown flagNotExistErrorMessageType: %v", e.messageType)) } +// GetSpecifiedName returns the name of the flag (without dashes) as it +// appeared in the parsed arguments. +func (e *NotExistError) GetSpecifiedName() string { + return e.name +} + +// GetSpecifiedShortnames returns the group of shorthand arguments +// (without dashes) that the flag appeared within. If the flag was not in a +// shorthand group, this will return an empty string. +func (e *NotExistError) GetSpecifiedShortnames() string { + return e.specifiedShorthands +} + // ValueRequiredError is the error returned when a flag needs an argument but // no argument was provided. type ValueRequiredError struct { @@ -65,6 +78,24 @@ func (e *ValueRequiredError) Error() string { return fmt.Sprintf("flag needs an argument: --%s", e.specifiedName) } +// GetFlag returns the flag for which the error occurred. +func (e *ValueRequiredError) GetFlag() *Flag { + return e.flag +} + +// GetSpecifiedName returns the name of the flag (without dashes) as it +// appeared in the parsed arguments. +func (e *ValueRequiredError) GetSpecifiedName() string { + return e.specifiedName +} + +// GetSpecifiedShortnames returns the group of shorthand arguments +// (without dashes) that the flag appeared within. If the flag was not in a +// shorthand group, this will return an empty string. +func (e *ValueRequiredError) GetSpecifiedShortnames() string { + return e.specifiedShorthands +} + // InvalidValueError is the error returned when an invalid value is used // for a flag. type InvalidValueError struct { @@ -85,6 +116,21 @@ func (e *InvalidValueError) Error() string { return fmt.Sprintf("invalid argument %q for %q flag: %v", e.value, flagName, e.cause) } +// Unwrap implements errors.Unwrap. +func (e *InvalidValueError) Unwrap() error { + return e.cause +} + +// GetFlag returns the flag for which the error occurred. +func (e *InvalidValueError) GetFlag() *Flag { + return e.flag +} + +// GetValue returns the invalid value that was provided. +func (e *InvalidValueError) GetValue() string { + return e.value +} + // InvalidSyntaxError is the error returned when a bad flag name is passed on // the command line. type InvalidSyntaxError struct { @@ -95,3 +141,9 @@ type InvalidSyntaxError struct { func (e *InvalidSyntaxError) Error() string { return fmt.Sprintf("bad flag syntax: %s", e.specifiedFlag) } + +// GetSpecifiedName returns the exact flag (with dashes) as it +// appeared in the parsed arguments. +func (e *InvalidSyntaxError) GetSpecifiedFlag() string { + return e.specifiedFlag +} |
