Skip to content

Commit

Permalink
Auto merge of #13269 - weihanglo:zhelp, r=epage
Browse files Browse the repository at this point in the history
 feat(cli): add colors to `-Zhelp` console output
  • Loading branch information
bors committed Jan 9, 2024
2 parents c0db7f9 + dbfe427 commit 8859998
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 79 deletions.
38 changes: 17 additions & 21 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,34 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
.map(String::as_str)
== Some("help")
{
let header = style::HEADER.render();
let literal = style::LITERAL.render();
let placeholder = style::PLACEHOLDER.render();
let reset = anstyle::Reset.render();

let options = CliUnstable::help();
let non_hidden_options: Vec<(String, String)> = options
.iter()
.filter(|(_, help_message)| *help_message != HIDDEN)
.map(|(name, help)| (name.to_string(), help.to_string()))
.collect();
let longest_option = non_hidden_options
let max_length = options
.iter()
.filter(|(_, help)| *help != HIDDEN)
.map(|(option_name, _)| option_name.len())
.max()
.unwrap_or(0);
let help_lines: Vec<String> = non_hidden_options
let z_flags = options
.iter()
.map(|(option_name, option_help_message)| {
let option_name_kebab_case = option_name.replace("_", "-");
let padding = " ".repeat(longest_option - option_name.len()); // safe to subtract
format!(
" -Z {}{} -- {}",
option_name_kebab_case, padding, option_help_message
)
.filter(|(_, help)| *help != HIDDEN)
.map(|(opt, help)| {
let opt = opt.replace("_", "-");
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
})
.collect();
let joined = help_lines.join("\n");
.join("\n");
drop_println!(
config,
"
Available unstable (nightly-only) flags:
"\
{header}Available unstable (nightly-only) flags:{reset}
{}
{z_flags}
Run with 'cargo -Z [FLAG] [COMMAND]'",
joined
Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
);
if !config.nightly_features_allowed {
drop_println!(
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod help;
mod z_help;
13 changes: 13 additions & 0 deletions tests/testsuite/cargo/z_help/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cargo_test_support::curr_dir;
use cargo_test_support::prelude::*;

#[cargo_test]
fn case() {
snapbox::cmd::Command::cargo_ui()
.masquerade_as_nightly_cargo(&["-Z help"])
.args(["-Z", "help"])
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));
}
Empty file.
36 changes: 36 additions & 0 deletions tests/testsuite/cargo/z_help/stdout.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Available unstable (nightly-only) flags:

-Z allow-features Allow *only* the listed unstable features
-Z asymmetric-token Allows authenticating with asymmetric tokens
-Z avoid-dev-deps Avoid installing dev-dependencies if possible
-Z binary-dep-depinfo Track changes to dependency artifacts
-Z bindeps Allow Cargo packages to depend on bin, cdylib, and staticlib crates, and use the artifacts built by those crates
-Z build-std Enable Cargo to compile the standard library itself as part of a crate graph compilation
-Z build-std-features Configure features enabled for the standard library itself when building the standard library
-Z check-cfg Enable compile-time checking of `cfg` names/values/features
-Z codegen-backend Enable the `codegen-backend` option in profiles in .cargo/config.toml file
-Z config-include Enable the `include` key in config files
-Z direct-minimal-versions Resolve minimal dependency versions instead of maximum (direct dependencies only)
-Z doctest-xcompile Compile and run doctests for non-host target using runner config
-Z dual-proc-macros Build proc-macros for both the host and the target
-Z gc Track cache usage and "garbage collect" unused files
-Z gitoxide Use gitoxide for the given git interactions, or all of them if no argument is given
-Z host-config Enable the [host] section in the .cargo/config.toml file
-Z lints Pass `[lints]` to the linting tools
-Z minimal-versions Resolve minimal dependency versions instead of maximum
-Z msrv-policy Enable rust-version aware policy within cargo
-Z mtime-on-use Configure Cargo to update the mtime of used files
-Z no-index-update Do not update the registry index even if the cache is outdated
-Z panic-abort-tests Enable support to run tests with -Cpanic=abort
-Z profile-rustflags Enable the `rustflags` option in profiles in .cargo/config.toml file
-Z publish-timeout Enable the `publish.timeout` key in .cargo/config.toml file
-Z rustdoc-map Allow passing external documentation mappings to rustdoc
-Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies
-Z script Enable support for single-file, `.rs` packages
-Z target-applies-to-host Enable the `target-applies-to-host` key in the .cargo/config.toml file
-Z trim-paths Enable the `trim-paths` option in profiles
-Z unstable-options Allow the usage of unstable options

Run with `cargo -Z [FLAG] [COMMAND]`

See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information about these flags.
65 changes: 7 additions & 58 deletions tests/testsuite/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ fn help_external_subcommand() {
.run();
}

#[cargo_test]
fn z_flags_help() {
// Test that the output of `cargo -Z help` shows a different help screen with
// all the `-Z` flags.
cargo_process("-Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();
}

fn help_with_man(display_command: &str) {
// Build a "man" process that just echoes the contents.
let p = project()
Expand Down Expand Up @@ -169,51 +158,11 @@ fn help_alias() {

#[cargo_test]
fn alias_z_flag_help() {
cargo_process("build -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("run -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("check -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("test -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("b -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("r -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("c -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();

cargo_process("t -Z help")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();
for cmd in ["build", "run", "check", "test", "b", "r", "c", "t"] {
cargo_process(&format!("{cmd} -Z help"))
.with_stdout_contains(
" -Z allow-features[..] Allow *only* the listed unstable features",
)
.run();
}
}

0 comments on commit 8859998

Please sign in to comment.