clap: AppSettings::SubcommandsNegateReqs may cause panic in Clap::parse(), Clap::try_parse()
Clap version
3.0.0-beta.2
Where
https://docs.rs/clap/3.0.0-beta.2/clap/enum.AppSettings.html#variant.SubcommandsNegateReqs
What’s wrong
There’s no mention that this AppSetting can cause the Clap derive macro to panic on parse() and try_parse()
How to fix
use clap::{AppSettings, Clap, IntoApp, FromArgMatches};
#[derive(Debug, Clap)]
#[clap(setting(AppSettings::SubcommandsNegateReqs))]
struct ExOpts {
req_str: String,
#[clap(subcommand)]
pub cmd: Option<SubCommands>,
}
#[derive(Debug, Clap)]
enum SubCommands{
ExSub {
#[clap(short, long, parse(from_occurrences))]
verbose: u8
},
}
fn main(){
// We cant use Clap::parse or try_parse because we have SubcommandsNegateReqs enabled
// this would cause a panic when Clap attempts to parse the req_str arg that isn't there
// let opts = ExOpts::parse(); // panics
// let opts = ExOpts::try_parse(); // panics
// Instead we need to check to see if a subcommand exists before we run from_arg_matches on ExOpts
let matches = ExOpts::into_app().get_matches();
if matches.subcommand_name().is_none() {
// If no subcommand we can derive ExOpts
let opts = ExOpts::from_arg_matches(&matches);
println!("{:?}", opts);
} else {
// If subcommand we need derive the subcommands instead
let cmd_opts = SubCommands::from_arg_matches(&matches);
println!("{:?}", cmd_opts);
}
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (11 by maintainers)
Commits related to this issue
- fix: Provide context for panics One of the challenges with #2255 is for the user to discover whats going wrong. This helps by at least telling people how they got into a bad state and we can search ... — committed to epage/clap by epage 3 years ago
- fix(derive): Support SubcommandsNegateReqs Before there was no way to make `SubcommandsNegateReqs` with `clap_derive` because it required a required field with a sentinel value for when the required ... — committed to epage/clap by epage 3 years ago
- fix(derive): Support SubcommandsNegateReqs Before there was no way to make `SubcommandsNegateReqs` with `clap_derive` because it required a required field with a sentinel value for when the required ... — committed to epage/clap by epage 3 years ago
- feat: Add backtraces to errors This is gated behind the `debug` feature flag so only explicit debugging cases pay the build time and runtime costs. The builder API's stack traces are generally not t... — committed to epage/clap by epage 3 years ago
- feat: Add backtraces to errors This is gated behind the `debug` feature flag so only explicit debugging cases pay the build time and runtime costs. The builder API's stack traces are generally not t... — committed to epage/clap by epage 3 years ago
- refactor(derive): Simplify 'error' handling Our goal is to not panic inside of the macro (e.g. #2255). Currently, we `.unwrap()` everywhere except when turning an `ArgMatches` into an `enum`. To ha... — committed to epage/clap by epage 3 years ago
- feat: Expose clap-style errors to users This gives users the basic error template for quick and dirty messages. In addition to the lack of customization, they are not given anything to help them with... — committed to epage/clap by epage 3 years ago
- feat: Expose clap-style errors to users This gives users the basic error template for quick and dirty messages. In addition to the lack of customization, they are not given anything to help them with... — committed to epage/clap by epage 3 years ago
- feat: Expose clap-style errors to users This gives users the basic error template for quick and dirty messages. In addition to the lack of customization, they are not given anything to help them with... — committed to epage/clap by epage 3 years ago
- feat: Expose clap-style errors to users This gives users the basic error template for quick and dirty messages. In addition to the lack of customization, they are not given anything to help them with... — committed to epage/clap by epage 3 years ago
- feat: Expose clap-style errors to users This gives users the basic error template for quick and dirty messages. In addition to the lack of customization, they are not given anything to help them with... — committed to epage/clap by epage 3 years ago
- fix(derive): Error, don't Panic Part of #2255 and in general will make it easier for people using the builder API to reuse logic from derive crates. — committed to epage/clap by epage 3 years ago
- fix(derive)!: Error, don't panic! The derive is generating `Error::raw` (from scratch or by converting existing erors) and then the inherent `Parser` methods format them. Fixes #2255 — committed to epage/clap by epage 3 years ago
- fix(derive)!: Error, don't panic! The derive is generating `Error::raw` (from scratch or by converting existing erors) and then the inherent `Parser` methods format them. Fixes #2255 — committed to epage/clap by epage 3 years ago
Oh wait. Haha, sorry. No it still panics. Sorry.
$ cargo run – ex-sub Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running
target/debug/clap-ex ex-sub
thread ‘main’ panicked at ‘calledOption::unwrap()
on aNone
value’, src/main.rs:9:14 note: run withRUST_BACKTRACE=1
environment variable to display a backtrace