From 0108d5b0dca4dd2039bb6bac1f0d0351eeb6ba56 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sat, 7 Oct 2023 20:21:07 +0800 Subject: [PATCH 1/3] Add test for unsupported short unstable feature flag Signed-off-by: hi-rustin --- tests/testsuite/build.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index bd4b43b4266..7ba4dcbeb14 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -244,6 +244,31 @@ fn cargo_compile_directory_not_cwd() { assert!(p.bin("foo").is_file()); } +#[cargo_test] +fn cargo_compile_with_unsupported_short_unstable_feature_flag() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .file(".cargo/config.toml", &"") + .build(); + + p.cargo("-zunstable-options -C foo build") + .masquerade_as_nightly_cargo(&["chdir"]) + .cwd(p.root().parent().unwrap()) + .with_stderr( + "\ +error: unexpected argument '-z' found + +Usage: cargo [+toolchain] [OPTIONS] [COMMAND] + cargo [+toolchain] [OPTIONS] -Zscript [ARGS]... + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn cargo_compile_directory_not_cwd_with_invalid_config() { let p = project() From f9719dcf4e5f59da2e7b3ab94fee977683b194b7 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sat, 7 Oct 2023 20:25:21 +0800 Subject: [PATCH 2/3] add unsupported lowercase flag suggestion for -Z flag Signed-off-by: hi-rustin --- src/bin/cargo/cli.rs | 9 +-------- src/cargo/util/command_prelude.rs | 22 ++++++++++++++++++++++ tests/testsuite/build.rs | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 7c0e3b5ee85..3189a789c58 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -619,14 +619,7 @@ See 'cargo help <>' for more information on a sp .global(true), ) .arg_config() - .arg( - Arg::new("unstable-features") - .help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details") - .short('Z') - .value_name("FLAG") - .action(ArgAction::Append) - .global(true), - ) + .arg_unstable_feature() .subcommands(commands::builtin()) } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index bfc2c45cd05..09857b76f58 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -393,6 +393,28 @@ pub trait CommandExt: Sized { self._arg(unsupported_short_arg) ._arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) } + + fn arg_unstable_feature(self) -> Self { + let unsupported_short_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("-Z"); + Arg::new("unsupported-lowercase-unstable-feature-flag") + .help("") + .short('z') + .value_parser(value_parser) + .action(ArgAction::SetTrue) + .global(true) + .hide(true) + }; + self._arg( + Arg::new("unstable-features") + .help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details") + .short('Z') + .value_name("FLAG") + .action(ArgAction::Append) + .global(true), + ) + ._arg(unsupported_short_arg) + } } impl CommandExt for Command { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 7ba4dcbeb14..77cdd538eef 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -259,6 +259,8 @@ fn cargo_compile_with_unsupported_short_unstable_feature_flag() { "\ error: unexpected argument '-z' found + tip: a similar argument exists: '-Z' + Usage: cargo [+toolchain] [OPTIONS] [COMMAND] cargo [+toolchain] [OPTIONS] -Zscript [ARGS]... From d1540ccd7ed25ec69503a958e40ae9698adf6752 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 10 Oct 2023 08:44:17 +0800 Subject: [PATCH 3/3] Move arg_config and arg_unstable_feature out of command prelude Signed-off-by: hi-rustin --- src/bin/cargo/cli.rs | 27 +++++++++++++++++++--- src/cargo/util/command_prelude.rs | 37 ------------------------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 3189a789c58..a21030f0141 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Context as _}; use cargo::core::shell::Shell; use cargo::core::{features, CliUnstable}; use cargo::{self, drop_print, drop_println, CargoResult, CliResult, Config}; -use clap::{Arg, ArgMatches}; +use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches}; use itertools::Itertools; use std::collections::HashMap; use std::ffi::OsStr; @@ -618,8 +618,29 @@ See 'cargo help <>' for more information on a sp .help_heading(heading::MANIFEST_OPTIONS) .global(true), ) - .arg_config() - .arg_unstable_feature() + // Better suggestion for the unsupported short config flag. + .arg( Arg::new("unsupported-short-config-flag") + .help("") + .short('c') + .value_parser(UnknownArgumentValueParser::suggest_arg("--config")) + .action(ArgAction::SetTrue) + .global(true) + .hide(true)) + .arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) + // Better suggestion for the unsupported lowercase unstable feature flag. + .arg( Arg::new("unsupported-lowercase-unstable-feature-flag") + .help("") + .short('z') + .value_parser(UnknownArgumentValueParser::suggest_arg("-Z")) + .action(ArgAction::SetTrue) + .global(true) + .hide(true)) + .arg(Arg::new("unstable-features") + .help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details") + .short('Z') + .value_name("FLAG") + .action(ArgAction::Append) + .global(true)) .subcommands(commands::builtin()) } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 09857b76f58..a8b8e31c7eb 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -378,43 +378,6 @@ pub trait CommandExt: Sized { ) ._arg(unsupported_short_arg) } - - fn arg_config(self) -> Self { - let unsupported_short_arg = { - let value_parser = UnknownArgumentValueParser::suggest_arg("--config"); - Arg::new("unsupported-short-config-flag") - .help("") - .short('c') - .value_parser(value_parser) - .action(ArgAction::SetTrue) - .global(true) - .hide(true) - }; - self._arg(unsupported_short_arg) - ._arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) - } - - fn arg_unstable_feature(self) -> Self { - let unsupported_short_arg = { - let value_parser = UnknownArgumentValueParser::suggest_arg("-Z"); - Arg::new("unsupported-lowercase-unstable-feature-flag") - .help("") - .short('z') - .value_parser(value_parser) - .action(ArgAction::SetTrue) - .global(true) - .hide(true) - }; - self._arg( - Arg::new("unstable-features") - .help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details") - .short('Z') - .value_name("FLAG") - .action(ArgAction::Append) - .global(true), - ) - ._arg(unsupported_short_arg) - } } impl CommandExt for Command {