mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
d840d5650e
Before #2005, `Clap` was a special trait that derived all clap traits it detected were relevant (including an enum getting both `ArgEnum`, `Clap`, and `Subcommand`). Now, we have elevated `Clap`, `Args`, `Subcommand`, and `ArgEnum` to be user facing but the name `Clap` isn't very descriptive. This also helps further clarify the relationships so a crate providing an item to be `#[clap(flatten)]` or `#[clap(subcommand)]` is more likely to choose the needed trait to derive. Also, my proposed fix fo #2785 includes making `App` attributes almost exclusively for `Clap`. Clarifying the names/roles will help communicate this. For prior discussion, see #2583
155 lines
3 KiB
Rust
155 lines
3 KiB
Rust
// 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::Parser;
|
|
|
|
#[test]
|
|
fn skip_1() {
|
|
#[derive(Parser, Debug, PartialEq)]
|
|
struct Opt {
|
|
#[clap(short)]
|
|
x: u32,
|
|
#[clap(skip)]
|
|
s: u32,
|
|
}
|
|
|
|
assert!(Opt::try_parse_from(&["test", "-x", "10", "20"]).is_err());
|
|
|
|
let mut opt = Opt::parse_from(&["test", "-x", "10"]);
|
|
assert_eq!(
|
|
opt,
|
|
Opt {
|
|
x: 10,
|
|
s: 0, // default
|
|
}
|
|
);
|
|
opt.s = 42;
|
|
|
|
opt.update_from(&["test", "-x", "22"]);
|
|
|
|
assert_eq!(opt, Opt { x: 22, s: 42 });
|
|
}
|
|
|
|
#[test]
|
|
fn skip_2() {
|
|
#[derive(Parser, Debug, PartialEq)]
|
|
struct Opt {
|
|
#[clap(short)]
|
|
x: u32,
|
|
#[clap(skip)]
|
|
ss: String,
|
|
#[clap(skip)]
|
|
sn: u8,
|
|
y: u32,
|
|
#[clap(skip)]
|
|
sz: u16,
|
|
t: u32,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::parse_from(&["test", "-x", "10", "20", "30"]),
|
|
Opt {
|
|
x: 10,
|
|
ss: String::from(""),
|
|
sn: 0,
|
|
y: 20,
|
|
sz: 0,
|
|
t: 30,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn skip_enum() {
|
|
#[derive(Debug, PartialEq)]
|
|
#[allow(unused)]
|
|
enum Kind {
|
|
A,
|
|
B,
|
|
}
|
|
|
|
impl Default for Kind {
|
|
fn default() -> Self {
|
|
Kind::B
|
|
}
|
|
}
|
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
|
pub struct Opt {
|
|
#[clap(long, short)]
|
|
number: u32,
|
|
#[clap(skip)]
|
|
k: Kind,
|
|
#[clap(skip)]
|
|
v: Vec<u32>,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::parse_from(&["test", "-n", "10"]),
|
|
Opt {
|
|
number: 10,
|
|
k: Kind::B,
|
|
v: vec![],
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn skip_help_doc_comments() {
|
|
#[derive(Parser, Debug, PartialEq)]
|
|
pub struct Opt {
|
|
#[clap(skip, about = "internal_stuff")]
|
|
a: u32,
|
|
|
|
#[clap(skip, long_about = "internal_stuff\ndo not touch")]
|
|
b: u32,
|
|
|
|
/// Not meant to be used by clap.
|
|
///
|
|
/// I want a default here.
|
|
#[clap(skip)]
|
|
c: u32,
|
|
|
|
#[clap(short, parse(try_from_str))]
|
|
n: u32,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::parse_from(&["test", "-n", "10"]),
|
|
Opt {
|
|
n: 10,
|
|
a: 0,
|
|
b: 0,
|
|
c: 0,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn skip_val() {
|
|
#[derive(Parser, Debug, PartialEq)]
|
|
pub struct Opt {
|
|
#[clap(long, short)]
|
|
number: u32,
|
|
|
|
#[clap(skip = "key")]
|
|
k: String,
|
|
|
|
#[clap(skip = vec![1, 2, 3])]
|
|
v: Vec<u32>,
|
|
}
|
|
|
|
assert_eq!(
|
|
Opt::parse_from(&["test", "-n", "10"]),
|
|
Opt {
|
|
number: 10,
|
|
k: "key".to_string(),
|
|
v: vec![1, 2, 3]
|
|
}
|
|
);
|
|
}
|