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