Skip to content

fix(lib): validate choices for variadic args and flags#520

Merged
jdx merged 1 commit intomainfrom
fix/variadic-choice-validation
Feb 24, 2026
Merged

fix(lib): validate choices for variadic args and flags#520
jdx merged 1 commit intomainfrom
fix/variadic-choice-validation

Conversation

@jdx
Copy link
Copy Markdown
Owner

@jdx jdx commented Feb 24, 2026

Summary

  • Variadic arguments and flags with choices were not being validated — any value was accepted
  • Added choice validation to both variadic code paths (positional args and flags) in parse.rs, matching the existing non-variadic behavior
  • Added 4 test cases covering valid and invalid choices for both variadic args and flags

Closes jdx/mise#8334

Test plan

  • cargo test -p usage-lib --test parse — all 32 tests pass
  • cargo clippy -p usage-lib --all-features -- -D warnings — clean

🤖 Generated with Claude Code


Note

Low Risk
Narrow change to CLI parsing validation: variadic args/flags with choices now error where they previously accepted any value, which may affect callers relying on permissive behavior.

Overview
Fixes a parsing gap where variadic positional args and variadic flag values (var=true) were not being validated against declared choices.

parse.rs now enforces choices for both variadic arg capture and variadic flag value capture, mirroring the existing non-variadic behavior (including --help/-h handling). Adds tests ensuring valid values pass and invalid values produce the expected error messages for both variadic args and flags.

Written by Cursor Bugbot for commit 88fbc18. This will update automatically on new commits. Configure here.

Copilot AI review requested due to automatic review settings February 24, 2026 17:06
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @jdx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a missing validation step for variadic arguments and flags. It ensures that when choices are defined, only those values are accepted, mirroring the behavior of non-variadic arguments. The changes include modifications to parse.rs and the addition of new test cases to verify the fix.

Highlights

  • Choice Validation: Implemented choice validation for variadic arguments and flags, ensuring that only allowed values are accepted.
  • Code Modification: Modified parse.rs to include choice validation in the variadic code paths for both positional arguments and flags.
  • Test Cases: Added four new test cases to cover valid and invalid choices for variadic arguments and flags.
Changelog
  • lib/src/parse.rs
    • Added choice validation for variadic flags.
    • Added choice validation for variadic arguments.
  • lib/tests/parse.rs
    • Added test case for valid variadic argument choices.
    • Added test case for invalid variadic argument choices.
    • Added test case for valid variadic flag choices.
    • Added test case for invalid variadic flag choices.
Activity
  • The author, jdx, has addressed issue #8334 by adding validation for choices in variadic arguments and flags.
  • The pull request includes updates to parse.rs and the addition of new test cases.
  • The test plan includes running cargo test -p usage-lib --test parse and cargo clippy -p usage-lib --all-features -- -D warnings.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread lib/src/parse.rs
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces crucial choice validation for variadic arguments and flags, enhancing the robustness and correctness of the parsing logic. The addition of comprehensive test cases for both valid and invalid scenarios is commendable and ensures the new functionality works as expected. There is a minor inconsistency in error handling for variadic flags when a help argument is encountered, which should be addressed to maintain consistent user experience.

Comment thread lib/src/parse.rs
Comment on lines +476 to +484
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
bail!(
"Invalid choice for option {}: {w}, expected one of {}",
flag.name,
choices.choices.join(", ")
);
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The choice validation for variadic flags is missing the is_help_arg check that is present in the non-variadic flag validation path (lines 495-500). This inconsistency could lead to a user receiving an "Invalid choice" error when attempting to request help for a variadic flag, instead of the expected help message. It's important to handle help arguments gracefully in all validation paths.

Suggested change
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
bail!(
"Invalid choice for option {}: {w}, expected one of {}",
flag.name,
choices.choices.join(", ")
);
}
}
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
if is_help_arg(spec, &w) {
out.errors
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
bail!(
"Invalid choice for option {}: {w}, expected one of {}",
flag.name,
choices.choices.join(", ")
);
}
}

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where variadic arguments and flags with choices constraints were not being validated, allowing any value to be accepted. The fix adds choice validation to both variadic code paths in the parser, matching the behavior of non-variadic arguments and flags.

Changes:

  • Added choice validation for variadic flags (parse.rs lines 476-484)
  • Added choice validation for variadic arguments (parse.rs lines 516-529)
  • Added 4 test cases covering valid and invalid choices for both variadic args and flags

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/src/parse.rs Added choice validation logic for variadic flags and arguments
lib/tests/parse.rs Added test cases for variadic argument and flag choice validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/src/parse.rs
Comment on lines +476 to +483
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
bail!(
"Invalid choice for option {}: {w}, expected one of {}",
flag.name,
choices.choices.join(", ")
);
}
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

The variadic flag choice validation is missing help argument handling that exists in the non-variadic flag path (lines 495-498). When a user provides an invalid choice for a variadic flag, they should still be able to request help with -h or --help. Add the same help check here before the bail! call to match the pattern used in non-variadic flags and variadic args.

Copilot uses AI. Check for mistakes.
@jdx jdx force-pushed the fix/variadic-choice-validation branch from 82947f9 to 02510ea Compare February 24, 2026 17:09
Variadic arguments and flags with `choices` were not being validated,
allowing any value to pass. Added choice validation to both variadic
code paths matching the existing non-variadic behavior.

Closes jdx/mise#8334

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@jdx jdx force-pushed the fix/variadic-choice-validation branch from 02510ea to 88fbc18 Compare February 24, 2026 22:22
@jdx jdx merged commit 2a9a12e into main Feb 24, 2026
7 checks passed
@jdx jdx deleted the fix/variadic-choice-validation branch February 24, 2026 22:26
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 7.40741% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.83%. Comparing base (5fb3420) to head (88fbc18).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/src/parse.rs 7.40% 10 Missing and 15 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #520      +/-   ##
==========================================
- Coverage   77.88%   75.83%   -2.05%     
==========================================
  Files          48       48              
  Lines        6660     6721      +61     
  Branches     6660     6721      +61     
==========================================
- Hits         5187     5097      -90     
- Misses       1109     1153      +44     
- Partials      364      471     +107     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

jdx pushed a commit that referenced this pull request Feb 24, 2026
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 25, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [usage](https://github.com/jdx/usage) | patch | `2.18.0` → `2.18.1` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jdx/usage (usage)</summary>

### [`v2.18.1`](https://github.com/jdx/usage/blob/HEAD/CHANGELOG.md#2181---2026-02-24)

[Compare Source](jdx/usage@v2.18.0...v2.18.1)

##### 🐛 Bug Fixes

- **(lib)** validate choices for variadic args and flags by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;520](jdx/usage#520)

##### 🛡️ Security

- require AI disclosure on GitHub comments by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;519](jdx/usage#519)

##### 📦️ Dependency Updates

- update autofix-ci/action action to v1.3.3 by [@&#8203;renovate\[bot\]](https://github.com/renovate\[bot]) in [#&#8203;515](jdx/usage#515)
- update rust crate clap to v4.5.60 by [@&#8203;renovate\[bot\]](https://github.com/renovate\[bot]) in [#&#8203;516](jdx/usage#516)
- lock file maintenance by [@&#8203;renovate\[bot\]](https://github.com/renovate\[bot]) in [#&#8203;518](jdx/usage#518)

</details>

---

### Configuration

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

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

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

---

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

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4zNS4xIiwidXBkYXRlZEluVmVyIjoiNDMuMzUuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6OnBhdGNoIl19-->
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