-
-
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
Error with multiple flags and positional arguments #3959
Comments
One thing missing the issue is where the expectation comes from. There is a lot of functionality to clap so its easier to lose track of and that extra context you have is helpful. For example, is this a regression? In trying to answer it myself, I first reminded myself of Command::allow_missing_positional I then gave it a try and found it interestingly inconsistent $ ./clap-3959.rs 1 2 3 4 5 6 7 8
input: ["1", "2", "3", "4", "5", "6", "7"]
output: "8"
one: None
two: None
yes: Some(false)
$ ./clap-3959.rs --one 1 2 3 4 5 6 7 8
input: ["2", "3", "4", "5", "6", "7"]
output: "8"
one: Some("1")
two: None
yes: Some(false)
$ ./clap-3959.rs --one 1 --two 2 3 4 5 6 7 8
error: Found argument '4' which wasn't expected, or isn't valid in this context
USAGE:
clap-3959_599c761b77b65bcdaa699e56 [OPTIONS] <input>... <output>
For more information try --help |
Yes, I described it in the related repo
I'll try other ways tomorrow |
I tried it and it doesn't help
For me, my expectation for a linked to repo is to be optional and all of the needed content is in the original issue. |
I backported to clap 3.0.0 and its still a problem then #!/usr/bin/env -S rust-script --debug
//! ```cargo
//! [dependencies]
//! clap = { version = "=3.0.0", features = ["derive", "debug"] }
//! ```
use clap::{App, Arg};
fn main() {
let matches = args().get_matches();
println!(
"input: {:?}",
matches
.values_of("input")
.unwrap_or_default()
.collect::<Vec<_>>()
);
println!("output: {:?}", matches.value_of("output"));
println!("one: {:?}", matches.value_of("one"));
println!("two: {:?}", matches.value_of("two"));
println!("yes: {:?}", matches.is_present("yes"));
}
fn args<'a>() -> clap::App<'a> {
App::new("Clap test")
.version("1.0")
.author("Dmitriy Pleshevskiy <[email protected]>")
.about("Error reproduction with multiple parameters")
//.allow_missing_positional(true)
.arg(Arg::new("yes").long("yes"))
.arg(Arg::new("one").long("one").takes_value(true))
.arg(Arg::new("two").long("two").takes_value(true))
.arg(Arg::new("input").multiple_values(true).required(true))
.arg(Arg::new("output").required(true))
} |
Looking at the log, it looks like we are increasing the positional counter when we shouldn't be. |
We had some tests for this but not sufficient obviously. The problem is we were tweaking the positional argument counter when processing flags and not just positional arguments. Delaying it until after flags seems to fix this. Fixes clap-rs#3959
Thanks for solving the problem so quickly! |
Please complete the following tasks
Rust Version
rustc 1.58.1 (db9d1b20b 2022-01-20)
Clap Version
3.2.13
Minimal reproducible code
Steps to reproduce the bug with the above code
Actual Behaviour
Expected Behaviour
Additional Context
More examples in repository with error reproduction. https://github.com/pleshevskiy/clap-multi-arg-repro
Debug Output
The text was updated successfully, but these errors were encountered: