Skip to content

Commit

Permalink
refactor(cli): rewrite rustup (check|default) with clap-derive
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed May 12, 2024
1 parent 8983028 commit 48c798c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
45 changes: 22 additions & 23 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ use crate::{
toolchain::{
distributable::DistributableToolchain,
names::{
maybe_resolvable_toolchainame_parser, partial_toolchain_desc_parser,
resolvable_local_toolchainame_parser, resolvable_toolchainame_parser,
CustomToolchainName, LocalToolchainName, MaybeResolvableToolchainName,
ResolvableLocalToolchainName, ResolvableToolchainName, ToolchainName,
partial_toolchain_desc_parser, resolvable_local_toolchainame_parser,
resolvable_toolchainame_parser, CustomToolchainName, LocalToolchainName,
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
ToolchainName,
},
toolchain::Toolchain,
},
Expand Down Expand Up @@ -129,6 +129,16 @@ enum RustupSubcmd {
force_non_host: bool,
},

/// Check for updates to Rust toolchains and rustup
Check,

/// Set the default toolchain
#[command(after_help = DEFAULT_HELP)]
Default {
#[arg(help = MAYBE_RESOLVABLE_TOOLCHAIN_ARG_HELP)]
toolchain: Option<MaybeResolvableToolchainName>,
},

/// Modify or query the installed toolchains
Toolchain {
#[command(subcommand)]
Expand Down Expand Up @@ -273,6 +283,8 @@ impl Rustup {
ToolchainSubcmd::Link { toolchain, path } => toolchain_link(cfg, &toolchain, &path),
ToolchainSubcmd::Uninstall { opts } => toolchain_remove(cfg, opts),
},
RustupSubcmd::Check => check_updates(cfg),
RustupSubcmd::Default { toolchain } => default_(cfg, toolchain),
}
}
}
Expand Down Expand Up @@ -349,11 +361,10 @@ pub fn main() -> Result<utils::ExitCode> {
Ok(match matches.subcommand() {
Some(s) => match s {
("dump-testament", _) => common::dump_testament()?,
("show" | "update" | "install" | "uninstall" | "toolchain", _) => {
Rustup::from_arg_matches(&matches)?.dispatch(cfg)?
}
("check", _) => check_updates(cfg)?,
("default", m) => default_(cfg, m)?,
(
"show" | "update" | "install" | "uninstall" | "toolchain" | "check" | "default",
_,
) => Rustup::from_arg_matches(&matches)?.dispatch(cfg)?,
("target", c) => match c.subcommand() {
Some(s) => match s {
("list", m) => handle_epipe(target_list(cfg, m))?,
Expand Down Expand Up @@ -459,18 +470,6 @@ pub(crate) fn cli() -> Command {
.about("Dump information about the build")
.hide(true), // Not for users, only CI
)
.subcommand(Command::new("check").about("Check for updates to Rust toolchains and rustup"))
.subcommand(
Command::new("default")
.about("Set the default toolchain")
.after_help(DEFAULT_HELP)
.arg(
Arg::new("toolchain")
.help(MAYBE_RESOLVABLE_TOOLCHAIN_ARG_HELP)
.required(false)
.value_parser(maybe_resolvable_toolchainame_parser),
),
)
.subcommand(
Command::new("target")
.about("Modify a toolchain's supported targets")
Expand Down Expand Up @@ -797,10 +796,10 @@ fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result<bool> {
}
}

fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
fn default_(cfg: &Cfg, toolchain: Option<MaybeResolvableToolchainName>) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

if let Some(toolchain) = m.get_one::<MaybeResolvableToolchainName>("toolchain") {
if let Some(toolchain) = toolchain {
match toolchain.to_owned() {
MaybeResolvableToolchainName::None => {
cfg.set_default(None)?;
Expand Down
9 changes: 0 additions & 9 deletions src/toolchain/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,6 @@ impl Display for MaybeResolvableToolchainName {
}
}

/// Thunk to avoid errors like
/// = 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`...
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
pub(crate) fn maybe_resolvable_toolchainame_parser(
value: &str,
) -> Result<MaybeResolvableToolchainName, InvalidName> {
MaybeResolvableToolchainName::try_from(value)
}

/// ResolvableToolchainName + none, for overriding default-has-a-value
/// situations in the CLI with an official toolchain name or none
#[derive(Debug, Clone)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
bin.name = "rustup"
args = ["default","--help"]
args = ["default", "--help"]
stdout = """
...
Set the default toolchain
Usage: rustup[EXE] default [toolchain]
Usage: rustup[EXE] default [TOOLCHAIN]
Arguments:
[toolchain] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
[TOOLCHAIN] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
name. For more information see `rustup help toolchain`
Options:
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ The Rust toolchain installer
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
Commands:
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
target Modify a toolchain's supported targets
component Modify a toolchain's installed components
override Modify toolchain overrides for directories
Expand All @@ -23,6 +21,8 @@ Commands:
completions Generate tab-completion scripts for your shell
show Show the active and installed toolchains or profiles
update Update Rust toolchains and rustup
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
toolchain Modify or query the installed toolchains
help Print this message or the help of the given subcommand(s)
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ The Rust toolchain installer
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
Commands:
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
target Modify a toolchain's supported targets
component Modify a toolchain's installed components
override Modify toolchain overrides for directories
Expand All @@ -23,6 +21,8 @@ Commands:
completions Generate tab-completion scripts for your shell
show Show the active and installed toolchains or profiles
update Update Rust toolchains and rustup
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
toolchain Modify or query the installed toolchains
help Print this message or the help of the given subcommand(s)
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ The Rust toolchain installer
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
Commands:
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
target Modify a toolchain's supported targets
component Modify a toolchain's installed components
override Modify toolchain overrides for directories
Expand All @@ -23,6 +21,8 @@ Commands:
completions Generate tab-completion scripts for your shell
show Show the active and installed toolchains or profiles
update Update Rust toolchains and rustup
check Check for updates to Rust toolchains and rustup
default Set the default toolchain
toolchain Modify or query the installed toolchains
help Print this message or the help of the given subcommand(s)
Expand Down

0 comments on commit 48c798c

Please sign in to comment.