Merge pull request #2820 from epage/option

fix(derive): Support SubcommandsNegateReqs
This commit is contained in:
Ed Page 2021-10-05 19:10:01 -05:00 committed by GitHub
commit 6fd3e0b834
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 29 deletions

View file

@ -717,9 +717,6 @@ impl Attrs {
if let Some(m) = res.find_method("default_value") {
abort!(m.name, "default_value is meaningless for Option")
}
if let Some(m) = res.find_method("required") {
abort!(m.name, "required is meaningless for Option")
}
}
Ty::OptionOption => {
if res.is_positional() {

View file

@ -298,3 +298,42 @@ fn two_optional_vecs() {
assert_eq!(Opt { arg: None, b: None }, Opt::parse_from(&["test"]));
}
#[test]
fn required_option_type() {
#[derive(Debug, PartialEq, Eq, Clap)]
#[clap(setting(clap::AppSettings::SubcommandsNegateReqs))]
struct Opt {
#[clap(required = true)]
req_str: Option<String>,
#[clap(subcommand)]
cmd: Option<SubCommands>,
}
#[derive(Debug, PartialEq, Eq, Clap)]
enum SubCommands {
ExSub {
#[clap(short, long, parse(from_occurrences))]
verbose: u8,
},
}
assert_eq!(
Opt {
req_str: Some(("arg").into()),
cmd: None,
},
Opt::parse_from(&["test", "arg"])
);
assert_eq!(
Opt {
req_str: None,
cmd: Some(SubCommands::ExSub { verbose: 1 }),
},
Opt::parse_from(&["test", "ex-sub", "-v"])
);
assert!(Opt::try_parse_from(&["test"]).is_err());
}

View file

@ -1,21 +0,0 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use clap::Clap;
#[derive(Clap, Debug)]
#[clap(name = "basic")]
struct Opt {
#[clap(short, required = true)]
n: Option<u32>,
}
fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
}

View file

@ -1,5 +0,0 @@
error: required is meaningless for Option
--> $DIR/option_required.rs:14:19
|
14 | #[clap(short, required = true)]
| ^^^^^^^^