From ca5cf963c30751583422d6264a5427e9682f1a1a Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Mon, 21 Apr 2025 21:47:13 -0700 Subject: feat: Add getters to error structs --- errors.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/errors.go b/errors.go index 4d72a93..ff11b66 100644 --- a/errors.go +++ b/errors.go @@ -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 +} -- cgit v1.2.3