Skip to content

Conversation

eth-p
Copy link

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

This pull request is the pflag complement to spf13/cobra#2266, converting a bunch of fmt.Errorf errors into structs implementing the error interface.

The goal of this pull request is to make it easier for callers of the Parse function to determine why flag parsing failed without having to resort to regex matching or parsing the returned error message. This pull request goes a bit further than using the %w error-wrapping format verb, opting to use structs as a way to both determine the type of returned error and get specific details about it (e.g. the relevant *pflag.Flag).

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:

  • NotExistError
  • ValueRequiredError
  • InvalidValueError
  • InvalidSyntaxError

Tests have been added as well, and I made sure to maintain compatibility with versions of pflag prior to this pull request by using the exact same error messages.

Add tests to ensure invalid arguments and unknown flag error messages
don't change.
@CLAassistant
Copy link

CLAassistant commented Apr 22, 2025

CLA assistant check
All committers have signed the CLA.

eth-p added 3 commits April 21, 2025 22:06
This allows callers to differentiate between error types without having
to parse the error message string.
Copy link
Collaborator

@tomasaschan tomasaschan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks for your contribution!

@tomasaschan tomasaschan merged commit d661846 into spf13:master Apr 22, 2025
5 checks passed
project-mirrors-bot-tu bot pushed a commit to project-mirrors/forgejo-runner-as-gitea-act-runner-fork that referenced this pull request Aug 2, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [github.com/spf13/pflag](https://github.com/spf13/pflag) | `v1.0.6` -> `v1.0.7` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fspf13%2fpflag/v1.0.7?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fspf13%2fpflag/v1.0.6/v1.0.7?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>spf13/pflag (github.com/spf13/pflag)</summary>

### [`v1.0.7`](https://github.com/spf13/pflag/releases/tag/v1.0.7)

[Compare Source](spf13/pflag@v1.0.6...v1.0.7)

#### What's Changed

- Fix defaultIsZeroValue check for generic Value types by [@&#8203;MidnightRocket](https://github.com/MidnightRocket) in spf13/pflag#422
- feat: Use structs for errors returned by pflag. by [@&#8203;eth-p](https://github.com/eth-p) in spf13/pflag#425
- Fix typos by [@&#8203;co63oc](https://github.com/co63oc) in spf13/pflag#428
- fix [#&#8203;423](spf13/pflag#423) : Add helper function and some documentation to parse shorthand go test flags. by [@&#8203;valdar](https://github.com/valdar) in spf13/pflag#424
- add support equivalent to golang flag.TextVar(), also fixes the test failure as described in [#&#8203;368](spf13/pflag#368) by [@&#8203;hujun-open](https://github.com/hujun-open) in spf13/pflag#418
- add support for Func() and BoolFunc() [#&#8203;426](spf13/pflag#426) by [@&#8203;LeGEC](https://github.com/LeGEC) in spf13/pflag#429
- fix: correct argument length check in FlagSet.Parse by [@&#8203;ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) in spf13/pflag#409
- fix usage message for func flags, fix arguments order by [@&#8203;LeGEC](https://github.com/LeGEC) in spf13/pflag#431
- Add support for time.Time flags by [@&#8203;max-frank](https://github.com/max-frank) in spf13/pflag#348

#### New Contributors

- [@&#8203;MidnightRocket](https://github.com/MidnightRocket) made their first contribution in spf13/pflag#422
- [@&#8203;eth-p](https://github.com/eth-p) made their first contribution in spf13/pflag#425
- [@&#8203;co63oc](https://github.com/co63oc) made their first contribution in spf13/pflag#428
- [@&#8203;valdar](https://github.com/valdar) made their first contribution in spf13/pflag#424
- [@&#8203;hujun-open](https://github.com/hujun-open) made their first contribution in spf13/pflag#418
- [@&#8203;LeGEC](https://github.com/LeGEC) made their first contribution in spf13/pflag#429
- [@&#8203;ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) made their first contribution in spf13/pflag#409
- [@&#8203;max-frank](https://github.com/max-frank) made their first contribution in spf13/pflag#348

**Full Changelog**: spf13/pflag@v1.0.6...v1.0.7

</details>

---

### Configuration

📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS40My41IiwidXBkYXRlZEluVmVyIjoiNDEuNDMuNSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/794
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
Co-authored-by: Renovate Bot <bot@kriese.eu>
Co-committed-by: Renovate Bot <bot@kriese.eu>
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.

3 participants