Skip to content

Commit

Permalink
Fix --quiet being used with nested subcommands.
Browse files Browse the repository at this point in the history
This fixes an issue where `--quiet` doesn't work with commands that have
subcommands. This is because `config_configure` only looks at the global
and top-level subcommand, and not deeper subcommands. The issue was that
`--quiet` was not defined as a global flag. This was changed in
rust-lang#6358 in order to give a better
help message for `cargo test --quiet`. I don't remember if clap just
didn't support overriding at the time, or if we just didn't know how it
worked. Anyways, it seems to work to override it now, so I think it
should be fine to mark it as global.

This should bring in `--quiet` more in-line with how `--verbose` works.
This means that `--quiet` is now accepted with `cargo report`,
`cargo help`, and `cargo config`.

This also fixes `--quiet` with `cargo clean gc`.

This should also help with supporting `--quiet` with the new `cargo
owner` subcommands being added in
rust-lang#11879.

Fixes rust-lang#12957
  • Loading branch information
ehuss committed Nov 12, 2023
1 parent 9a1b092 commit 80ffb1d
Show file tree
Hide file tree
Showing 74 changed files with 140 additions and 83 deletions.
6 changes: 5 additions & 1 deletion crates/xtask-bump-check/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ pub fn cli() -> clap::Command {
.action(ArgAction::Count)
.global(true),
)
.arg_quiet()
.arg(
flag("quiet", "Do not print cargo log messages")
.short('q')
.global(true),
)
.arg(
opt("color", "Coloring: auto, always, never")
.value_name("WHEN")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
.action(ArgAction::Count)
.global(true),
)
.arg_quiet()
.arg(flag("quiet", "Do not print cargo log messages").short('q').global(true))
.arg(
opt("color", "Coloring: auto, always, never")
.value_name("WHEN")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Example uses:
.arg_manifest_path_without_unsupported_path_tip()
.arg_package("Package to modify")
.arg_dry_run("Don't actually write the manifest")
.arg_quiet()
.arg_silent_suggestion()
.next_help_heading("Source")
.args([
clap::Arg::new("path")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn cli() -> Command {
))
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec(
"Package to run benchmarks for",
"Benchmark all packages in the workspace",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn cli() -> Command {
.arg_ignore_rust_version()
.arg_future_incompat_report()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec(
"Package to build (see `cargo help pkgid`)",
"Build all packages in the workspace",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn cli() -> Command {
.arg_ignore_rust_version()
.arg_future_incompat_report()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec(
"Package(s) to check",
"Check all packages in the workspace",
Expand Down
5 changes: 2 additions & 3 deletions src/bin/cargo/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn cli() -> Command {
subcommand("clean")
.about("Remove artifacts that cargo has generated in the past")
.arg_doc("Whether or not to clean just the documentation directory")
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec_simple("Package to clean artifacts for")
.arg_release("Whether or not to clean release artifacts")
.arg_profile("Clean artifacts of the specified profile")
Expand All @@ -25,8 +25,7 @@ pub fn cli() -> Command {
subcommand("gc")
.about("Clean global caches")
.hide(true)
// FIXME: arg_quiet doesn't work because `config_configure`
// doesn't know about subcommands.
.arg_silent_suggestion()
.arg_dry_run("Display what would be deleted without deleting anything")
// NOTE: Not all of these options may get stabilized. Some of them are
// very low-level details, and may not be something typical users need.
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn cli() -> Command {
.arg(flag("document-private-items", "Document private items"))
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec(
"Package to document",
"Document all packages in the workspace",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cargo::ops::FetchOptions;
pub fn cli() -> Command {
subcommand("fetch")
.about("Fetch dependencies of a package from the network")
.arg_quiet()
.arg_silent_suggestion()
.arg_target_triple("Fetch dependencies for the target triple")
.arg_manifest_path()
.after_help(color_print::cstr!(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn cli() -> Command {
))
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec(
"Package(s) to fix",
"Fix all packages in the workspace",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cargo::ops;
pub fn cli() -> Command {
subcommand("generate-lockfile")
.about("Generate the lockfile for a package")
.arg_quiet()
.arg_silent_suggestion()
.arg_manifest_path()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help generate-lockfile</>` for more detailed information.\n"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn cli() -> Command {
)
.arg_new_opts()
.arg_registry("Registry to use")
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help init</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn cli() -> Command {
))
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_targets_bins_examples(
"Install only the specified binary",
"Install all binaries",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/locate_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn cli() -> Command {
)
.value_name("FMT"),
)
.arg_quiet()
.arg_silent_suggestion()
.arg_manifest_path()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help locate-project</>` for more detailed information.\n"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn cli() -> Command {
.num_args(0..)
.last(true),
)
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help login</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn cli() -> Command {
subcommand("logout")
.about("Remove an API token from the registry locally")
.arg_registry("Registry to use")
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help logout</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn cli() -> Command {
.value_name("VERSION")
.value_parser(["1"]),
)
.arg_quiet()
.arg_silent_suggestion()
.arg_features()
.arg_manifest_path()
.after_help(color_print::cstr!(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn cli() -> Command {
)
.arg_new_opts()
.arg_registry("Registry to use")
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help new</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn cli() -> Command {
.arg_index("Registry index URL to modify owners for")
.arg_registry("Registry to modify owners for")
.arg(opt("token", "API token to use when authenticating").value_name("TOKEN"))
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help owner</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn cli() -> Command {
"allow-dirty",
"Allow dirty working directories to be packaged",
))
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec_no_all(
"Package(s) to assemble",
"Assemble all packages in the workspace",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn cli() -> Command {
subcommand("pkgid")
.about("Print a fully qualified package specification")
.arg(Arg::new("spec").value_name("SPEC").action(ArgAction::Set))
.arg_quiet()
.arg_silent_suggestion()
.arg_package("Argument to get the package ID specifier for")
.arg_manifest_path()
.after_help(color_print::cstr!(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn cli() -> Command {
"allow-dirty",
"Allow dirty working directories to be packaged",
))
.arg_quiet()
.arg_silent_suggestion()
.arg_package("Package to publish")
.arg_features()
.arg_parallel()
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/read_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Print a JSON representation of a Cargo.toml manifest.
Deprecated, use `<cyan,bold>cargo metadata --no-deps</>` instead.\
"
))
.arg_quiet()
.arg_silent_suggestion()
.arg_manifest_path()
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn cli() -> clap::Command {
.value_name("DEP_ID")
.help("Dependencies to be removed")])
.arg_dry_run("Don't actually write the manifest")
.arg_quiet()
.arg_silent_suggestion()
.next_help_heading("Section")
.args([
clap::Arg::new("dev")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn cli() -> Command {
)
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package("Package with the target to run")
.arg_targets_bin_example(
"Name of the bin target to run",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn cli() -> Command {
.arg_future_incompat_report()
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package("Package to build")
.arg_targets_all(
"Build only this package's library",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn cli() -> Command {
))
.arg_ignore_rust_version()
.arg_message_format()
.arg_quiet()
.arg_silent_suggestion()
.arg_package("Package to document")
.arg_targets_all(
"Build only this package's library",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn cli() -> Command {
)
.arg_index("Registry index URL to search packages in")
.arg_registry("Registry to search packages in")
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help search</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn cli() -> Command {
.short('a')
.hide(true),
)
.arg_quiet()
.arg_silent_suggestion()
.arg(flag("no-dev-dependencies", "Deprecated, use -e=no-dev instead").hide(true))
.arg(
multi_opt(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn cli() -> Command {
.about("Remove a Rust binary")
.arg(Arg::new("spec").value_name("SPEC").num_args(0..))
.arg(opt("root", "Directory to uninstall packages from").value_name("DIR"))
.arg_quiet()
.arg_silent_suggestion()
.arg_package_spec_simple("Package to uninstall")
.arg(
multi_opt("bin", "NAME", "Only uninstall the binary NAME")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn cli() -> Command {
.value_name("PRECISE")
.requires("package-group"),
)
.arg_quiet()
.arg_silent_suggestion()
.arg(
flag("workspace", "Only update the workspace packages")
.short('w')
Expand Down
1 change: 0 additions & 1 deletion src/bin/cargo/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub fn cli() -> Command {
.arg(unsupported("relative-path"))
.arg(unsupported("only-git-deps"))
.arg(unsupported("disallow-duplicates"))
.arg_quiet_without_unknown_silent_arg_tip()
.arg_manifest_path()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help vendor</>` for more detailed information.\n"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/verify_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process;
pub fn cli() -> Command {
subcommand("verify-project")
.about("Check correctness of crate manifest")
.arg_quiet()
.arg_silent_suggestion()
.arg_manifest_path()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help verify-project</>` for more detailed information.\n"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::command_prelude::*;
pub fn cli() -> Command {
subcommand("version")
.about("Show version information")
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help version</>` for more detailed information.\n"
))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn cli() -> Command {
.arg_index("Registry index URL to yank from")
.arg_registry("Registry to yank from")
.arg(opt("token", "API token to use when authenticating").value_name("TOKEN"))
.arg_quiet()
.arg_silent_suggestion()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help yank</>` for more detailed information.\n"
))
Expand Down
21 changes: 10 additions & 11 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,19 @@ pub trait CommandExt: Sized {
))
}

fn arg_quiet(self) -> Self {
let unsupported_silent_arg = {
let value_parser = UnknownArgumentValueParser::suggest_arg("--quiet");
/// Adds a suggestion for the `--silent` or `-s` flags to use the
/// `--quiet` flag instead. This is to help with people familiar with
/// other tools that use `-s`.
///
/// Every command should call this, unless it has its own `-s` short flag.
fn arg_silent_suggestion(self) -> Self {
let value_parser = UnknownArgumentValueParser::suggest_arg("--quiet");
self._arg(
flag("silent", "")
.short('s')
.value_parser(value_parser)
.hide(true)
};
self.arg_quiet_without_unknown_silent_arg_tip()
._arg(unsupported_silent_arg)
}

fn arg_quiet_without_unknown_silent_arg_tip(self) -> Self {
self._arg(flag("quiet", "Do not print cargo log messages").short('q'))
.hide(true),
)
}

fn arg_timings(self) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/cargo_add/help/stdout.log
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Options:
-n, --dry-run
Don't actually write the manifest

-q, --quiet
Do not print cargo log messages

-v, --verbose...
Use verbose output (-vv very verbose/build.rs output)

-q, --quiet
Do not print cargo log messages

--color <WHEN>
Coloring: auto, always, never

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_bench/help/stdout.log
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Options:
--no-fail-fast Run all benchmarks regardless of failure
--ignore-rust-version Ignore `rust-version` specification in packages
--message-format <FMT> Error format
-q, --quiet Do not print cargo log messages
-v, --verbose... Use verbose output (-vv very verbose/build.rs output)
-q, --quiet Do not print cargo log messages
--color <WHEN> Coloring: auto, always, never
--config <KEY=VALUE> Override a configuration value
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_build/help/stdout.log
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Options:
--ignore-rust-version Ignore `rust-version` specification in packages
--future-incompat-report Outputs a future incompatibility report at the end of the build
--message-format <FMT> Error format
-q, --quiet Do not print cargo log messages
-v, --verbose... Use verbose output (-vv very verbose/build.rs output)
-q, --quiet Do not print cargo log messages
--color <WHEN> Coloring: auto, always, never
--config <KEY=VALUE> Override a configuration value
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
Expand Down
Loading

0 comments on commit 80ffb1d

Please sign in to comment.