Skip to content

Commit c97c906

Browse files
committed
Auto merge of #12788 - hi-rustin:rustin-patch-short-Z, r=epage
Add unsupported lowercase `-z` flag suggestion for `-Z` flag
2 parents b27f203 + d1540cc commit c97c906

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

src/bin/cargo/cli.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{anyhow, Context as _};
22
use cargo::core::shell::Shell;
33
use cargo::core::{features, CliUnstable};
44
use cargo::{self, drop_print, drop_println, CargoResult, CliResult, Config};
5-
use clap::{Arg, ArgMatches};
5+
use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches};
66
use itertools::Itertools;
77
use std::collections::HashMap;
88
use std::ffi::OsStr;
@@ -618,15 +618,29 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
618618
.help_heading(heading::MANIFEST_OPTIONS)
619619
.global(true),
620620
)
621-
.arg_config()
622-
.arg(
623-
Arg::new("unstable-features")
624-
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
625-
.short('Z')
626-
.value_name("FLAG")
627-
.action(ArgAction::Append)
628-
.global(true),
629-
)
621+
// Better suggestion for the unsupported short config flag.
622+
.arg( Arg::new("unsupported-short-config-flag")
623+
.help("")
624+
.short('c')
625+
.value_parser(UnknownArgumentValueParser::suggest_arg("--config"))
626+
.action(ArgAction::SetTrue)
627+
.global(true)
628+
.hide(true))
629+
.arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true))
630+
// Better suggestion for the unsupported lowercase unstable feature flag.
631+
.arg( Arg::new("unsupported-lowercase-unstable-feature-flag")
632+
.help("")
633+
.short('z')
634+
.value_parser(UnknownArgumentValueParser::suggest_arg("-Z"))
635+
.action(ArgAction::SetTrue)
636+
.global(true)
637+
.hide(true))
638+
.arg(Arg::new("unstable-features")
639+
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
640+
.short('Z')
641+
.value_name("FLAG")
642+
.action(ArgAction::Append)
643+
.global(true))
630644
.subcommands(commands::builtin())
631645
}
632646

src/cargo/util/command_prelude.rs

-15
Original file line numberDiff line numberDiff line change
@@ -378,21 +378,6 @@ pub trait CommandExt: Sized {
378378
)
379379
._arg(unsupported_short_arg)
380380
}
381-
382-
fn arg_config(self) -> Self {
383-
let unsupported_short_arg = {
384-
let value_parser = UnknownArgumentValueParser::suggest_arg("--config");
385-
Arg::new("unsupported-short-config-flag")
386-
.help("")
387-
.short('c')
388-
.value_parser(value_parser)
389-
.action(ArgAction::SetTrue)
390-
.global(true)
391-
.hide(true)
392-
};
393-
self._arg(unsupported_short_arg)
394-
._arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true))
395-
}
396381
}
397382

398383
impl CommandExt for Command {

tests/testsuite/build.rs

+27
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,33 @@ fn cargo_compile_directory_not_cwd() {
244244
assert!(p.bin("foo").is_file());
245245
}
246246

247+
#[cargo_test]
248+
fn cargo_compile_with_unsupported_short_unstable_feature_flag() {
249+
let p = project()
250+
.file("Cargo.toml", &basic_bin_manifest("foo"))
251+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
252+
.file(".cargo/config.toml", &"")
253+
.build();
254+
255+
p.cargo("-zunstable-options -C foo build")
256+
.masquerade_as_nightly_cargo(&["chdir"])
257+
.cwd(p.root().parent().unwrap())
258+
.with_stderr(
259+
"\
260+
error: unexpected argument '-z' found
261+
262+
tip: a similar argument exists: '-Z'
263+
264+
Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
265+
cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]...
266+
267+
For more information, try '--help'.
268+
",
269+
)
270+
.with_status(1)
271+
.run();
272+
}
273+
247274
#[cargo_test]
248275
fn cargo_compile_directory_not_cwd_with_invalid_config() {
249276
let p = project()

0 commit comments

Comments
 (0)