Skip to content

Commit 83d4206

Browse files
committed
test: Update fixture to cover all cases + styling
1 parent b13274d commit 83d4206

File tree

6 files changed

+197
-29
lines changed

6 files changed

+197
-29
lines changed

src/bin/stdio-fixture.rs

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,105 @@
1+
use clap::{builder::PossibleValue, Arg, ArgAction, Command};
2+
13
fn main() {
24
#[allow(unused_mut)]
3-
let mut cmd = clap::Command::new("stdio-fixture")
5+
let mut cmd = Command::new("stdio-fixture")
46
.version("1.0")
57
.long_version("1.0 - a2132c")
68
.arg_required_else_help(true)
7-
.subcommand(clap::Command::new("more"))
9+
.subcommand(Command::new("more"))
10+
.subcommand(
11+
Command::new("test")
12+
.visible_alias("do-stuff")
13+
.long_about("Subcommand with one visible alias"),
14+
)
15+
.subcommand(
16+
Command::new("test_2")
17+
.visible_aliases(["do-other-stuff", "tests"])
18+
.about("several visible aliases")
19+
.long_about("Subcommand with multiple visible aliases"),
20+
)
21+
.subcommand(
22+
Command::new("test_3")
23+
.long_flag("test")
24+
.about("several visible long flag aliases")
25+
.visible_long_flag_aliases(["testing", "testall", "test_all"]),
26+
)
27+
.subcommand(
28+
Command::new("test_4")
29+
.short_flag('t')
30+
.about("several visible short flag aliases")
31+
.visible_short_flag_aliases(['q', 'w']),
32+
)
33+
.subcommand(
34+
Command::new("test_5")
35+
.short_flag('e')
36+
.long_flag("test-hdr")
37+
.about("all kinds of visible aliases")
38+
.visible_aliases(["tests_4k"])
39+
.visible_long_flag_aliases(["thetests", "t4k"])
40+
.visible_short_flag_aliases(['r', 'y']),
41+
)
842
.arg(
9-
clap::Arg::new("verbose")
43+
Arg::new("verbose")
1044
.long("verbose")
1145
.help("log")
12-
.action(clap::ArgAction::SetTrue)
46+
.action(ArgAction::SetTrue)
1347
.long_help("more log"),
48+
)
49+
.arg(
50+
Arg::new("config")
51+
.action(ArgAction::Set)
52+
.help("Speed configuration")
53+
.short('c')
54+
.long("config")
55+
.value_name("MODE")
56+
.value_parser([
57+
PossibleValue::new("fast"),
58+
PossibleValue::new("slow").help("slower than fast"),
59+
PossibleValue::new("secret speed").hide(true),
60+
])
61+
.default_value("fast"),
62+
)
63+
.arg(
64+
Arg::new("name")
65+
.action(ArgAction::Set)
66+
.help("App name")
67+
.long_help("Set the instance app name")
68+
.value_name("NAME")
69+
.visible_alias("app-name")
70+
.default_value("clap"),
71+
)
72+
.arg(
73+
Arg::new("fruits")
74+
.short('f')
75+
.visible_short_alias('b')
76+
.action(ArgAction::Append)
77+
.value_name("FRUITS")
78+
.help("List of fruits")
79+
.default_values(["apple", "banane", "orange"]),
1480
);
81+
#[cfg(feature = "env")]
82+
{
83+
cmd = cmd.arg(
84+
Arg::new("env_arg")
85+
.help("Read from env var when arg is not present.")
86+
.value_name("ENV")
87+
.env("ENV_ARG"),
88+
);
89+
}
1590
#[cfg(feature = "color")]
1691
{
17-
use clap::builder::styling;
18-
const STYLES: styling::Styles = styling::Styles::styled()
19-
.header(styling::AnsiColor::Green.on_default().bold())
20-
.usage(styling::AnsiColor::Green.on_default().bold())
21-
.literal(styling::AnsiColor::Blue.on_default().bold())
22-
.placeholder(styling::AnsiColor::Cyan.on_default());
92+
use clap::builder::styling::{AnsiColor, Styles};
93+
const STYLES: Styles = Styles::styled()
94+
.header(AnsiColor::Green.on_default().bold())
95+
.error(AnsiColor::Red.on_default().bold())
96+
.usage(AnsiColor::Green.on_default().bold().underline())
97+
.literal(AnsiColor::Blue.on_default().bold())
98+
.placeholder(AnsiColor::Cyan.on_default())
99+
.valid(AnsiColor::Green.on_default())
100+
.invalid(AnsiColor::Magenta.on_default().bold())
101+
.context(AnsiColor::Yellow.on_default().dimmed())
102+
.context_value(AnsiColor::Yellow.on_default().italic());
23103
cmd = cmd.styles(STYLES);
24104
}
25105
cmd.get_matches();

tests/ui/arg_required_else_help_stderr.toml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ args = []
33
status.code = 2
44
stdout = ""
55
stderr = """
6-
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
6+
Usage: stdio-fixture[EXE] [OPTIONS] [NAME] [ENV] [COMMAND]
77
88
Commands:
9-
more
10-
help Print this message or the help of the given subcommand(s)
9+
more
10+
test Subcommand with one visible alias [aliases: do-stuff]
11+
test_2 several visible aliases [aliases: do-other-stuff, tests]
12+
test_3, --test several visible long flag aliases
13+
test_4, -t several visible short flag aliases [aliases: -q, -w]
14+
test_5, -e, --test-hdr all kinds of visible aliases [aliases: -r, -y, tests_4k]
15+
help Print this message or the help of the given subcommand(s)
16+
17+
Arguments:
18+
[NAME] App name [default: clap] [aliases: --app-name]
19+
[ENV] Read from env var when arg is not present. [env: ENV_ARG=]
1120
1221
Options:
13-
--verbose log
14-
-h, --help Print help (see more with '--help')
15-
-V, --version Print version
22+
--verbose log
23+
-c, --config <MODE> Speed configuration [default: fast] [possible values: fast, slow]
24+
-f <FRUITS> List of fruits [default: apple banane orange] [aliases: -b]
25+
-h, --help Print help (see more with '--help')
26+
-V, --version Print version
1627
"""

tests/ui/error_stderr.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ stdout = ""
55
stderr = """
66
error: unexpected argument '--unknown-argument' found
77
8-
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
8+
tip: to pass '--unknown-argument' as a value, use '-- --unknown-argument'
9+
10+
Usage: stdio-fixture[EXE] [OPTIONS] [NAME] [ENV] [COMMAND]
911
1012
For more information, try '--help'.
1113
"""

tests/ui/h_flag_stdout.toml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@ bin.name = "stdio-fixture"
22
args = ["-h"]
33
status.code = 0
44
stdout = """
5-
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
5+
Usage: stdio-fixture[EXE] [OPTIONS] [NAME] [ENV] [COMMAND]
66
77
Commands:
8-
more
9-
help Print this message or the help of the given subcommand(s)
8+
more
9+
test Subcommand with one visible alias [aliases: do-stuff]
10+
test_2 several visible aliases [aliases: do-other-stuff, tests]
11+
test_3, --test several visible long flag aliases
12+
test_4, -t several visible short flag aliases [aliases: -q, -w]
13+
test_5, -e, --test-hdr all kinds of visible aliases [aliases: -r, -y, tests_4k]
14+
help Print this message or the help of the given subcommand(s)
15+
16+
Arguments:
17+
[NAME] App name [default: clap] [aliases: --app-name]
18+
[ENV] Read from env var when arg is not present. [env: ENV_ARG=]
1019
1120
Options:
12-
--verbose log
13-
-h, --help Print help (see more with '--help')
14-
-V, --version Print version
21+
--verbose log
22+
-c, --config <MODE> Speed configuration [default: fast] [possible values: fast, slow]
23+
-f <FRUITS> List of fruits [default: apple banane orange] [aliases: -b]
24+
-h, --help Print help (see more with '--help')
25+
-V, --version Print version
1526
"""
1627
stderr = ""

tests/ui/help_cmd_stdout.toml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,48 @@ bin.name = "stdio-fixture"
22
args = ["help"]
33
status.code = 0
44
stdout = """
5-
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
5+
Usage: stdio-fixture[EXE] [OPTIONS] [NAME] [ENV] [COMMAND]
66
77
Commands:
8-
more
9-
help Print this message or the help of the given subcommand(s)
8+
more
9+
test Subcommand with one visible alias [aliases: do-stuff]
10+
test_2 several visible aliases [aliases: do-other-stuff, tests]
11+
test_3, --test several visible long flag aliases
12+
test_4, -t several visible short flag aliases [aliases: -q, -w]
13+
test_5, -e, --test-hdr all kinds of visible aliases [aliases: -r, -y, tests_4k]
14+
help Print this message or the help of the given subcommand(s)
15+
16+
Arguments:
17+
[NAME]
18+
Set the instance app name
19+
20+
[default: clap]
21+
[aliases: --app-name]
22+
23+
[ENV]
24+
Read from env var when arg is not present.
25+
26+
[env: ENV_ARG=]
1027
1128
Options:
1229
--verbose
1330
more log
1431
32+
-c, --config <MODE>
33+
Speed configuration
34+
35+
[default: fast]
36+
37+
Possible values:
38+
- fast
39+
- slow: slower than fast
40+
41+
-f <FRUITS>
42+
List of fruits
43+
44+
[default: apple banane orange]
45+
[aliases: -b]
46+
1547
-h, --help
1648
Print help (see a summary with '-h')
1749

tests/ui/help_flag_stdout.toml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,48 @@ bin.name = "stdio-fixture"
22
args = ["--help"]
33
status.code = 0
44
stdout = """
5-
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
5+
Usage: stdio-fixture[EXE] [OPTIONS] [NAME] [ENV] [COMMAND]
66
77
Commands:
8-
more
9-
help Print this message or the help of the given subcommand(s)
8+
more
9+
test Subcommand with one visible alias [aliases: do-stuff]
10+
test_2 several visible aliases [aliases: do-other-stuff, tests]
11+
test_3, --test several visible long flag aliases
12+
test_4, -t several visible short flag aliases [aliases: -q, -w]
13+
test_5, -e, --test-hdr all kinds of visible aliases [aliases: -r, -y, tests_4k]
14+
help Print this message or the help of the given subcommand(s)
15+
16+
Arguments:
17+
[NAME]
18+
Set the instance app name
19+
20+
[default: clap]
21+
[aliases: --app-name]
22+
23+
[ENV]
24+
Read from env var when arg is not present.
25+
26+
[env: ENV_ARG=]
1027
1128
Options:
1229
--verbose
1330
more log
1431
32+
-c, --config <MODE>
33+
Speed configuration
34+
35+
[default: fast]
36+
37+
Possible values:
38+
- fast
39+
- slow: slower than fast
40+
41+
-f <FRUITS>
42+
List of fruits
43+
44+
[default: apple banane orange]
45+
[aliases: -b]
46+
1547
-h, --help
1648
Print help (see a summary with '-h')
1749

0 commit comments

Comments
 (0)