mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
Merge pull request #2820 from epage/option
fix(derive): Support SubcommandsNegateReqs
This commit is contained in:
commit
6fd3e0b834
4 changed files with 39 additions and 29 deletions
|
@ -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() {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
error: required is meaningless for Option
|
||||
--> $DIR/option_required.rs:14:19
|
||||
|
|
||||
14 | #[clap(short, required = true)]
|
||||
| ^^^^^^^^
|
Loading…
Add table
Reference in a new issue