2020-02-04 09:10:53 +01:00
|
|
|
#![allow(unused_imports, dead_code)]
|
|
|
|
|
2022-03-02 16:15:28 +01:00
|
|
|
use std::io::{BufRead, Cursor, Write};
|
2020-02-04 09:10:53 +01:00
|
|
|
use std::str;
|
|
|
|
|
2022-06-06 12:35:00 -05:00
|
|
|
use clap::{arg, Arg, ArgAction, ArgGroup, Command};
|
2020-02-04 09:10:53 +01:00
|
|
|
|
2022-04-29 15:32:25 -05:00
|
|
|
#[track_caller]
|
|
|
|
pub fn assert_eq<S, S2>(expected: S, actual: S2)
|
2020-02-04 09:10:53 +01:00
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
S2: AsRef<str>,
|
|
|
|
{
|
2022-04-29 15:32:25 -05:00
|
|
|
let expected = expected.as_ref();
|
|
|
|
let actual = actual.as_ref();
|
|
|
|
snapbox::assert_eq(expected, actual);
|
2020-02-04 09:10:53 +01:00
|
|
|
}
|
|
|
|
|
2022-04-29 15:32:25 -05:00
|
|
|
#[track_caller]
|
|
|
|
pub fn assert_output(l: Command, args: &str, expected: &str, stderr: bool) {
|
2020-02-04 09:10:53 +01:00
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
|
|
|
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
|
|
|
|
let err = res.unwrap_err();
|
2020-04-12 03:39:13 +02:00
|
|
|
write!(&mut buf, "{}", err).unwrap();
|
2022-04-29 15:32:25 -05:00
|
|
|
let actual = buf.into_inner();
|
|
|
|
let actual = String::from_utf8(actual).unwrap();
|
2020-02-04 09:10:53 +01:00
|
|
|
assert_eq!(
|
|
|
|
stderr,
|
|
|
|
err.use_stderr(),
|
|
|
|
"Should Use STDERR failed. Should be {} but is {}",
|
|
|
|
stderr,
|
|
|
|
err.use_stderr()
|
|
|
|
);
|
2022-04-29 15:39:17 -05:00
|
|
|
assert_eq(expected, actual)
|
2020-02-04 09:10:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:26:53 +03:00
|
|
|
// Legacy tests from the python script days
|
2020-02-04 09:10:53 +01:00
|
|
|
|
2022-08-15 14:29:46 -05:00
|
|
|
pub fn complex_app() -> Command {
|
2020-02-04 09:10:53 +01:00
|
|
|
let opt3_vals = ["fast", "slow"];
|
|
|
|
let pos3_vals = ["vi", "emacs"];
|
|
|
|
|
2022-02-11 21:48:29 -06:00
|
|
|
Command::new("clap-test")
|
2020-02-04 09:10:53 +01:00
|
|
|
.version("v1.4.8")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(
|
|
|
|
arg!(
|
|
|
|
-o --option <opt> "tests options"
|
|
|
|
)
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(1..)
|
2022-06-07 13:48:48 -05:00
|
|
|
.action(ArgAction::Append),
|
2021-11-19 14:33:11 -06:00
|
|
|
)
|
|
|
|
.arg(arg!([positional] "tests positionals"))
|
2022-06-06 12:35:00 -05:00
|
|
|
.arg(
|
|
|
|
arg!(-f --flag "tests flags")
|
|
|
|
.action(ArgAction::Count)
|
|
|
|
.global(true),
|
|
|
|
)
|
2022-08-15 13:21:05 -05:00
|
|
|
.args([
|
2021-11-19 14:33:11 -06:00
|
|
|
arg!(flag2: -F "tests flags with exclusions")
|
2020-02-04 09:10:53 +01:00
|
|
|
.conflicts_with("flag")
|
2022-06-09 20:03:28 -05:00
|
|
|
.requires("long-option-2")
|
|
|
|
.action(ArgAction::SetTrue),
|
2021-11-19 14:33:11 -06:00
|
|
|
arg!(--"long-option-2" <option2> "tests long options with exclusions")
|
|
|
|
.required(false)
|
2020-02-04 09:10:53 +01:00
|
|
|
.conflicts_with("option")
|
|
|
|
.requires("positional2"),
|
2021-11-19 14:33:11 -06:00
|
|
|
arg!([positional2] "tests positionals with exclusions"),
|
|
|
|
arg!(-O --option3 <option3> "specific vals")
|
|
|
|
.required(false)
|
2022-05-23 20:16:02 -05:00
|
|
|
.value_parser(opt3_vals),
|
|
|
|
arg!([positional3] ... "tests specific values").value_parser(pos3_vals),
|
2022-07-21 16:36:54 -05:00
|
|
|
arg!(--multvals <val> "Tests multiple values, not mult occs")
|
2022-08-15 13:21:05 -05:00
|
|
|
.value_names(["one", "two"])
|
2021-11-19 14:33:11 -06:00
|
|
|
.required(false),
|
2022-07-21 16:36:54 -05:00
|
|
|
arg!(--multvalsmo <val> ... "Tests multiple values, and mult occs")
|
2022-08-15 13:21:05 -05:00
|
|
|
.value_names(["one", "two"])
|
2021-11-19 14:33:11 -06:00
|
|
|
.required(false),
|
|
|
|
arg!(--minvals2 <minvals> "Tests 2 min vals")
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(2..),
|
2021-11-19 14:33:11 -06:00
|
|
|
arg!(--maxvals3 <maxvals> "Tests 3 max vals")
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(1..=3),
|
2022-01-26 10:23:35 -05:00
|
|
|
arg!(--optvaleq <optval> "Tests optional value, require = sign")
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(0..=1)
|
2022-01-26 10:23:35 -05:00
|
|
|
.require_equals(true),
|
|
|
|
arg!(--optvalnoeq <optval> "Tests optional value")
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(0..=1),
|
2020-02-04 09:10:53 +01:00
|
|
|
])
|
|
|
|
.subcommand(
|
2022-02-11 21:48:29 -06:00
|
|
|
Command::new("subcmd")
|
2020-02-04 09:10:53 +01:00
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2021-11-19 14:33:11 -06:00
|
|
|
.arg(
|
|
|
|
arg!(-o --option <scoption> "tests options")
|
|
|
|
.required(false)
|
2022-08-03 11:20:07 -05:00
|
|
|
.num_args(1..),
|
2021-11-19 14:33:11 -06:00
|
|
|
)
|
|
|
|
.arg(arg!(-s --subcmdarg <subcmdarg> "tests other args").required(false))
|
|
|
|
.arg(arg!([scpositional] "tests positionals")),
|
2020-02-04 09:10:53 +01:00
|
|
|
)
|
|
|
|
}
|