Skip to content

feat: Use structs for CLI-validation errors returned by Cobra. #2266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

eth-p
Copy link

@eth-p eth-p commented Apr 15, 2025

This pull request converts a bunch of fmt.Errorf errors into structs implementing the error interface.

Similar to #2178, the goal of this pull request is to make it easier for callers of a Cobra command to determine why a command failed without having to resort to regex matching or parsing the error message. This pull request goes a bit further, covering all types of CLI validation-related errors (with the exception of pflag-created ones) and adding methods to get the specific details of the errors.

Giving callers this info enables them to do new things such as:

  • Printing localized error messages.
  • Changing how the errors are displayed (e.g. pretty printing, colors).

The error structs added are:

  • InvalidArgCountError
  • InvalidArgValueError
  • UnknownSubcommandError
  • RequiredFlagError
  • FlagGroupError

Tests and documentation have been updated as well.

@CLAassistant
Copy link

CLAassistant commented Apr 15, 2025

CLA assistant check
All committers have signed the CLA.

@eth-p eth-p changed the title Use structs for CLI-validation errors returned by Cobra. feat: Use structs for CLI-validation errors returned by Cobra. Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants