Open x3rAx opened 3 years ago
Can you please run this with the master branch?
I'm sorry, but I am not sure how to do this. I added
clap = { git = "https://github.com/clap-rs/clap.git", branch = "master" }
to cargo.toml
and now I get the following errors and I don't understand how I can fix them (I'm new to Rust so please forgive me if this is actually pretty obvious):
error[E0308]: mismatched types
--> src/main.rs:27:13
|
27 | subcmd: SubCommand,
| ^^^^^^^^^^ expected enum `Option`, found `&mut SubCommand`
|
= note: expected enum `Option<(&str, &ArgMatches)>`
found mutable reference `&mut SubCommand`
error[E0308]: mismatched types
--> src/main.rs:42:13
|
42 | subcmd: TestSubCommand,
| ^^^^^^^^^^^^^^ expected enum `Option`, found `&mut TestSubCommand`
|
= note: expected enum `Option<(&str, &ArgMatches)>`
found mutable reference `&mut TestSubCommand`
What I tried was changing subcmd
of the Opts
struct to subcmd: Option<SubCommand>,
and subcmd: Option<(&str, &SubCommand)>,
but with no success...
You would need to clone the repo locally and point to it using path = "../clap"
instead of git
/branch
/version
.
From the project root, I ran
git clone https://github.com/clap-rs/clap.git
then changed the dependency in cargo.toml
to
clap = { path = "./clap" }
but I still get the same compile error as above.
I'm sorry, but I am not sure how to do this. I added
clap = { git = "https://github.com/clap-rs/clap.git", branch = "master" }
to
cargo.toml
and now I get the following errors and I don't understand how I can fix them (I'm new to Rust so please forgive me if this is actually pretty obvious):error[E0308]: mismatched types --> src/main.rs:27:13 | 27 | subcmd: SubCommand, | ^^^^^^^^^^ expected enum `Option`, found `&mut SubCommand` | = note: expected enum `Option<(&str, &ArgMatches)>` found mutable reference `&mut SubCommand` error[E0308]: mismatched types --> src/main.rs:42:13 | 42 | subcmd: TestSubCommand, | ^^^^^^^^^^^^^^ expected enum `Option`, found `&mut TestSubCommand` | = note: expected enum `Option<(&str, &ArgMatches)>` found mutable reference `&mut TestSubCommand`
What I tried was changing
subcmd
of theOpts
struct tosubcmd: Option<SubCommand>,
andsubcmd: Option<(&str, &SubCommand)>,
but with no success...
I noticed that this only happens when the field is named subcmd
. Renaming this field to anything else like sub
would fix the compilation issue. Not sure about the reason though
@omar25h Does his original code compile and it is giving the expected behaviour after that change?
If yes, in that case, we need to turn this issue into solving the subcmd
field name problem.
@pksunkara The original code compiles, but the behavior is still the same as 3.0.0-beta.2
, so the issue can be reproduced.
However, the subcmd
field name looks like a different problem. The code compiles on 3.0.0-beta.2
, but not on master
Now that the error has been fixed, can someone paste what the actual behaviour is on master?
Now that the error has been fixed, can someone paste what the actual behaviour is on master?
❯ tree .
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
├── test
└── xtest
1 directory, 5 files
❯ cat src/main.rs
use std::path::PathBuf;
use clap::Clap;
fn main() {
let _opts = Opts::parse();
}
#[derive(Clap)]
struct Opts {
#[clap(subcommand)]
subcmd: SubCommand,
}
#[derive(Clap)]
enum SubCommand {
File(File),
}
#[derive(Clap)]
struct File {
file: PathBuf,
#[clap(subcommand)]
subcmd: TestSubCommand,
}
#[derive(Clap)]
enum TestSubCommand {
Test(Test),
}
#[derive(Clap)]
struct Test {
value: i32
}
❯ cat test xtest
test file
xtest file
❯ cargo run --quiet -- file test test 1; echo $status
error: Found argument '1' which wasn't expected, or isn't valid in this context
USAGE:
clap_issue_2375 file <file> test <value>
For more information try --help
2
❯ cargo run --quiet -- file xtest test 1; echo $status
0
❯ cargo tree | rg "clap v" | head -1
├── clap v3.0.0-beta.2 (https://github.com/clap-rs/clap#2952fd6d)
So, the second case has been fixed (probably by @ldm0), but not the first case.
Please complete the following tasks
Rust Version
rustc 1.50.0 (cb75ad5db 2021-02-10)
Clap Version
clap = "3.0.0-beta.2"
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run -- file test test 1
cargo run -- file xtest test 1
Actual Behaviour
The first argument
file
is a subcommand, that accepts a filename and a sub-subcommandtest
which accepts a number. If the filename is the same as the sub-subcommand, it fails with the following error:Putting
--
anywhere between the arguments does not fix it but produces other errors.If the filename is similar to the subcommand name, then it fails and proposes the "correct" subcommand:
Again putting
--
anywhere between the arguments does not fix it but produces other errors.Expected Behaviour
The argument after
file
should be treated as an argument (as it is not optional), not as a subcommand, just as the help text suggests:Additional Context
No response
Debug Output