2020-01-07 10:17:23 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
use clap::Parser;
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_1() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-01-07 10:17:23 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(short)]
|
|
|
|
x: u32,
|
|
|
|
#[clap(skip)]
|
|
|
|
s: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(Opt::try_parse_from(&["test", "-x", "10", "20"]).is_err());
|
2020-11-14 10:09:39 +00:00
|
|
|
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
let mut opt = Opt::try_parse_from(&["test", "-x", "10"]).unwrap();
|
2020-01-07 10:17:23 +00:00
|
|
|
assert_eq!(
|
2020-11-14 10:09:39 +00:00
|
|
|
opt,
|
2020-01-07 10:17:23 +00:00
|
|
|
Opt {
|
|
|
|
x: 10,
|
|
|
|
s: 0, // default
|
|
|
|
}
|
|
|
|
);
|
2020-11-14 10:09:39 +00:00
|
|
|
opt.s = 42;
|
|
|
|
|
|
|
|
opt.update_from(&["test", "-x", "22"]);
|
|
|
|
|
|
|
|
assert_eq!(opt, Opt { x: 22, s: 42 });
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_2() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-01-07 10:17:23 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(short)]
|
|
|
|
x: u32,
|
|
|
|
#[clap(skip)]
|
|
|
|
ss: String,
|
|
|
|
#[clap(skip)]
|
|
|
|
sn: u8,
|
|
|
|
y: u32,
|
|
|
|
#[clap(skip)]
|
|
|
|
sz: u16,
|
|
|
|
t: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
Opt::try_parse_from(&["test", "-x", "10", "20", "30"]).unwrap(),
|
2020-01-07 10:17:23 +00:00
|
|
|
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 {
|
2020-01-31 11:05:12 +00:00
|
|
|
Kind::B
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-01-07 10:17:23 +00:00
|
|
|
pub struct Opt {
|
|
|
|
#[clap(long, short)]
|
|
|
|
number: u32,
|
|
|
|
#[clap(skip)]
|
|
|
|
k: Kind,
|
|
|
|
#[clap(skip)]
|
|
|
|
v: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
|
2020-01-07 10:17:23 +00:00
|
|
|
Opt {
|
|
|
|
number: 10,
|
|
|
|
k: Kind::B,
|
|
|
|
v: vec![],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_help_doc_comments() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-01-07 10:17:23 +00:00
|
|
|
pub struct Opt {
|
2020-04-21 15:51:38 +00:00
|
|
|
#[clap(skip, about = "internal_stuff")]
|
2020-01-07 10:17:23 +00:00
|
|
|
a: u32,
|
|
|
|
|
2020-04-21 15:51:38 +00:00
|
|
|
#[clap(skip, long_about = "internal_stuff\ndo not touch")]
|
2020-01-07 10:17:23 +00:00
|
|
|
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!(
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
|
2020-01-07 10:17:23 +00:00
|
|
|
Opt {
|
|
|
|
n: 10,
|
|
|
|
a: 0,
|
|
|
|
b: 0,
|
|
|
|
c: 0,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_val() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-01-07 10:17:23 +00:00
|
|
|
pub struct Opt {
|
|
|
|
#[clap(long, short)]
|
|
|
|
number: u32,
|
|
|
|
|
|
|
|
#[clap(skip = "key")]
|
|
|
|
k: String,
|
|
|
|
|
|
|
|
#[clap(skip = vec![1, 2, 3])]
|
|
|
|
v: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
Opt::try_parse_from(&["test", "-n", "10"]).unwrap(),
|
2020-01-07 10:17:23 +00:00
|
|
|
Opt {
|
|
|
|
number: 10,
|
2020-01-31 11:05:12 +00:00
|
|
|
k: "key".to_string(),
|
2020-01-07 10:17:23 +00:00
|
|
|
v: vec![1, 2, 3]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|