Skip to content

Commit 8859998

Browse files
committed
Auto merge of #13269 - weihanglo:zhelp, r=epage
feat(cli): add colors to `-Zhelp` console output
2 parents c0db7f9 + dbfe427 commit 8859998

File tree

6 files changed

+74
-79
lines changed

6 files changed

+74
-79
lines changed

src/bin/cargo/cli.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,34 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
5555
.map(String::as_str)
5656
== Some("help")
5757
{
58+
let header = style::HEADER.render();
59+
let literal = style::LITERAL.render();
60+
let placeholder = style::PLACEHOLDER.render();
61+
let reset = anstyle::Reset.render();
62+
5863
let options = CliUnstable::help();
59-
let non_hidden_options: Vec<(String, String)> = options
60-
.iter()
61-
.filter(|(_, help_message)| *help_message != HIDDEN)
62-
.map(|(name, help)| (name.to_string(), help.to_string()))
63-
.collect();
64-
let longest_option = non_hidden_options
64+
let max_length = options
6565
.iter()
66+
.filter(|(_, help)| *help != HIDDEN)
6667
.map(|(option_name, _)| option_name.len())
6768
.max()
6869
.unwrap_or(0);
69-
let help_lines: Vec<String> = non_hidden_options
70+
let z_flags = options
7071
.iter()
71-
.map(|(option_name, option_help_message)| {
72-
let option_name_kebab_case = option_name.replace("_", "-");
73-
let padding = " ".repeat(longest_option - option_name.len()); // safe to subtract
74-
format!(
75-
" -Z {}{} -- {}",
76-
option_name_kebab_case, padding, option_help_message
77-
)
72+
.filter(|(_, help)| *help != HIDDEN)
73+
.map(|(opt, help)| {
74+
let opt = opt.replace("_", "-");
75+
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
7876
})
79-
.collect();
80-
let joined = help_lines.join("\n");
77+
.join("\n");
8178
drop_println!(
8279
config,
83-
"
84-
Available unstable (nightly-only) flags:
80+
"\
81+
{header}Available unstable (nightly-only) flags:{reset}
8582
86-
{}
83+
{z_flags}
8784
88-
Run with 'cargo -Z [FLAG] [COMMAND]'",
89-
joined
85+
Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
9086
);
9187
if !config.nightly_features_allowed {
9288
drop_println!(

tests/testsuite/cargo/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod help;
2+
mod z_help;

tests/testsuite/cargo/z_help/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use cargo_test_support::curr_dir;
2+
use cargo_test_support::prelude::*;
3+
4+
#[cargo_test]
5+
fn case() {
6+
snapbox::cmd::Command::cargo_ui()
7+
.masquerade_as_nightly_cargo(&["-Z help"])
8+
.args(["-Z", "help"])
9+
.assert()
10+
.success()
11+
.stdout_matches_path(curr_dir!().join("stdout.log"))
12+
.stderr_matches_path(curr_dir!().join("stderr.log"));
13+
}

tests/testsuite/cargo/z_help/stderr.log

Whitespace-only changes.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Available unstable (nightly-only) flags:
2+
3+
-Z allow-features Allow *only* the listed unstable features
4+
-Z asymmetric-token Allows authenticating with asymmetric tokens
5+
-Z avoid-dev-deps Avoid installing dev-dependencies if possible
6+
-Z binary-dep-depinfo Track changes to dependency artifacts
7+
-Z bindeps Allow Cargo packages to depend on bin, cdylib, and staticlib crates, and use the artifacts built by those crates
8+
-Z build-std Enable Cargo to compile the standard library itself as part of a crate graph compilation
9+
-Z build-std-features Configure features enabled for the standard library itself when building the standard library
10+
-Z check-cfg Enable compile-time checking of `cfg` names/values/features
11+
-Z codegen-backend Enable the `codegen-backend` option in profiles in .cargo/config.toml file
12+
-Z config-include Enable the `include` key in config files
13+
-Z direct-minimal-versions Resolve minimal dependency versions instead of maximum (direct dependencies only)
14+
-Z doctest-xcompile Compile and run doctests for non-host target using runner config
15+
-Z dual-proc-macros Build proc-macros for both the host and the target
16+
-Z gc Track cache usage and "garbage collect" unused files
17+
-Z gitoxide Use gitoxide for the given git interactions, or all of them if no argument is given
18+
-Z host-config Enable the [host] section in the .cargo/config.toml file
19+
-Z lints Pass `[lints]` to the linting tools
20+
-Z minimal-versions Resolve minimal dependency versions instead of maximum
21+
-Z msrv-policy Enable rust-version aware policy within cargo
22+
-Z mtime-on-use Configure Cargo to update the mtime of used files
23+
-Z no-index-update Do not update the registry index even if the cache is outdated
24+
-Z panic-abort-tests Enable support to run tests with -Cpanic=abort
25+
-Z profile-rustflags Enable the `rustflags` option in profiles in .cargo/config.toml file
26+
-Z publish-timeout Enable the `publish.timeout` key in .cargo/config.toml file
27+
-Z rustdoc-map Allow passing external documentation mappings to rustdoc
28+
-Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies
29+
-Z script Enable support for single-file, `.rs` packages
30+
-Z target-applies-to-host Enable the `target-applies-to-host` key in the .cargo/config.toml file
31+
-Z trim-paths Enable the `trim-paths` option in profiles
32+
-Z unstable-options Allow the usage of unstable options
33+
34+
Run with `cargo -Z [FLAG] [COMMAND]`
35+
36+
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information about these flags.

tests/testsuite/help.rs

+7-58
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ fn help_external_subcommand() {
4343
.run();
4444
}
4545

46-
#[cargo_test]
47-
fn z_flags_help() {
48-
// Test that the output of `cargo -Z help` shows a different help screen with
49-
// all the `-Z` flags.
50-
cargo_process("-Z help")
51-
.with_stdout_contains(
52-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
53-
)
54-
.run();
55-
}
56-
5746
fn help_with_man(display_command: &str) {
5847
// Build a "man" process that just echoes the contents.
5948
let p = project()
@@ -169,51 +158,11 @@ fn help_alias() {
169158

170159
#[cargo_test]
171160
fn alias_z_flag_help() {
172-
cargo_process("build -Z help")
173-
.with_stdout_contains(
174-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
175-
)
176-
.run();
177-
178-
cargo_process("run -Z help")
179-
.with_stdout_contains(
180-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
181-
)
182-
.run();
183-
184-
cargo_process("check -Z help")
185-
.with_stdout_contains(
186-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
187-
)
188-
.run();
189-
190-
cargo_process("test -Z help")
191-
.with_stdout_contains(
192-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
193-
)
194-
.run();
195-
196-
cargo_process("b -Z help")
197-
.with_stdout_contains(
198-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
199-
)
200-
.run();
201-
202-
cargo_process("r -Z help")
203-
.with_stdout_contains(
204-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
205-
)
206-
.run();
207-
208-
cargo_process("c -Z help")
209-
.with_stdout_contains(
210-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
211-
)
212-
.run();
213-
214-
cargo_process("t -Z help")
215-
.with_stdout_contains(
216-
" -Z allow-features[..]-- Allow *only* the listed unstable features",
217-
)
218-
.run();
161+
for cmd in ["build", "run", "check", "test", "b", "r", "c", "t"] {
162+
cargo_process(&format!("{cmd} -Z help"))
163+
.with_stdout_contains(
164+
" -Z allow-features[..] Allow *only* the listed unstable features",
165+
)
166+
.run();
167+
}
219168
}

0 commit comments

Comments
 (0)