-
Hi, I'm trying to port One of the features is how both #[derive(Debug, Parser)]
#[command(name = "cargo-tarpaulin")]
#[command(bin_name = "cargo")]
#[command(author, version, about, long_about = None)]
pub enum CargoTarpaulinCli {
Tarpaulin(TarpaulinCli),
}
match CargoTarpaulinCli::try_parse() {
Ok(CargoTarpaulinCli::Tarpaulin(tarpaulin_cli)) => tarpaulin_cli,
Err(_) => TarpaulinCli::parse(),
} Reversing the order of parsing yields exactly opposite results, so still incorrect. This is how it was originally done in clap v2. let args = App::new("cargo-tarpaulin");
// app building
let args = args.subcommand_matches("tarpaulin").unwrap_or(&args); How could I achieve the same functionality without reverting to builder-style commands? OtherI've tried looking around through issues, discussions and examples, but couldn't find this exact use case. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Would the following work? #!/usr/bin/env nargo
/*!
```cargo
[dependencies]
clap = { version = "4", features = ["derive"] }
```
*/
use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "cargo-tarpaulin")]
#[command(bin_name = "cargo")]
#[command(author, version, about, long_about = None)]
pub enum CargoTarpaulinCli {
Tarpaulin(TarpaulinCli),
}
#[derive(Debug, Parser)]
pub struct TarpaulinCli {}
fn main() {
let cli = match CargoTarpaulinCli::try_parse() {
Ok(CargoTarpaulinCli::Tarpaulin(tarpaulin_cli)) => tarpaulin_cli,
Err(err) if !err.use_stderr() => err.exit(),
Err(_) => TarpaulinCli::parse(),
};
dbg!(cli);
} We bubble Alternatively, you could do #!/usr/bin/env nargo
/*!
```cargo
[dependencies]
clap = { version = "4", features = ["derive"] }
```
*/
use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "cargo-tarpaulin")]
#[command(bin_name = "cargo")]
#[command(author, version, about, long_about = None)]
#[command(arg_required_else_help = true)]
#[command(args_conflicts_with_subcommands = true)]
pub struct CargoTarpaulinCli {
#[command(subcommand)]
indirect: Option<CargoTarpaulinCommand>,
#[command(flatten)]
direct: Option<TarpaulinCli>,
}
#[derive(Debug, Parser)]
pub enum CargoTarpaulinCommand {
Tarpaulin(TarpaulinCli),
}
#[derive(Debug, Parser)]
pub struct TarpaulinCli {}
fn main() {
let cli = match CargoTarpaulinCli::parse() {
CargoTarpaulinCli {
indirect: Some(CargoTarpaulinCommand::Tarpaulin(cli)),
direct: None,
} => cli,
CargoTarpaulinCli {
indirect: None,
direct: Some(cli),
} => cli,
CargoTarpaulinCli { .. } => unreachable!(),
};
dbg!(cli);
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for the fast response 😄 The first option works the same way the original did, for match TarpaulinCli::try_parse() {
Ok(tarpaulin_cli) => tarpaulin_cli,
Err(err) if !err.use_stderr() => err.exit(),
Err(_) => match CargoTarpaulinCommand::parse() {
CargoTarpaulinCommand::Tarpaulin(tarpaulin_cli) => tarpaulin_cli,
},
} Second option also does work and is very explicit, but I think I'll roll with the previous one |
Beta Was this translation helpful? Give feedback.
Would the following work?
We bubble
--version
and--help
…