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
Vector of anonymous args + named arg + subcommand leads to USAGE which fails to parse with cargo run ... --
#4238
Comments
With clap 3.2.22 and bash, Case 2 matches Case 4 in erroring out for me. |
The reason In clap 4, we are switching the derive to If you want the subcommand to win out over use clap::Parser;
#[derive(clap::Parser)]
#[clap(
name = "erm",
about = "Doesn't accept NAMED before ANONYMOUS as advertised by USAGE",
subcommand_precedence_over_arg = true
)]
pub struct Cli {
// Changing this from Vec<u32> to u32 makes the problem go away
pub anonymous: Vec<u32>,
#[clap(short, long)]
pub named: u32,
// Removing this makes the problem go away
#[clap(subcommand)]
reco: Subcommand,
}
#[derive(clap::Subcommand)]
enum Subcommand {
SubA,
SubB,
}
fn main() {
Cli::parse();
println!("All's well");
} |
See also #3721 |
Thanks. Agree with closing this in favour of #3721. The Is it possible to make |
display order is meant to change the other within a group and does't affect the ordering of groups. I don't think its worth changing the default as this is the order people generally expect. I don't see it being worth the cost to customize it (API surface, code size, etc). It also doesn't help that I'm not sympathetic to a case where positional arguments and subcommands are being mixed; that just seems like its asking for trouble, in terms of user confusion if nothing else. |
Perfectly reasonable. What design would you suggest for the use case
? I was hoping for something like As the input files typically number in the hundreds (and are specified with a wildcard) I'd rather avoid having to prepend each and every input with a (Apologies if this is not an appropriate place to ask this question. If so, just say so, and I'll buzz off.) |
Why is the algorithm a subcommand? And why are the args coming before it? Normally, I would all relevant arguments to come after the subcommand. Any arguments that come before the subcommand are generally application-wide configuration (color and logging support, etc). |
Hmm, maybe because of a combination of historical reasons and misunderstandings that have been lost in the mists of time ...
The interface that we've got used to looks something like this:
Probably because, historically, we wanted to compare how different
In our case, these would be the args to
As this is code for analyzing experimental data, and not your typical 'application', I guess it doesn't fit comfortably into the usual mould. Colour, logging and other typical application-config are completely irrelevant in this case. In an exploratory phase, the data are fairly constant: you want to see how different algos treat some representative data. So the input data are constant, and should go at the front: you want to try different algos and tweak their parameters, so those should go at the end, where they are most easily editable in the shell. Once you've discovered a good algo with a decent set of parameters, you probably want to fix those and run the process on many different sets of data. At this point the algo and its parameters feels like it should go at the front and the data at the end. But, as I said, this might be a misguided consequence of history and misunderstandings. |
Why isn't the algorithm a required option ( |
Quite possibly because I'm completely missing something. For the sake of illustration imagine we have two algorithms:
How would you express that with the algorithms being required options? |
If you do a |
I should mention that we've recently migrated from |
The feature set should be similar. |
Please complete the following tasks
Rust Version
rustc 1.62.0 (a8314ef7d 2022-06-27)
Clap Version
{ version = "3.2.20", features = ["derive"] }
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run --bin erm -- --named 1 2 sub-a
Actual Behaviour
target/debug/erm 1 --named 2 sub-a
-> All's welltarget/debug/erm --named 1 2 sub-a
-> All's wellcargo run --bin erm -- 1 --named 2 sub-a
-> All's wellcargo run --bin erm -- --named 1 2 sub-a
failsBy 'fails' I mean that it displays the
clap
help message, because it fails to parse the command line.Expected Behaviour
Version 4, above, shoud behave just like versions 1-3.
In other words, it should manage to parse the command line, which
cargo run
clap
-generated USAGE:erm --named <NAMED> [ANONYMOUS]... <SUBCOMMAND>
Additional Context
This behaviour was observed in both
zsh
andbash
. I'm aware thatzsh
has some options which may affect how commands are parsed, but don't remember any details about this.Debug Output
No response
The text was updated successfully, but these errors were encountered: