Skip to content
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

Fix missing required flag error uses flag name and not alias #1709

Conversation

nirhaas
Copy link

@nirhaas nirhaas commented Mar 20, 2023

What type of PR is this?

  • bug

What this PR does / why we need it:

Up until now, the missing required flag error showed the last alias of a flag, instead of the actual flag name. This results in a relatively unclear error messages:

Required flag "c" not set

Instead of

Required flag "collection" not set

Which issue(s) this PR fixes:

Fixes #1701

Testing

Added test case to ensure this change works as expected.

Release Notes

NONE

@nirhaas nirhaas requested a review from a team as a code owner March 20, 2023 13:19
for _, key := range f.Names() {
flagName = key
flagNames := f.Names()
flagName = flagNames[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if someone passes in a flag with no name or aliases defined ? Will the slice be nil ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 🦾

Copy link
Author

@nirhaas nirhaas Mar 21, 2023

Choose a reason for hiding this comment

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

It returns an empty slice (length 0) slice with empty string (length 1), not a nil. I can make a test case for it to prove that it doesn't panic if you want, even though the code doesn't handle this kind of flag nicely (showing as a newline in the help).

What would a flag without a name or alias means to the user? Shouldn't the code have a different check and error for this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

if len(flagNames) == 0 ln 208 will panic:

https://go.dev/play/p/aN-gKini_p9

Copy link
Author

Choose a reason for hiding this comment

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

@skelouse it's length 1 with an empty string. Sorry for the confusion. https://go.dev/play/p/tsnBea9RKk4 - No panic

Copy link
Contributor

Choose a reason for hiding this comment

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

Both examples provided are illustrative but its better to add a simple test case for this. Just have a flag without a name and see what happens. If everything works well and good.

Copy link
Author

Choose a reason for hiding this comment

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

Sure. Added another test case

@meatballhat meatballhat added kind/bug describes or fixes a bug area/v2 relates to / is being considered for v2 labels Mar 26, 2023
@meatballhat meatballhat added this to the Release 2.x milestone Mar 26, 2023
@meatballhat meatballhat changed the title v2 - Fix: missing required flag error uses flag name and not alias Fix missing required flag error uses flag name and not alias Mar 26, 2023
@meatballhat meatballhat added status/waiting-for-response Waiting for response from original requester and removed status/waiting-for-response Waiting for response from original requester labels Mar 26, 2023
@dearchap dearchap merged commit 31b7abb into urfave:v2-maint Mar 28, 2023
10 checks passed
mauromorales pushed a commit to kairos-io/provider-kairos that referenced this pull request May 3, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/urfave/cli/v2](https://togithub.com/urfave/cli) | require
| patch | `v2.25.1` -> `v2.25.3` |

---

### Release Notes

<details>
<summary>urfave/cli</summary>

### [`v2.25.3`](https://togithub.com/urfave/cli/releases/tag/v2.25.3)

[Compare
Source](https://togithub.com/urfave/cli/compare/v2.25.2...v2.25.3)

#### What's Changed

- Fix `incorrectTypeForFlagError` for unknowns by
[@&#8203;danhunsaker](https://togithub.com/danhunsaker) in
[urfave/cli#1708

#### New Contributors

- [@&#8203;danhunsaker](https://togithub.com/danhunsaker) made their
first contribution in
[urfave/cli#1708

**Full Changelog**:
urfave/cli@v2.25.2...v2.25.3

### [`v2.25.2`](https://togithub.com/urfave/cli/releases/tag/v2.25.2)

[Compare
Source](https://togithub.com/urfave/cli/compare/v2.25.1...v2.25.2)

#### What's Changed

- Fix missing required flag error uses flag name and not alias by
[@&#8203;nirhaas](https://togithub.com/nirhaas) in
[urfave/cli#1709
- Remove redundant variable declarations by
[@&#8203;huiyifyj](https://togithub.com/huiyifyj) in
[urfave/cli#1714
- Fix:(issue 1689) Match markdown output with help output by
[@&#8203;dearchap](https://togithub.com/dearchap) in
[urfave/cli#1723

#### New Contributors

- [@&#8203;nirhaas](https://togithub.com/nirhaas) made their first
contribution in
[urfave/cli#1709
- [@&#8203;huiyifyj](https://togithub.com/huiyifyj) made their first
contribution in
[urfave/cli#1714

**Full Changelog**:
urfave/cli@v2.25.1...v2.25.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **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 [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/kairos-io/provider-kairos).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42Ni4xIiwidXBkYXRlZEluVmVyIjoiMzUuNjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants