-
-
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
No way to accept unbounded number of a custom formatted values that starts with a hyphen #4283
Comments
allow_hyphen_values
with custom value_parser
unepxectedly parses known flags
allow_hyphen_values
with custom value_parser
unepxectedly parses known flagsallow_hyphen_values
with custom value_parser
unexpectedly parses known flags
I've reduced the reproduction case down to: #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "4", features = ["derive"] }
//! ```
use std::path::PathBuf;
use clap::Parser;
#[derive(Debug, clap::Parser)]
#[clap(version, about)]
pub struct Opt {
/// Input file
#[clap(short, long)]
pub input: Option<PathBuf>,
/// Allow a series of potentially hyphenated values
#[clap(required = true, allow_hyphen_values = true)]
pub choices: Vec<String>,
}
fn main() {
let opt = Opt::parse();
dbg!(opt);
} |
Switching to clap 3 produces the same result #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "3", features = ["derive"] }
//! ```
use std::path::PathBuf;
use clap::Parser;
#[derive(Debug, clap::Parser)]
#[clap(version, about)]
pub struct Opt {
/// Input file
#[clap(short, long)]
pub input: Option<PathBuf>,
/// Allow a series of potentially hyphenated values
#[clap(required = true, allow_hyphen_values = true)]
pub choices: Vec<String>,
}
fn main() {
let opt = Opt::parse();
dbg!(opt);
}
However, if I switch to #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "3", features = ["derive"] }
//! ```
use std::path::PathBuf;
use clap::Parser;
#[derive(Debug, clap::Parser)]
#[clap(version, about)]
#[clap(allow_hyphen_values = true)]
pub struct Opt {
/// Input file
#[clap(short, long)]
pub input: Option<PathBuf>,
/// Allow a series of potentially hyphenated values
#[clap(required = true)]
pub choices: Vec<String>,
}
fn main() {
let opt = Opt::parse();
dbg!(opt);
}
|
My apologies on the incomplete report. We were actually using (Plus, I learned about |
Yes, they are the same thing.
I'm actually using a fork that exists for the purpose of creating an RFC |
Hmm, looks like we had a test specifically for the "broken" behavior in clap 3. This is going to take some digging to unwind all of this #[test]
fn leading_hyphen_with_flag_after() {
let r = Command::new("mvae")
.arg(
arg!(o: -o <opt> "some opt")
.multiple_values(true)
.allow_hyphen_values(true),
)
.arg(arg!(f: -f "some flag").action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "-o", "-2", "-f"]);
assert!(r.is_ok(), "{}", r.unwrap_err());
let m = r.unwrap();
assert!(m.contains_id("o"));
assert_eq!(
m.get_many::<String>("o")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
&["-2", "-f"]
);
assert!(!*m.get_one::<bool>("f").expect("defaulted by clap"));
} |
allow_hyphen_values
with custom value_parser
unexpectedly parses known flagsallow_hyphen_values
unexpectedly parses known flags following positional arg
allow_hyphen_values
unexpectedly parses known flags following positional argallow_hyphen_values
unexpectedly parses known flags after positional arg
BurntSushi/ripgrep#233 (comment) discusses the motivation for the design in #755 In #4187, what I fixed was when you have the flag before the positional ( |
In 7a2bbca, we had a check for |
So the rules as of today
At the moment, I'm leaning towards this being a documentation bug rather than a behavior bug or feature request. |
This is reasonable. It would "break" the cli of That said, if a documentation change turns out to be the plan, I'd suggest changing both the docs linked above on |
If I'm understanding |
Correct, they can also be things like |
Something I've been considering is generalizing The issues to work out
|
This matches what we hope is the right behavior, see #4283
allow_hyphen_values
unexpectedly parses known flags after positional arg
That does sound interesting! I'm no |
At the moment, it is not. I have re-worked the issue for being focused on restoring your use case, somehow, including possibly the Another interesting angle to include in this is that when we are parsing, we try to capture up to the max for
|
Got it. Thank you for the triaging and explanations :) |
Please complete the following tasks
Rust Version
rustc 1.64.0 (a55dd71d5 2022-09-19)
Clap Version
4.0.2
Minimal reproducible code
Steps to reproduce the bug with the above code
Actual Behaviour
This is both in v3 and v4 with
Arg::allow_hyphen_values
Expected Behaviour
With clap v3 and
Command::allow_hyphen_values
, this parsed correctly as-3
as a positional value, and-i test/file.png
as an option.Also note that using
allow_negative_numbers
is not a solution here, as sometimes the values may be things like-3:-4
.Additional Context
https://docs.rs/clap/latest/clap/builder/struct.Arg.html#method.allow_hyphen_values
This issue seems to go against the documentation. Here, we have a positional argument followed by a known flag, but, the flag is NOT treated as a flag, and instead causes an unexpected parsing error.
This behavior is the same whether or not I add
trailing_var_arg = false
to the positional arg.Debug Output
The text was updated successfully, but these errors were encountered: