Skip to content

Commit 7c171af

Browse files
committed
refactor(cli): rewrite rustup (run|which|dump-testament) with clap-derive
1 parent 675e1a3 commit 7c171af

File tree

7 files changed

+62
-87
lines changed

7 files changed

+62
-87
lines changed

src/cli/rustup_mode.rs

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use crate::{
3737
toolchain::{
3838
distributable::DistributableToolchain,
3939
names::{
40-
partial_toolchain_desc_parser, resolvable_local_toolchainame_parser,
41-
resolvable_toolchainame_parser, CustomToolchainName, LocalToolchainName,
40+
partial_toolchain_desc_parser, CustomToolchainName, LocalToolchainName,
4241
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
4342
ToolchainName,
4443
},
@@ -95,6 +94,10 @@ enum RustupSubcmd {
9594
opts: UninstallOpts,
9695
},
9796

97+
/// Dump information about the build
98+
#[command(hide = true)]
99+
DumpTestament,
100+
98101
/// Show the active and installed toolchains or profiles
99102
#[command(after_help = SHOW_HELP)]
100103
Show {
@@ -162,6 +165,28 @@ enum RustupSubcmd {
162165
#[command(subcommand)]
163166
subcmd: OverrideSubcmd,
164167
},
168+
169+
/// Run a command with an environment configured for a given toolchain
170+
#[command(after_help = RUN_HELP, trailing_var_arg = true)]
171+
Run {
172+
#[arg(help = RESOLVABLE_LOCAL_TOOLCHAIN_ARG_HELP)]
173+
toolchain: ResolvableLocalToolchainName,
174+
175+
#[arg(required = true, num_args = 1.., use_value_delimiter = false)]
176+
command: Vec<String>,
177+
178+
/// Install the requested toolchain if needed
179+
#[arg(long)]
180+
install: bool,
181+
},
182+
183+
/// Display which binary will be run for a given command
184+
Which {
185+
command: String,
186+
187+
#[arg(long, help = RESOLVABLE_TOOLCHAIN_ARG_HELP)]
188+
toolchain: Option<ResolvableToolchainName>,
189+
},
165190
}
166191

167192
#[derive(Debug, Subcommand)]
@@ -381,6 +406,7 @@ enum OverrideSubcmd {
381406
impl Rustup {
382407
fn dispatch(self, cfg: &mut Cfg) -> Result<utils::ExitCode> {
383408
match self.subcmd {
409+
RustupSubcmd::DumpTestament => common::dump_testament(),
384410
RustupSubcmd::Install { opts } => update(cfg, opts),
385411
RustupSubcmd::Uninstall { opts } => toolchain_remove(cfg, opts),
386412
RustupSubcmd::Show { verbose, subcmd } => match subcmd {
@@ -447,6 +473,12 @@ impl Rustup {
447473
override_remove(cfg, path.as_deref(), nonexistent)
448474
}
449475
},
476+
RustupSubcmd::Run {
477+
toolchain,
478+
command,
479+
install,
480+
} => run(cfg, toolchain, command, install),
481+
RustupSubcmd::Which { command, toolchain } => which(cfg, &command, toolchain),
450482
}
451483
}
452484
}
@@ -522,14 +554,11 @@ pub fn main() -> Result<utils::ExitCode> {
522554

523555
Ok(match matches.subcommand() {
524556
Some(s) => match s {
525-
("dump-testament", _) => common::dump_testament()?,
526557
(
527-
"show" | "update" | "install" | "uninstall" | "toolchain" | "check" | "default"
528-
| "target" | "component" | "override",
558+
"dump-testament" | "show" | "update" | "install" | "uninstall" | "toolchain"
559+
| "check" | "default" | "target" | "component" | "override" | "run" | "which",
529560
_,
530561
) => Rustup::from_arg_matches(&matches)?.dispatch(cfg)?,
531-
("run", m) => run(cfg, m)?,
532-
("which", m) => which(cfg, m)?,
533562
("doc", m) => doc(cfg, m)?,
534563
#[cfg(not(windows))]
535564
("man", m) => man(cfg, m)?,
@@ -601,48 +630,6 @@ pub(crate) fn cli() -> Command {
601630
}
602631
}),
603632
)
604-
.subcommand(
605-
Command::new("dump-testament")
606-
.about("Dump information about the build")
607-
.hide(true), // Not for users, only CI
608-
)
609-
.subcommand(
610-
Command::new("run")
611-
.about("Run a command with an environment configured for a given toolchain")
612-
.after_help(RUN_HELP)
613-
.trailing_var_arg(true)
614-
.arg(
615-
Arg::new("toolchain")
616-
.help(RESOLVABLE_LOCAL_TOOLCHAIN_ARG_HELP)
617-
.required(true)
618-
.num_args(1)
619-
.value_parser(resolvable_local_toolchainame_parser),
620-
)
621-
.arg(
622-
Arg::new("command")
623-
.required(true)
624-
.num_args(1..)
625-
.use_value_delimiter(false),
626-
)
627-
.arg(
628-
Arg::new("install")
629-
.help("Install the requested toolchain if needed")
630-
.long("install")
631-
.action(ArgAction::SetTrue),
632-
),
633-
)
634-
.subcommand(
635-
Command::new("which")
636-
.about("Display which binary will be run for a given command")
637-
.arg(Arg::new("command").required(true))
638-
.arg(
639-
Arg::new("toolchain")
640-
.help(RESOLVABLE_TOOLCHAIN_ARG_HELP)
641-
.long("toolchain")
642-
.num_args(1)
643-
.value_parser(resolvable_toolchainame_parser),
644-
),
645-
)
646633
.subcommand(
647634
Command::new("doc")
648635
.alias("docs")
@@ -965,22 +952,25 @@ fn update(cfg: &mut Cfg, opts: UpdateOpts) -> Result<utils::ExitCode> {
965952
Ok(utils::ExitCode(0))
966953
}
967954

968-
fn run(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
969-
let toolchain = m
970-
.get_one::<ResolvableLocalToolchainName>("toolchain")
971-
.unwrap();
972-
let args = m.get_many::<String>("command").unwrap();
973-
let args: Vec<_> = args.collect();
955+
fn run(
956+
cfg: &Cfg,
957+
toolchain: ResolvableLocalToolchainName,
958+
command: Vec<String>,
959+
install: bool,
960+
) -> Result<utils::ExitCode> {
974961
let toolchain = toolchain.resolve(&cfg.get_default_host_triple()?)?;
975-
let cmd = cfg.create_command_for_toolchain(&toolchain, m.get_flag("install"), args[0])?;
962+
let cmd = cfg.create_command_for_toolchain(&toolchain, install, &command[0])?;
976963

977-
let code = command::run_command_for_dir(cmd, args[0], &args[1..])?;
964+
let code = command::run_command_for_dir(cmd, &command[0], &command[1..])?;
978965
Ok(code)
979966
}
980967

981-
fn which(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
982-
let binary = m.get_one::<String>("command").unwrap();
983-
let binary_path = if let Some(toolchain) = m.get_one::<ResolvableToolchainName>("toolchain") {
968+
fn which(
969+
cfg: &Cfg,
970+
binary: &str,
971+
toolchain: Option<ResolvableToolchainName>,
972+
) -> Result<utils::ExitCode> {
973+
let binary_path = if let Some(toolchain) = toolchain {
984974
let desc = toolchain.resolve(&cfg.get_default_host_triple()?)?;
985975
Toolchain::new(cfg, desc.into())?.binary_file(binary)
986976
} else {

src/toolchain/names.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,6 @@ impl Display for ResolvableToolchainName {
185185
}
186186
}
187187

188-
/// Thunk to avoid errors like
189-
/// = note: `fn(&'2 str) -> Result<CustomToolchainName, <CustomToolchainName as TryFrom<&'2 str>>::Error> {<CustomToolchainName as TryFrom<&'2 str>>::try_from}` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
190-
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
191-
pub(crate) fn resolvable_toolchainame_parser(
192-
value: &str,
193-
) -> Result<ResolvableToolchainName, InvalidName> {
194-
ResolvableToolchainName::try_from(value)
195-
}
196-
197188
/// A toolchain name from user input. MaybeToolchainName accepts 'none' or a
198189
/// custom or resolvable official name. Possibly this should be an Option with a
199190
/// local trait for our needs.
@@ -366,12 +357,6 @@ impl Display for ResolvableLocalToolchainName {
366357
}
367358
}
368359

369-
pub(crate) fn resolvable_local_toolchainame_parser(
370-
value: &str,
371-
) -> Result<ResolvableLocalToolchainName, InvalidName> {
372-
ResolvableLocalToolchainName::try_from(value)
373-
}
374-
375360
/// LocalToolchainName can be used in calls to Cfg that alter configuration,
376361
/// like setting overrides, or that depend on configuration, like calculating
377362
/// the toolchain directory. It is not used to model the RUSTUP_TOOLCHAIN

tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
run Run a command with an environment configured for a given toolchain
13-
which Display which binary will be run for a given command
1412
doc Open the documentation for the current toolchain
1513
...
1614
self Modify the rustup installation
@@ -24,6 +22,8 @@ Commands:
2422
target Modify a toolchain's supported targets
2523
component Modify a toolchain's installed components
2624
override Modify toolchain overrides for directories
25+
run Run a command with an environment configured for a given toolchain
26+
which Display which binary will be run for a given command
2727
help Print this message or the help of the given subcommand(s)
2828
2929
Arguments:

tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
run Run a command with an environment configured for a given toolchain
13-
which Display which binary will be run for a given command
1412
doc Open the documentation for the current toolchain
1513
...
1614
self Modify the rustup installation
@@ -24,6 +22,8 @@ Commands:
2422
target Modify a toolchain's supported targets
2523
component Modify a toolchain's installed components
2624
override Modify toolchain overrides for directories
25+
run Run a command with an environment configured for a given toolchain
26+
which Display which binary will be run for a given command
2727
help Print this message or the help of the given subcommand(s)
2828
2929
Arguments:

tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
run Run a command with an environment configured for a given toolchain
13-
which Display which binary will be run for a given command
1412
doc Open the documentation for the current toolchain
1513
...
1614
self Modify the rustup installation
@@ -24,6 +22,8 @@ Commands:
2422
target Modify a toolchain's supported targets
2523
component Modify a toolchain's installed components
2624
override Modify toolchain overrides for directories
25+
run Run a command with an environment configured for a given toolchain
26+
which Display which binary will be run for a given command
2727
help Print this message or the help of the given subcommand(s)
2828
2929
Arguments:

tests/suite/cli-ui/rustup/rustup_run_cmd_help_flag_stdout.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ stdout = """
55
...
66
Run a command with an environment configured for a given toolchain
77
8-
Usage: rustup[EXE] run [OPTIONS] <toolchain> <command>...
8+
Usage: rustup[EXE] run [OPTIONS] <TOOLCHAIN> <COMMAND>...
99
1010
Arguments:
11-
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name, or
11+
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name, or
1212
an absolute path. For more information see `rustup help toolchain`
13-
<command>...
13+
<COMMAND>...
1414
1515
Options:
1616
--install Install the requested toolchain if needed

tests/suite/cli-ui/rustup/rustup_which_cmd_help_flag_stdout.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ stdout = """
55
...
66
Display which binary will be run for a given command
77
8-
Usage: rustup[EXE] which [OPTIONS] <command>
8+
Usage: rustup[EXE] which [OPTIONS] <COMMAND>
99
1010
Arguments:
11-
<command>
11+
<COMMAND>
1212
1313
Options:
14-
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom
14+
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom
1515
toolchain name. For more information see `rustup help toolchain`
1616
-h, --help Print help
1717
"""

0 commit comments

Comments
 (0)