clap/tests/derive/subcommands.rs

633 lines
15 KiB
Rust
Raw Normal View History

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.
use crate::utils;
2020-01-18 12:10:07 +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 {
#[arg(long)]
2020-01-18 12:10:07 +00:00
all: bool,
/// Overwrite local branches.
#[arg(short, long)]
2020-01-18 12:10:07 +00:00
force: bool,
2020-01-18 12:10:07 +00:00
repo: String,
},
Add {
#[arg(short, long)]
2020-01-18 12:10:07 +00:00
interactive: bool,
#[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 {
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)]
#[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
);
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"));
}
#[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)]
struct Opt {
#[arg(global = true, long)]
other: bool,
#[command(subcommand)]
sub: Subcommands,
}
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Subcommand)]
enum Subcommands {
Add,
Global(GlobalCmd),
}
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Args)]
struct GlobalCmd {
#[arg(from_global)]
other: bool,
}
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "global"]).unwrap(),
Opt {
other: false,
sub: Subcommands::Global(GlobalCmd { other: false })
}
);
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "global", "--other"]).unwrap(),
Opt {
other: true,
sub: Subcommands::Global(GlobalCmd { other: true })
}
);
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 {
#[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,
#[command(external_subcommand)]
2021-05-18 20:35:49 +00:00
Other(Vec<String>),
}
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "add"]).unwrap(),
Opt {
2021-05-18 20:35:49 +00:00
sub: Subcommands::Add
}
);
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "remove"]).unwrap(),
Opt {
2021-05-18 20:35:49 +00:00
sub: Subcommands::Remove
}
);
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["test"]).is_err());
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
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)]
struct Opt {
#[command(subcommand)]
sub: Subcommands,
}
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Subcommand)]
enum Subcommands {
#[command(external_subcommand)]
Other(Vec<OsString>),
}
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
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());
}
#[test]
fn external_subcommand_optional() {
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Parser)]
struct Opt {
#[command(subcommand)]
sub: Option<Subcommands>,
}
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Subcommand)]
enum Subcommands {
#[command(external_subcommand)]
Other(Vec<String>),
}
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "git", "status"]).unwrap(),
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 });
}
#[test]
fn enum_in_enum_subsubcommand() {
2022-08-11 21:07:58 +00:00
#[derive(Parser, Debug, PartialEq, Eq)]
pub enum Opt {
#[command(alias = "l")]
List,
#[command(subcommand, alias = "d")]
Daemon(DaemonCommand),
}
2022-08-11 21:07:58 +00:00
#[derive(Subcommand, Debug, PartialEq, Eq)]
pub enum DaemonCommand {
Start,
Stop,
}
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test"]);
assert!(result.is_err());
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test", "list"]).unwrap();
assert_eq!(Opt::List, result);
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test", "l"]).unwrap();
assert_eq!(Opt::List, result);
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test", "daemon"]);
assert!(result.is_err());
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test", "daemon", "start"]).unwrap();
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
2022-11-24 13:54:25 +00:00
let result = Opt::try_parse_from(["test", "d", "start"]).unwrap();
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}
#[test]
fn update_subcommands() {
2022-08-11 21:07:58 +00:00
#[derive(Parser, PartialEq, Eq, Debug)]
enum Opt {
Command1(Command1),
Command2(Command2),
}
2022-08-11 21:07:58 +00:00
#[derive(Parser, PartialEq, Eq, Debug)]
struct Command1 {
arg1: i32,
arg2: i32,
}
2022-08-11 21:07:58 +00:00
#[derive(Parser, PartialEq, Eq, Debug)]
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"])
.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
);
// 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
);
// 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
);
}
#[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 });
opt.try_update_from(["test"]).unwrap();
assert_eq!(Opt::Command1(Command1 { arg1: 12, arg2: 14 }), opt);
}
#[test]
fn update_sub_subcommands() {
2022-08-11 21:07:58 +00:00
#[derive(Parser, PartialEq, Eq, Debug)]
enum Opt {
#[command(subcommand)]
Child1(Child1),
#[command(subcommand)]
Child2(Child2),
}
2022-08-11 21:07:58 +00:00
#[derive(Subcommand, PartialEq, Eq, Debug)]
enum Child1 {
Command1(Command1),
Command2(Command2),
}
2022-08-11 21:07:58 +00:00
#[derive(Subcommand, PartialEq, Eq, Debug)]
enum Child2 {
Command1(Command1),
Command2(Command2),
}
2022-08-11 21:07:58 +00:00
#[derive(Args, PartialEq, Eq, Debug)]
struct Command1 {
arg1: i32,
arg2: i32,
}
2022-08-11 21:07:58 +00:00
#[derive(Args, PartialEq, Eq, Debug)]
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"])
.unwrap();
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "child1", "command1", "42", "44"]).unwrap(),
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"])
.unwrap();
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "child1", "command1", "42", "14"]).unwrap(),
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"])
.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
);
// 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"])
.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
);
}
#[test]
fn update_ext_subcommand() {
2022-08-11 21:07:58 +00:00
#[derive(Parser, PartialEq, Eq, Debug)]
enum Opt {
Command1(Command1),
Command2(Command2),
#[command(external_subcommand)]
Ext(Vec<String>),
}
2022-08-11 21:07:58 +00:00
#[derive(Args, PartialEq, Eq, Debug)]
struct Command1 {
arg1: i32,
arg2: i32,
}
2022-08-11 21:07:58 +00:00
#[derive(Args, PartialEq, Eq, Debug)]
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
);
// 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);
// 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
);
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
);
}
#[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)]
struct Opt {
#[command(subcommand)]
subcmd: SubCmd,
}
2022-08-11 21:07:58 +00:00
#[derive(Subcommand, PartialEq, Eq, Debug)]
enum SubCmd {
#[command(name = get_name())]
SubCmd1,
}
2022-11-24 13:54:25 +00:00
assert!(Opt::try_parse_from(["test", "renamed"]).is_ok());
}
#[test]
fn skip_subcommand() {
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Parser)]
struct Opt {
#[command(subcommand)]
sub: Subcommands,
}
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Subcommand)]
enum Subcommands {
Add,
Remove,
#[allow(dead_code)]
#[command(skip)]
Skip,
#[allow(dead_code)]
#[command(skip)]
Other(Other),
}
#[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"));
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "add"]).unwrap(),
Opt {
sub: Subcommands::Add
}
);
assert_eq!(
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "remove"]).unwrap(),
Opt {
sub: Subcommands::Remove
}
);
2022-11-24 13:54:25 +00:00
let res = Opt::try_parse_from(["test", "skip"]);
assert_eq!(
res.unwrap_err().kind(),
clap::error::ErrorKind::InvalidSubcommand,
);
2022-11-24 13:54:25 +00:00
let res = Opt::try_parse_from(["test", "other"]);
assert_eq!(
res.unwrap_err().kind(),
clap::error::ErrorKind::InvalidSubcommand,
);
}
#[test]
fn built_in_subcommand_escaped() {
2022-08-11 21:07:58 +00:00
#[derive(Debug, PartialEq, Eq, Parser)]
enum Command {
Install {
arg: Option<String>,
},
#[command(external_subcommand)]
Custom(Vec<String>),
}
assert_eq!(
2022-11-24 13:54:25 +00:00
Command::try_parse_from(["test", "install", "arg"]).unwrap(),
Command::Install {
arg: Some(String::from("arg"))
}
);
assert_eq!(
2022-11-24 13:54:25 +00:00
Command::try_parse_from(["test", "--", "install"]).unwrap(),
Command::Custom(vec![String::from("install")])
);
assert_eq!(
2022-11-24 13:54:25 +00:00
Command::try_parse_from(["test", "--", "install", "arg"]).unwrap(),
Command::Custom(vec![String::from("install"), String::from("arg")])
);
}