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.
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
use crate::utils;
|
2020-01-18 12:10:07 +00:00
|
|
|
|
2021-10-11 19:48:13 +00:00
|
|
|
use clap::{Args, Parser, Subcommand};
|
2020-01-18 12:10:07 +00:00
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
enum Opt {
|
|
|
|
/// Fetch stuff from GitHub
|
|
|
|
Fetch {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
all: bool,
|
|
|
|
/// Overwrite local branches.
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
force: bool,
|
2022-07-22 14:19:14 +00:00
|
|
|
|
2020-01-18 12:10:07 +00:00
|
|
|
repo: String,
|
|
|
|
},
|
|
|
|
|
|
|
|
Add {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
interactive: bool,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
2020-01-18 12:10:07 +00:00
|
|
|
verbose: bool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Fetch {
|
|
|
|
all: true,
|
|
|
|
force: false,
|
|
|
|
repo: "origin".to_string()
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "fetch", "--all", "origin"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Fetch {
|
|
|
|
all: false,
|
|
|
|
force: true,
|
|
|
|
repo: "origin".to_string()
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "fetch", "-f", "origin"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Add {
|
|
|
|
interactive: false,
|
|
|
|
verbose: false
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "add"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Add {
|
|
|
|
interactive: true,
|
|
|
|
verbose: true
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "add", "-i", "-v"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_parse() {
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "badcmd", "-i", "-v"]);
|
2020-01-18 12:10:07 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "add", "--badoption"]);
|
2020-01-18 12:10:07 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test"]);
|
2020-01-18 12:10:07 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
enum Opt2 {
|
2022-07-22 14:19:14 +00:00
|
|
|
DoSomething { arg: String },
|
2020-01-18 12:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// This test is specifically to make sure that hyphenated subcommands get
|
|
|
|
/// processed correctly.
|
|
|
|
fn test_hyphenated_subcommands() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt2::DoSomething {
|
|
|
|
arg: "blah".to_string()
|
|
|
|
},
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt2::try_parse_from(["test", "do-something", "blah"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
enum Opt3 {
|
|
|
|
Add,
|
|
|
|
Init,
|
|
|
|
Fetch,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_null_commands() {
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt3::Add, Opt3::try_parse_from(["test", "add"]).unwrap());
|
|
|
|
assert_eq!(Opt3::Init, Opt3::try_parse_from(["test", "init"]).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!(
|
|
|
|
Opt3::Fetch,
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt3::try_parse_from(["test", "fetch"]).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
|
|
|
);
|
2020-01-18 12:10:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(about = "Not shown")]
|
2020-01-18 12:10:07 +00:00
|
|
|
struct Add {
|
|
|
|
file: String,
|
|
|
|
}
|
|
|
|
/// Not shown
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
struct Fetch {
|
|
|
|
remote: String,
|
|
|
|
}
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2020-01-18 12:10:07 +00:00
|
|
|
enum Opt4 {
|
|
|
|
// Not shown
|
|
|
|
/// Add a file
|
|
|
|
Add(Add),
|
|
|
|
Init,
|
|
|
|
/// download history from remote
|
|
|
|
Fetch(Fetch),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_commands() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt4::Add(Add {
|
|
|
|
file: "f".to_string()
|
|
|
|
}),
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt4::try_parse_from(["test", "add", "f"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt4::Init, Opt4::try_parse_from(["test", "init"]).unwrap());
|
2020-01-18 12:10:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt4::Fetch(Fetch {
|
|
|
|
remote: "origin".to_string()
|
|
|
|
}),
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt4::try_parse_from(["test", "fetch", "origin"]).unwrap()
|
2020-01-18 12:10:07 +00:00
|
|
|
);
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let output = utils::get_long_help::<Opt4>();
|
2020-01-18 12:10:07 +00:00
|
|
|
|
|
|
|
assert!(output.contains("download history from remote"));
|
|
|
|
assert!(output.contains("Add a file"));
|
|
|
|
assert!(!output.contains("Not shown"));
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:20:21 +00:00
|
|
|
#[test]
|
2021-05-18 20:35:49 +00:00
|
|
|
fn global_passed_down() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2020-04-30 17:20:21 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(global = true, long)]
|
2021-05-19 18:42:39 +00:00
|
|
|
other: bool,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
enum Subcommands {
|
2020-05-01 12:33:36 +00:00
|
|
|
Add,
|
2020-07-19 20:31:59 +00:00
|
|
|
Global(GlobalCmd),
|
2020-04-30 17:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Args)]
|
2020-07-19 20:31:59 +00:00
|
|
|
struct GlobalCmd {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(from_global)]
|
2021-05-19 18:42:39 +00:00
|
|
|
other: bool,
|
2020-07-19 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:33:36 +00:00
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "global"]).unwrap(),
|
2020-05-01 12:33:36 +00:00
|
|
|
Opt {
|
2021-05-19 18:42:39 +00:00
|
|
|
other: false,
|
|
|
|
sub: Subcommands::Global(GlobalCmd { other: false })
|
2020-05-01 12:33:36 +00:00
|
|
|
}
|
|
|
|
);
|
2020-04-30 17:20:21 +00:00
|
|
|
|
2020-05-01 12:33:36 +00:00
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "global", "--other"]).unwrap(),
|
2020-05-01 12:33:36 +00:00
|
|
|
Opt {
|
2021-05-19 18:42:39 +00:00
|
|
|
other: true,
|
|
|
|
sub: Subcommands::Global(GlobalCmd { other: true })
|
2020-05-01 12:33:36 +00:00
|
|
|
}
|
|
|
|
);
|
2021-05-18 20:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_subcommand() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2021-05-18 20:35:49 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-05-18 20:35:49 +00:00
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Subcommand)]
|
2021-05-18 20:35:49 +00:00
|
|
|
enum Subcommands {
|
|
|
|
Add,
|
|
|
|
Remove,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(external_subcommand)]
|
2021-05-18 20:35:49 +00:00
|
|
|
Other(Vec<String>),
|
|
|
|
}
|
2020-04-30 17:20:21 +00:00
|
|
|
|
2020-07-19 20:31:59 +00:00
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "add"]).unwrap(),
|
2020-07-19 20:31:59 +00:00
|
|
|
Opt {
|
2021-05-18 20:35:49 +00:00
|
|
|
sub: Subcommands::Add
|
2020-07-19 20:31:59 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "remove"]).unwrap(),
|
2020-07-19 20:31:59 +00:00
|
|
|
Opt {
|
2021-05-18 20:35:49 +00:00
|
|
|
sub: Subcommands::Remove
|
2020-07-19 20:31:59 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["test"]).is_err());
|
2020-04-30 17:20:21 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
|
2020-04-30 17:20:21 +00:00
|
|
|
Opt {
|
|
|
|
sub: Subcommands::Other(vec!["git".into(), "status".into()])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_subcommand_os_string() {
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2020-04-30 17:20:21 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
enum Subcommands {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(external_subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
Other(Vec<OsString>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
|
2020-04-30 17:20:21 +00:00
|
|
|
Opt {
|
|
|
|
sub: Subcommands::Other(vec!["git".into(), "status".into()])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["test"]).is_err());
|
2020-04-30 17:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_subcommand_optional() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2020-04-30 17:20:21 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
sub: Option<Subcommands>,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
enum Subcommands {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(external_subcommand)]
|
2020-04-30 17:20:21 +00:00
|
|
|
Other(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
|
2020-04-30 17:20:21 +00:00
|
|
|
Opt {
|
|
|
|
sub: Some(Subcommands::Other(vec!["git".into(), "status".into()]))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert_eq!(Opt::try_parse_from(["test"]).unwrap(), Opt { sub: None });
|
2020-04-30 17:20:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 17:37:09 +00:00
|
|
|
#[test]
|
|
|
|
fn enum_in_enum_subsubcommand() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
2021-07-09 17:37:09 +00:00
|
|
|
pub enum Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(alias = "l")]
|
2022-02-23 15:22:42 +00:00
|
|
|
List,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand, alias = "d")]
|
2021-07-09 17:37:09 +00:00
|
|
|
Daemon(DaemonCommand),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Subcommand, Debug, PartialEq, Eq)]
|
2021-07-09 17:37:09 +00:00
|
|
|
pub enum DaemonCommand {
|
|
|
|
Start,
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test"]);
|
2021-07-09 17:37:09 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "list"]).unwrap();
|
2022-02-23 15:22:42 +00:00
|
|
|
assert_eq!(Opt::List, result);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "l"]).unwrap();
|
2022-02-23 15:22:42 +00:00
|
|
|
assert_eq!(Opt::List, result);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "daemon"]);
|
2021-07-09 17:37:09 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "daemon", "start"]).unwrap();
|
2021-07-09 17:37:09 +00:00
|
|
|
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
|
2022-02-23 15:22:42 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opt::try_parse_from(["test", "d", "start"]).unwrap();
|
2022-02-23 15:22:42 +00:00
|
|
|
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
|
2021-07-09 17:37:09 +00:00
|
|
|
}
|
2021-07-15 17:35:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_subcommands() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-07-15 17:35:20 +00:00
|
|
|
enum Opt {
|
|
|
|
Command1(Command1),
|
|
|
|
Command2(Command2),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-07-15 17:35:20 +00:00
|
|
|
struct Command1 {
|
|
|
|
arg1: i32,
|
2022-07-22 14:19:14 +00:00
|
|
|
|
2021-07-15 17:35:20 +00:00
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-07-15 17:35:20 +00:00
|
|
|
struct Command2 {
|
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full subcommand update
|
|
|
|
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "command1", "42", "44"])
|
2021-07-15 17:35:20 +00:00
|
|
|
.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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "command1", "42", "44"]).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
|
|
|
opt
|
|
|
|
);
|
2021-07-15 17:35:20 +00:00
|
|
|
|
|
|
|
// Partial subcommand update
|
|
|
|
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "command1", "42"]).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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "command1", "42", "14"]).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
|
|
|
opt
|
|
|
|
);
|
2021-07-15 17:35:20 +00:00
|
|
|
|
|
|
|
// Change subcommand
|
|
|
|
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "command2", "43"]).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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "command2", "43"]).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
|
|
|
opt
|
|
|
|
);
|
2021-07-15 17:35:20 +00:00
|
|
|
}
|
2021-07-19 18:42:07 +00:00
|
|
|
|
2023-01-10 01:58:09 +00:00
|
|
|
#[test]
|
|
|
|
fn update_subcommands_explicit_required() {
|
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
|
|
|
#[command(subcommand_required = true)]
|
|
|
|
enum Opt {
|
|
|
|
Command1(Command1),
|
|
|
|
Command2(Command2),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
|
|
|
struct Command1 {
|
|
|
|
arg1: i32,
|
|
|
|
|
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
|
|
|
struct Command2 {
|
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full subcommand update
|
|
|
|
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
|
2023-01-10 02:50:15 +00:00
|
|
|
opt.try_update_from(["test"]).unwrap();
|
|
|
|
assert_eq!(Opt::Command1(Command1 { arg1: 12, arg2: 14 }), opt);
|
2023-01-10 01:58:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 18:42:07 +00:00
|
|
|
#[test]
|
|
|
|
fn update_sub_subcommands() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-07-19 18:42:07 +00:00
|
|
|
enum Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-07-19 18:42:07 +00:00
|
|
|
Child1(Child1),
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-07-19 18:42:07 +00:00
|
|
|
Child2(Child2),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Subcommand, PartialEq, Eq, Debug)]
|
2021-07-19 18:42:07 +00:00
|
|
|
enum Child1 {
|
|
|
|
Command1(Command1),
|
|
|
|
Command2(Command2),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Subcommand, PartialEq, Eq, Debug)]
|
2021-07-19 18:42:07 +00:00
|
|
|
enum Child2 {
|
|
|
|
Command1(Command1),
|
|
|
|
Command2(Command2),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Args, PartialEq, Eq, Debug)]
|
2021-07-19 18:42:07 +00:00
|
|
|
struct Command1 {
|
|
|
|
arg1: i32,
|
2022-07-22 14:19:14 +00:00
|
|
|
|
2021-07-19 18:42:07 +00:00
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Args, PartialEq, Eq, Debug)]
|
2021-07-19 18:42:07 +00:00
|
|
|
struct Command2 {
|
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full subcommand update
|
|
|
|
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "child1", "command1", "42", "44"])
|
2021-07-19 18:42:07 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "child1", "command1", "42", "44"]).unwrap(),
|
2021-07-19 18:42:07 +00:00
|
|
|
opt
|
|
|
|
);
|
|
|
|
|
|
|
|
// Partial subcommand update
|
|
|
|
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "child1", "command1", "42"])
|
2021-07-19 18:42:07 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "child1", "command1", "42", "14"]).unwrap(),
|
2021-07-19 18:42:07 +00:00
|
|
|
opt
|
|
|
|
);
|
|
|
|
|
|
|
|
// Partial subcommand update
|
|
|
|
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "child1", "command2", "43"])
|
2021-07-19 18:42:07 +00:00
|
|
|
.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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "child1", "command2", "43"]).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
|
|
|
opt
|
|
|
|
);
|
2021-07-19 18:42:07 +00:00
|
|
|
|
|
|
|
// Change subcommand
|
|
|
|
let mut opt = Opt::Child1(Child1::Command1(Command1 { arg1: 12, arg2: 14 }));
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "child2", "command2", "43"])
|
2021-07-19 18:42:07 +00:00
|
|
|
.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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "child2", "command2", "43"]).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
|
|
|
opt
|
|
|
|
);
|
2021-07-19 18:42:07 +00:00
|
|
|
}
|
2021-08-13 17:33:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_ext_subcommand() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-08-13 17:33:10 +00:00
|
|
|
enum Opt {
|
|
|
|
Command1(Command1),
|
|
|
|
Command2(Command2),
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(external_subcommand)]
|
2021-08-13 17:33:10 +00:00
|
|
|
Ext(Vec<String>),
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Args, PartialEq, Eq, Debug)]
|
2021-08-13 17:33:10 +00:00
|
|
|
struct Command1 {
|
|
|
|
arg1: i32,
|
2022-07-22 14:19:14 +00:00
|
|
|
|
2021-08-13 17:33:10 +00:00
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Args, PartialEq, Eq, Debug)]
|
2021-08-13 17:33:10 +00:00
|
|
|
struct Command2 {
|
|
|
|
arg2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full subcommand update
|
|
|
|
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "ext", "42", "44"]).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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "ext", "42", "44"]).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
|
|
|
opt
|
|
|
|
);
|
2021-08-13 17:33:10 +00:00
|
|
|
|
|
|
|
// No partial subcommand update
|
|
|
|
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "ext", "42"]).unwrap();
|
|
|
|
assert_eq!(Opt::try_parse_from(["test", "ext", "42"]).unwrap(), opt);
|
2021-08-13 17:33:10 +00:00
|
|
|
|
|
|
|
// Change subcommand
|
|
|
|
let mut opt = Opt::Ext(vec!["12".into(), "14".into()]);
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "command2", "43"]).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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "command2", "43"]).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
|
|
|
opt
|
|
|
|
);
|
2021-08-13 17:33:10 +00:00
|
|
|
|
|
|
|
let mut opt = Opt::Command1(Command1 { arg1: 12, arg2: 14 });
|
2022-11-24 13:54:25 +00:00
|
|
|
opt.try_update_from(["test", "ext", "42", "44"]).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!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "ext", "42", "44"]).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
|
|
|
opt
|
|
|
|
);
|
2021-08-13 17:33:10 +00:00
|
|
|
}
|
2021-09-03 16:00:22 +00:00
|
|
|
#[test]
|
|
|
|
fn subcommand_name_not_literal() {
|
|
|
|
fn get_name() -> &'static str {
|
|
|
|
"renamed"
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
2021-09-03 16:00:22 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-09-03 16:00:22 +00:00
|
|
|
subcmd: SubCmd,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Subcommand, PartialEq, Eq, Debug)]
|
2021-09-03 16:00:22 +00:00
|
|
|
enum SubCmd {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(name = get_name())]
|
2021-09-03 16:00:22 +00:00
|
|
|
SubCmd1,
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
assert!(Opt::try_parse_from(["test", "renamed"]).is_ok());
|
2021-09-03 16:00:22 +00:00
|
|
|
}
|
2021-10-06 18:52:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_subcommand() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2021-10-06 18:52:02 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-10-06 18:52:02 +00:00
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Subcommand)]
|
2021-10-06 18:52:02 +00:00
|
|
|
enum Subcommands {
|
|
|
|
Add,
|
|
|
|
Remove,
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(skip)]
|
2021-10-06 18:52:02 +00:00
|
|
|
Skip,
|
2022-10-20 21:44:44 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[command(skip)]
|
|
|
|
Other(Other),
|
2021-10-06 18:52:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 21:44:44 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
enum Other {
|
|
|
|
One,
|
|
|
|
Twp,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(Subcommands::has_subcommand("add"));
|
|
|
|
assert!(Subcommands::has_subcommand("remove"));
|
|
|
|
assert!(!Subcommands::has_subcommand("skip"));
|
|
|
|
assert!(!Subcommands::has_subcommand("other"));
|
|
|
|
|
2021-10-06 18:52:02 +00:00
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "add"]).unwrap(),
|
2021-10-06 18:52:02 +00:00
|
|
|
Opt {
|
|
|
|
sub: Subcommands::Add
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Opt::try_parse_from(["test", "remove"]).unwrap(),
|
2021-10-06 18:52:02 +00:00
|
|
|
Opt {
|
|
|
|
sub: Subcommands::Remove
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let res = Opt::try_parse_from(["test", "skip"]);
|
2022-08-01 14:02:52 +00:00
|
|
|
assert_eq!(
|
|
|
|
res.unwrap_err().kind(),
|
2022-09-15 15:27:16 +00:00
|
|
|
clap::error::ErrorKind::InvalidSubcommand,
|
2022-08-01 14:02:52 +00:00
|
|
|
);
|
2022-10-20 21:44:44 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let res = Opt::try_parse_from(["test", "other"]);
|
2022-10-20 21:44:44 +00:00
|
|
|
assert_eq!(
|
|
|
|
res.unwrap_err().kind(),
|
|
|
|
clap::error::ErrorKind::InvalidSubcommand,
|
|
|
|
);
|
2021-10-06 18:52:02 +00:00
|
|
|
}
|
2022-05-06 20:46:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn built_in_subcommand_escaped() {
|
2022-08-11 21:07:58 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Parser)]
|
2022-05-06 20:46:34 +00:00
|
|
|
enum Command {
|
|
|
|
Install {
|
|
|
|
arg: Option<String>,
|
|
|
|
},
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(external_subcommand)]
|
2022-05-06 20:46:34 +00:00
|
|
|
Custom(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Command::try_parse_from(["test", "install", "arg"]).unwrap(),
|
2022-05-06 20:46:34 +00:00
|
|
|
Command::Install {
|
|
|
|
arg: Some(String::from("arg"))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Command::try_parse_from(["test", "--", "install"]).unwrap(),
|
2022-05-06 20:46:34 +00:00
|
|
|
Command::Custom(vec![String::from("install")])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-11-24 13:54:25 +00:00
|
|
|
Command::try_parse_from(["test", "--", "install", "arg"]).unwrap(),
|
2022-05-06 20:46:34 +00:00
|
|
|
Command::Custom(vec![String::from("install"), String::from("arg")])
|
|
|
|
);
|
|
|
|
}
|