1 #![allow(unused_imports, dead_code)]
2
3 use std::io::{BufRead, Cursor, Write};
4 use std::str;
5
6 use clap::{arg, Arg, ArgAction, ArgGroup, Command};
7
8 pub const FULL_TEMPLATE: &str = "\
9 {before-help}{name} {version}
10 {author-with-newline}{about-with-newline}
11 {usage-heading} {usage}
12
13 {all-args}{after-help}";
14
15 #[track_caller]
assert_eq<S, S2>(expected: S, actual: S2) where S: AsRef<str>, S2: AsRef<str>,16 pub fn assert_eq<S, S2>(expected: S, actual: S2)
17 where
18 S: AsRef<str>,
19 S2: AsRef<str>,
20 {
21 let expected = expected.as_ref();
22 let actual = actual.as_ref();
23 snapbox::assert_eq(expected, actual);
24 }
25
26 #[track_caller]
assert_output(l: Command, args: &str, expected: &str, stderr: bool)27 pub fn assert_output(l: Command, args: &str, expected: &str, stderr: bool) {
28 let mut buf = Cursor::new(Vec::with_capacity(50));
29 let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
30 let err = res.unwrap_err();
31 write!(&mut buf, "{err}").unwrap();
32 let actual = buf.into_inner();
33 let actual = String::from_utf8(actual).unwrap();
34 assert_eq!(
35 stderr,
36 err.use_stderr(),
37 "Should Use STDERR failed. Should be {} but is {}",
38 stderr,
39 err.use_stderr()
40 );
41 assert_eq(expected, actual)
42 }
43
44 // Legacy tests from the python script days
45
complex_app() -> Command46 pub fn complex_app() -> Command {
47 let opt3_vals = ["fast", "slow"];
48 let pos3_vals = ["vi", "emacs"];
49
50 Command::new("clap-test")
51 .version("v1.4.8")
52 .about("tests clap library")
53 .author("Kevin K. <kbknapp@gmail.com>")
54 .help_template(FULL_TEMPLATE)
55 .arg(
56 arg!(
57 -o --option <opt> "tests options"
58 )
59 .required(false)
60 .num_args(1..)
61 .action(ArgAction::Append),
62 )
63 .arg(arg!([positional] "tests positionals"))
64 .arg(
65 arg!(-f --flag "tests flags")
66 .action(ArgAction::Count)
67 .global(true),
68 )
69 .args([
70 arg!(flag2: -F "tests flags with exclusions")
71 .conflicts_with("flag")
72 .requires("long-option-2")
73 .action(ArgAction::SetTrue),
74 arg!(--"long-option-2" <option2> "tests long options with exclusions")
75 .conflicts_with("option")
76 .requires("positional2"),
77 arg!([positional2] "tests positionals with exclusions"),
78 arg!(-O --option3 <option3> "specific vals").value_parser(opt3_vals),
79 arg!([positional3] ... "tests specific values").value_parser(pos3_vals),
80 arg!(--multvals <val> "Tests multiple values, not mult occs")
81 .value_names(["one", "two"]),
82 arg!(--multvalsmo <val> ... "Tests multiple values, and mult occs")
83 .value_names(["one", "two"]),
84 arg!(--minvals2 <minvals> "Tests 2 min vals").num_args(2..),
85 arg!(--maxvals3 <maxvals> "Tests 3 max vals").num_args(1..=3),
86 arg!(--optvaleq <optval> "Tests optional value, require = sign")
87 .num_args(0..=1)
88 .require_equals(true),
89 arg!(--optvalnoeq <optval> "Tests optional value").num_args(0..=1),
90 ])
91 .subcommand(
92 Command::new("subcmd")
93 .about("tests subcommands")
94 .version("0.1")
95 .author("Kevin K. <kbknapp@gmail.com>")
96 .help_template(FULL_TEMPLATE)
97 .arg(arg!(-o --option <scoption> "tests options").num_args(1..))
98 .arg(arg!(-s --subcmdarg <subcmdarg> "tests other args"))
99 .arg(arg!([scpositional] "tests positionals")),
100 )
101 }
102