Skip to content

Commit 675e1a3

Browse files
committed
refactor(cli): rewrite rustup override with clap-derive
1 parent 8a3eb10 commit 675e1a3

8 files changed

+83
-87
lines changed

src/cli/rustup_mode.rs

Lines changed: 68 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ enum RustupSubcmd {
156156
#[command(subcommand)]
157157
subcmd: ComponentSubcmd,
158158
},
159+
160+
/// Modify toolchain overrides for directories
161+
Override {
162+
#[command(subcommand)]
163+
subcmd: OverrideSubcmd,
164+
},
159165
}
160166

161167
#[derive(Debug, Subcommand)]
@@ -338,6 +344,40 @@ enum ComponentSubcmd {
338344
},
339345
}
340346

347+
#[derive(Debug, Subcommand)]
348+
#[command(
349+
after_help = OVERRIDE_HELP,
350+
arg_required_else_help = true,
351+
subcommand_required = true,
352+
)]
353+
enum OverrideSubcmd {
354+
/// List directory toolchain overrides
355+
List,
356+
357+
/// Set the override toolchain for a directory
358+
#[command(alias = "add")]
359+
Set {
360+
#[arg(help = RESOLVABLE_TOOLCHAIN_ARG_HELP)]
361+
toolchain: ResolvableToolchainName,
362+
363+
/// Path to the directory
364+
#[arg(long)]
365+
path: Option<PathBuf>,
366+
},
367+
368+
/// Remove the override toolchain for a directory
369+
#[command(alias = "remove", after_help = OVERRIDE_UNSET_HELP)]
370+
Unset {
371+
/// Path to the directory
372+
#[arg(long)]
373+
path: Option<PathBuf>,
374+
375+
/// Remove override toolchain for all nonexistent directories
376+
#[arg(long)]
377+
nonexistent: bool,
378+
},
379+
}
380+
341381
impl Rustup {
342382
fn dispatch(self, cfg: &mut Cfg) -> Result<utils::ExitCode> {
343383
match self.subcmd {
@@ -398,6 +438,15 @@ impl Rustup {
398438
target,
399439
} => component_remove(cfg, component, toolchain, target.as_deref()),
400440
},
441+
RustupSubcmd::Override { subcmd } => match subcmd {
442+
OverrideSubcmd::List => handle_epipe(common::list_overrides(cfg)),
443+
OverrideSubcmd::Set { toolchain, path } => {
444+
override_add(cfg, toolchain, path.as_deref())
445+
}
446+
OverrideSubcmd::Unset { path, nonexistent } => {
447+
override_remove(cfg, path.as_deref(), nonexistent)
448+
}
449+
},
401450
}
402451
}
403452
}
@@ -476,18 +525,9 @@ pub fn main() -> Result<utils::ExitCode> {
476525
("dump-testament", _) => common::dump_testament()?,
477526
(
478527
"show" | "update" | "install" | "uninstall" | "toolchain" | "check" | "default"
479-
| "target" | "component",
528+
| "target" | "component" | "override",
480529
_,
481530
) => Rustup::from_arg_matches(&matches)?.dispatch(cfg)?,
482-
("override", c) => match c.subcommand() {
483-
Some(s) => match s {
484-
("list", _) => handle_epipe(common::list_overrides(cfg))?,
485-
("set", m) => override_add(cfg, m)?,
486-
("unset", m) => override_remove(cfg, m)?,
487-
_ => unreachable!(),
488-
},
489-
None => unreachable!(),
490-
},
491531
("run", m) => run(cfg, m)?,
492532
("which", m) => which(cfg, m)?,
493533
("doc", m) => doc(cfg, m)?,
@@ -566,50 +606,6 @@ pub(crate) fn cli() -> Command {
566606
.about("Dump information about the build")
567607
.hide(true), // Not for users, only CI
568608
)
569-
.subcommand(
570-
Command::new("override")
571-
.about("Modify toolchain overrides for directories")
572-
.after_help(OVERRIDE_HELP)
573-
.subcommand_required(true)
574-
.arg_required_else_help(true)
575-
.subcommand(Command::new("list").about("List directory toolchain overrides"))
576-
.subcommand(
577-
Command::new("set")
578-
.about("Set the override toolchain for a directory")
579-
.alias("add")
580-
.arg(
581-
Arg::new("toolchain")
582-
.help(RESOLVABLE_TOOLCHAIN_ARG_HELP)
583-
.required(true)
584-
.num_args(1)
585-
.value_parser(resolvable_toolchainame_parser),
586-
)
587-
.arg(
588-
Arg::new("path")
589-
.long("path")
590-
.num_args(1)
591-
.help("Path to the directory"),
592-
),
593-
)
594-
.subcommand(
595-
Command::new("unset")
596-
.about("Remove the override toolchain for a directory")
597-
.after_help(OVERRIDE_UNSET_HELP)
598-
.alias("remove")
599-
.arg(
600-
Arg::new("path")
601-
.long("path")
602-
.num_args(1)
603-
.help("Path to the directory"),
604-
)
605-
.arg(
606-
Arg::new("nonexistent")
607-
.long("nonexistent")
608-
.help("Remove override toolchain for all nonexistent directories")
609-
.action(ArgAction::SetTrue),
610-
),
611-
),
612-
)
613609
.subcommand(
614610
Command::new("run")
615611
.about("Run a command with an environment configured for a given toolchain")
@@ -1376,11 +1372,14 @@ fn toolchain_remove(cfg: &mut Cfg, opts: UninstallOpts) -> Result<utils::ExitCod
13761372
Ok(utils::ExitCode(0))
13771373
}
13781374

1379-
fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1380-
let toolchain_name = m.get_one::<ResolvableToolchainName>("toolchain").unwrap();
1381-
let toolchain_name = toolchain_name.resolve(&cfg.get_default_host_triple()?)?;
1375+
fn override_add(
1376+
cfg: &Cfg,
1377+
toolchain: ResolvableToolchainName,
1378+
path: Option<&Path>,
1379+
) -> Result<utils::ExitCode> {
1380+
let toolchain_name = toolchain.resolve(&cfg.get_default_host_triple()?)?;
13821381

1383-
let path = if let Some(path) = m.get_one::<String>("path") {
1382+
let path = if let Some(path) = path {
13841383
PathBuf::from(path)
13851384
} else {
13861385
utils::current_dir()?
@@ -1415,39 +1414,36 @@ fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
14151414
Ok(utils::ExitCode(0))
14161415
}
14171416

1418-
fn override_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1419-
let paths = if m.get_flag("nonexistent") {
1417+
fn override_remove(cfg: &Cfg, path: Option<&Path>, nonexistent: bool) -> Result<utils::ExitCode> {
1418+
let paths = if nonexistent {
14201419
let list: Vec<_> = cfg.settings_file.with(|s| {
14211420
Ok(s.overrides
14221421
.iter()
14231422
.filter_map(|(k, _)| {
1424-
if Path::new(k).is_dir() {
1425-
None
1426-
} else {
1427-
Some(k.clone())
1428-
}
1423+
let path = Path::new(k);
1424+
(!path.is_dir()).then(|| path.to_owned())
14291425
})
14301426
.collect())
14311427
})?;
14321428
if list.is_empty() {
14331429
info!("no nonexistent paths detected");
14341430
}
14351431
list
1436-
} else if let Some(path) = m.get_one::<String>("path") {
1432+
} else if let Some(path) = path {
14371433
vec![path.to_owned()]
14381434
} else {
1439-
vec![utils::current_dir()?.to_str().unwrap().to_string()]
1435+
vec![utils::current_dir()?]
14401436
};
14411437

1442-
for path in paths {
1438+
for p in &paths {
14431439
if cfg
14441440
.settings_file
1445-
.with_mut(|s| Ok(s.remove_override(Path::new(&path), cfg.notify_handler.as_ref())))?
1441+
.with_mut(|s| Ok(s.remove_override(p, cfg.notify_handler.as_ref())))?
14461442
{
1447-
info!("override toolchain for '{}' removed", path);
1443+
info!("override toolchain for '{}' removed", p.display());
14481444
} else {
1449-
info!("no override toolchain for '{}'", path);
1450-
if m.get_one::<String>("path").is_none() && !m.get_flag("nonexistent") {
1445+
info!("no override toolchain for '{}'", p.display());
1446+
if path.is_none() && !nonexistent {
14511447
info!(
14521448
"you may use `--path <path>` option to remove override toolchain \
14531449
for a specific path"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
override Modify toolchain overrides for directories
1312
run Run a command with an environment configured for a given toolchain
1413
which Display which binary will be run for a given command
1514
doc Open the documentation for the current toolchain
@@ -24,6 +23,7 @@ Commands:
2423
toolchain Modify or query the installed toolchains
2524
target Modify a toolchain's supported targets
2625
component Modify a toolchain's installed components
26+
override Modify toolchain overrides for directories
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
override Modify toolchain overrides for directories
1312
run Run a command with an environment configured for a given toolchain
1413
which Display which binary will be run for a given command
1514
doc Open the documentation for the current toolchain
@@ -24,6 +23,7 @@ Commands:
2423
toolchain Modify or query the installed toolchains
2524
target Modify a toolchain's supported targets
2625
component Modify a toolchain's installed components
26+
override Modify toolchain overrides for directories
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
override Modify toolchain overrides for directories
1312
run Run a command with an environment configured for a given toolchain
1413
which Display which binary will be run for a given command
1514
doc Open the documentation for the current toolchain
@@ -24,6 +23,7 @@ Commands:
2423
toolchain Modify or query the installed toolchains
2524
target Modify a toolchain's supported targets
2625
component Modify a toolchain's installed components
26+
override Modify toolchain overrides for directories
2727
help Print this message or the help of the given subcommand(s)
2828
2929
Arguments:
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
bin.name = "rustup"
2-
args = ["override","add","--help"]
2+
args = ["override", "add", "--help"]
33
stdout = """
44
...
55
Set the override toolchain for a directory
66
7-
Usage: rustup[EXE] override set [OPTIONS] <toolchain>
7+
Usage: rustup[EXE] override set [OPTIONS] <TOOLCHAIN>
88
99
Arguments:
10-
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
10+
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
1111
more information see `rustup help toolchain`
1212
1313
Options:
14-
--path <path> Path to the directory
14+
--path <PATH> Path to the directory
1515
-h, --help Print help
1616
"""
1717
stderr = ""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
bin.name = "rustup"
2-
args = ["override","unset","--help"]
2+
args = ["override", "remove", "--help"]
33
stdout = """
44
...
55
Remove the override toolchain for a directory
66
77
Usage: rustup[EXE] override unset [OPTIONS]
88
99
Options:
10-
--path <path> Path to the directory
10+
--path <PATH> Path to the directory
1111
--nonexistent Remove override toolchain for all nonexistent directories
1212
-h, --help Print help
1313
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
bin.name = "rustup"
2-
args = ["override","set","--help"]
2+
args = ["override", "set", "--help"]
33
stdout = """
44
...
55
Set the override toolchain for a directory
66
7-
Usage: rustup[EXE] override set [OPTIONS] <toolchain>
7+
Usage: rustup[EXE] override set [OPTIONS] <TOOLCHAIN>
88
99
Arguments:
10-
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
10+
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
1111
more information see `rustup help toolchain`
1212
1313
Options:
14-
--path <path> Path to the directory
14+
--path <PATH> Path to the directory
1515
-h, --help Print help
1616
"""
1717
stderr = ""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
bin.name = "rustup"
2-
args = ["override","unset","--help"]
2+
args = ["override", "unset", "--help"]
33
stdout = """
44
...
55
Remove the override toolchain for a directory
66
77
Usage: rustup[EXE] override unset [OPTIONS]
88
99
Options:
10-
--path <path> Path to the directory
10+
--path <PATH> Path to the directory
1111
--nonexistent Remove override toolchain for all nonexistent directories
1212
-h, --help Print help
1313

0 commit comments

Comments
 (0)