2020-01-18 12:10:07 +00:00
|
|
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
|
|
|
|
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
|
2022-01-04 20:10:35 +00:00
|
|
|
// Ana Hobden (@hoverbear) <operator@hoverbear.org>
|
2020-01-18 12:10:07 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
|
|
|
|
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
|
|
|
|
// MIT/Apache 2.0 license.
|
|
|
|
|
2022-08-19 03:07:04 +00:00
|
|
|
use clap::builder::BoolishValueParser;
|
|
|
|
use clap::builder::TypedValueParser as _;
|
2022-08-19 02:35:27 +00:00
|
|
|
use clap::ArgAction;
|
2022-08-05 19:22:09 +00:00
|
|
|
use clap::CommandFactory;
|
2021-07-13 17:50:55 +00:00
|
|
|
use clap::Parser;
|
2020-01-18 12:10:07 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn bool_type_is_flag() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
alice: bool,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: false },
|
|
|
|
Opt::try_parse_from(&["test"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: true },
|
|
|
|
Opt::try_parse_from(&["test", "-a"]).unwrap()
|
|
|
|
);
|
2022-06-01 15:31:23 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: true },
|
|
|
|
Opt::try_parse_from(&["test", "-a", "-a"]).unwrap()
|
|
|
|
);
|
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
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: true },
|
|
|
|
Opt::try_parse_from(&["test", "--alice"]).unwrap()
|
|
|
|
);
|
2020-01-18 12:10:07 +00:00
|
|
|
assert!(Opt::try_parse_from(&["test", "-i"]).is_err());
|
|
|
|
assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err());
|
|
|
|
}
|
|
|
|
|
2022-08-19 02:35:27 +00:00
|
|
|
#[test]
|
|
|
|
fn non_bool_type_flag() {
|
2022-08-19 03:07:04 +00:00
|
|
|
fn parse_from_flag(b: bool) -> usize {
|
|
|
|
if b {
|
|
|
|
10
|
|
|
|
} else {
|
|
|
|
5
|
|
|
|
}
|
2022-08-19 02:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = ArgAction::SetTrue, value_parser = BoolishValueParser::new().map(parse_from_flag))]
|
2022-08-19 02:35:27 +00:00
|
|
|
alice: usize,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = ArgAction::SetTrue, value_parser = BoolishValueParser::new().map(parse_from_flag))]
|
2022-08-19 02:35:27 +00:00
|
|
|
bob: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
let opt = Opt::try_parse_from(&["test"]).unwrap();
|
|
|
|
assert_eq!(opt.alice, 5);
|
|
|
|
assert_eq!(opt.bob, 5);
|
|
|
|
|
|
|
|
let opt = Opt::try_parse_from(&["test", "-a"]).unwrap();
|
|
|
|
assert_eq!(opt.alice, 10);
|
|
|
|
assert_eq!(opt.bob, 5);
|
|
|
|
|
|
|
|
let opt = Opt::try_parse_from(&["test", "-b"]).unwrap();
|
|
|
|
assert_eq!(opt.alice, 5);
|
|
|
|
assert_eq!(opt.bob, 10);
|
|
|
|
|
|
|
|
let opt = Opt::try_parse_from(&["test", "-b", "-a"]).unwrap();
|
|
|
|
assert_eq!(opt.alice, 10);
|
|
|
|
assert_eq!(opt.bob, 10);
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:22:09 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore] // Not a good path for supporting this atm
|
|
|
|
fn inferred_help() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2022-08-05 19:22:09 +00:00
|
|
|
struct Opt {
|
|
|
|
/// Foo
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2022-08-05 19:22:09 +00:00
|
|
|
help: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cmd = Opt::command();
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd.get_arguments().find(|a| a.get_id() == "help").unwrap();
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("Foo".to_owned()),
|
|
|
|
"Incorrect help"
|
|
|
|
);
|
2022-08-05 19:22:09 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore] // Not a good path for supporting this atm
|
|
|
|
fn inferred_version() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2022-08-05 19:22:09 +00:00
|
|
|
struct Opt {
|
|
|
|
/// Foo
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2022-08-05 19:22:09 +00:00
|
|
|
version: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cmd = Opt::command();
|
|
|
|
cmd.build();
|
|
|
|
let arg = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "version")
|
|
|
|
.unwrap();
|
2022-08-24 15:24:15 +00:00
|
|
|
assert_eq!(
|
|
|
|
arg.get_help().map(|s| s.to_string()),
|
|
|
|
Some("Foo".to_owned()),
|
|
|
|
"Incorrect help"
|
|
|
|
);
|
2022-08-05 19:22:09 +00:00
|
|
|
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:10:07 +00:00
|
|
|
#[test]
|
2022-06-01 15:31:23 +00:00
|
|
|
fn count() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
2022-06-09 16:09:36 +00:00
|
|
|
alice: u8,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
2022-06-09 16:09:36 +00:00
|
|
|
bob: u8,
|
2020-01-18 12:10:07 +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
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: 0, bob: 0 },
|
|
|
|
Opt::try_parse_from(&["test"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: 1, bob: 0 },
|
|
|
|
Opt::try_parse_from(&["test", "-a"]).unwrap()
|
|
|
|
);
|
2020-01-18 12:10:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: 2, bob: 0 },
|
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", "-a", "-a"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: 2, bob: 2 },
|
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", "-a", "--alice", "-bb"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt { alice: 3, bob: 1 },
|
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", "-aaa", "--bob"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert!(Opt::try_parse_from(&["test", "-i"]).is_err());
|
|
|
|
assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-04 15:15:04 +00:00
|
|
|
fn mixed_type_flags() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
alice: bool,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
2022-06-09 16:09:36 +00:00
|
|
|
bob: u8,
|
2020-01-18 12:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: false,
|
|
|
|
bob: 0
|
|
|
|
},
|
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"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: true,
|
|
|
|
bob: 0
|
|
|
|
},
|
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", "-a"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: true,
|
|
|
|
bob: 0
|
|
|
|
},
|
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", "-a"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: false,
|
|
|
|
bob: 1
|
|
|
|
},
|
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", "-b"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: true,
|
|
|
|
bob: 1
|
|
|
|
},
|
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", "--alice", "--bob"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
alice: true,
|
|
|
|
bob: 4
|
|
|
|
},
|
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", "-bb", "-a", "-bb"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
}
|
2021-11-04 15:15:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignore_qualified_bool_type() {
|
|
|
|
mod inner {
|
|
|
|
#[allow(non_camel_case_types)]
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
2021-11-04 15:15:04 +00:00
|
|
|
pub struct bool(pub String);
|
|
|
|
|
|
|
|
impl std::str::FromStr for self::bool {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Ok(self::bool(s.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-11-04 15:15:04 +00:00
|
|
|
struct Opt {
|
|
|
|
arg: inner::bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: inner::bool("success".into())
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test", "success"]).unwrap()
|
|
|
|
);
|
|
|
|
}
|
2022-06-02 17:48:52 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-06-01 15:31:23 +00:00
|
|
|
fn override_implicit_action() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2022-06-02 17:48:52 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, action = clap::ArgAction::Set)]
|
2022-06-02 17:48:52 +00:00
|
|
|
arg: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt { arg: false },
|
|
|
|
Opt::try_parse_from(&["test", "--arg", "false"]).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt { arg: true },
|
|
|
|
Opt::try_parse_from(&["test", "--arg", "true"]).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_implicit_from_flag_positional() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2022-06-02 17:48:52 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(action = clap::ArgAction::Set)]
|
2022-06-02 17:48:52 +00:00
|
|
|
arg: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt { arg: false },
|
|
|
|
Opt::try_parse_from(&["test", "false"]).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt { arg: true },
|
|
|
|
Opt::try_parse_from(&["test", "true"]).unwrap()
|
|
|
|
);
|
|
|
|
}
|