-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
num_args = 2..
does not error with 0 args and error has incorrect wording with 1 arg
#5526
Comments
In the future, please open an issue per concern.
The behavior is correct. From the docs:
This isn't as obvious for positionals but for options, the behavior is more clear. use clap::Parser;
#[derive(Parser)]
struct Cli {
/// Args
#[arg(long, value_name = "ARG", num_args = 2..)]
args: Vec<String>,
}
fn main() {
let cli = Cli::parse();
println!("{:#?}", cli.args);
} $ # good: 2+ values
$ cmd --args asdf blah
$ # good: 2+ values
$ cmd --args asdf blah spam
$ # good: 2+ values per `--arg`
$ cmd --args asdf blah --arg spam pizza
$ # bad: too few values
$ cmd --args asdf
$ # bad: too few values for last `--arg`
$ cmd --args asdf blah --arg spam
$ # good: no occurrences
$ cmd What you are looking for is use clap::Parser;
use clap::builder::ArgAction;
#[derive(Parser)]
struct Cli {
/// Args
#[arg(long, value_name = "ARG", num_args = 2.., required = true, action = ArgAction::Set)]
args: Vec<String>,
}
fn main() {
let cli = Cli::parse();
println!("{:#?}", cli.args);
} |
Thank you for the detailed response, @epage! Will try your suggested fix. |
Please complete the following tasks
Rust Version
rustc 1.78.0 (9b00956e5 2024-04-29)
Clap Version
4.5.6
Minimal reproducible code
Steps to reproduce the bug with the above code
Error when running
cargo run -- asdf
:Shouldn't it say either of the following?
error: 1 more value required by '[ARG] [ARG]...'; only 1 was provided
error: 2 values required by '[ARG] [ARG]...'; only 1 was provided
(my personal preference is for this one)Actual Behaviour
There are 2 issues:
2..
and user gives 0, it does not produce an error as it should because the minimum number of args is 2Expected Behaviour
2..
and user gives 0, it should produce an error saying that it requires 2 args and 0 were givenAdditional Context
Unsure root cause on this one... a quick search did not find it... but I imagine that somewhere it tests the len against the num_args range (?)
clap/clap_builder/src/error/format.rs
Line 340 in 2f645d3
should be:
remove the word "more"
Debug Output
The text was updated successfully, but these errors were encountered: