Skip to content

Commit 8a3eb10

Browse files
committed
refactor(cli): rewrite rustup component with clap-derive
1 parent 20cdc5c commit 8a3eb10

7 files changed

+104
-86
lines changed

src/cli/rustup_mode.rs

Lines changed: 91 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ enum RustupSubcmd {
150150
#[command(subcommand)]
151151
subcmd: TargetSubcmd,
152152
},
153+
154+
/// Modify a toolchain's installed components
155+
Component {
156+
#[command(subcommand)]
157+
subcmd: ComponentSubcmd,
158+
},
153159
}
154160

155161
#[derive(Debug, Subcommand)]
@@ -294,6 +300,44 @@ enum TargetSubcmd {
294300
},
295301
}
296302

303+
#[derive(Debug, Subcommand)]
304+
#[command(arg_required_else_help = true, subcommand_required = true)]
305+
enum ComponentSubcmd {
306+
/// List installed and available components
307+
List {
308+
#[arg(long, help = OFFICIAL_TOOLCHAIN_ARG_HELP)]
309+
toolchain: Option<PartialToolchainDesc>,
310+
311+
/// List only installed components
312+
#[arg(long)]
313+
installed: bool,
314+
},
315+
316+
/// Add a component to a Rust toolchain
317+
Add {
318+
#[arg(required = true, num_args = 1..)]
319+
component: Vec<String>,
320+
321+
#[arg(long, help = OFFICIAL_TOOLCHAIN_ARG_HELP)]
322+
toolchain: Option<PartialToolchainDesc>,
323+
324+
#[arg(long)]
325+
target: Option<String>,
326+
},
327+
328+
/// Remove a component from a Rust toolchain
329+
Remove {
330+
#[arg(required = true, num_args = 1..)]
331+
component: Vec<String>,
332+
333+
#[arg(long, help = OFFICIAL_TOOLCHAIN_ARG_HELP)]
334+
toolchain: Option<PartialToolchainDesc>,
335+
336+
#[arg(long)]
337+
target: Option<String>,
338+
},
339+
}
340+
297341
impl Rustup {
298342
fn dispatch(self, cfg: &mut Cfg) -> Result<utils::ExitCode> {
299343
match self.subcmd {
@@ -338,6 +382,22 @@ impl Rustup {
338382
TargetSubcmd::Add { target, toolchain } => target_add(cfg, target, toolchain),
339383
TargetSubcmd::Remove { target, toolchain } => target_remove(cfg, target, toolchain),
340384
},
385+
RustupSubcmd::Component { subcmd } => match subcmd {
386+
ComponentSubcmd::List {
387+
toolchain,
388+
installed,
389+
} => handle_epipe(component_list(cfg, toolchain, installed)),
390+
ComponentSubcmd::Add {
391+
component,
392+
toolchain,
393+
target,
394+
} => component_add(cfg, component, toolchain, target.as_deref()),
395+
ComponentSubcmd::Remove {
396+
component,
397+
toolchain,
398+
target,
399+
} => component_remove(cfg, component, toolchain, target.as_deref()),
400+
},
341401
}
342402
}
343403
}
@@ -416,18 +476,9 @@ pub fn main() -> Result<utils::ExitCode> {
416476
("dump-testament", _) => common::dump_testament()?,
417477
(
418478
"show" | "update" | "install" | "uninstall" | "toolchain" | "check" | "default"
419-
| "target",
479+
| "target" | "component",
420480
_,
421481
) => Rustup::from_arg_matches(&matches)?.dispatch(cfg)?,
422-
("component", c) => match c.subcommand() {
423-
Some(s) => match s {
424-
("list", m) => handle_epipe(component_list(cfg, m))?,
425-
("add", m) => component_add(cfg, m)?,
426-
("remove", m) => component_remove(cfg, m)?,
427-
_ => unreachable!(),
428-
},
429-
None => unreachable!(),
430-
},
431482
("override", c) => match c.subcommand() {
432483
Some(s) => match s {
433484
("list", _) => handle_epipe(common::list_overrides(cfg))?,
@@ -515,55 +566,6 @@ pub(crate) fn cli() -> Command {
515566
.about("Dump information about the build")
516567
.hide(true), // Not for users, only CI
517568
)
518-
.subcommand(
519-
Command::new("component")
520-
.about("Modify a toolchain's installed components")
521-
.subcommand_required(true)
522-
.arg_required_else_help(true)
523-
.subcommand(
524-
Command::new("list")
525-
.about("List installed and available components")
526-
.arg(
527-
Arg::new("toolchain")
528-
.help(OFFICIAL_TOOLCHAIN_ARG_HELP)
529-
.long("toolchain")
530-
.num_args(1)
531-
.value_parser(partial_toolchain_desc_parser),
532-
)
533-
.arg(
534-
Arg::new("installed")
535-
.long("installed")
536-
.help("List only installed components")
537-
.action(ArgAction::SetTrue),
538-
),
539-
)
540-
.subcommand(
541-
Command::new("add")
542-
.about("Add a component to a Rust toolchain")
543-
.arg(Arg::new("component").required(true).num_args(1..))
544-
.arg(
545-
Arg::new("toolchain")
546-
.help(OFFICIAL_TOOLCHAIN_ARG_HELP)
547-
.long("toolchain")
548-
.num_args(1)
549-
.value_parser(partial_toolchain_desc_parser),
550-
)
551-
.arg(Arg::new("target").long("target").num_args(1)),
552-
)
553-
.subcommand(
554-
Command::new("remove")
555-
.about("Remove a component from a Rust toolchain")
556-
.arg(Arg::new("component").required(true).num_args(1..))
557-
.arg(
558-
Arg::new("toolchain")
559-
.help(OFFICIAL_TOOLCHAIN_ARG_HELP)
560-
.long("toolchain")
561-
.num_args(1)
562-
.value_parser(partial_toolchain_desc_parser),
563-
)
564-
.arg(Arg::new("target").long("target").num_args(1)),
565-
),
566-
)
567569
.subcommand(
568570
Command::new("override")
569571
.about("Modify toolchain overrides for directories")
@@ -1260,40 +1262,56 @@ fn target_remove(
12601262
Ok(utils::ExitCode(0))
12611263
}
12621264

1263-
fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1264-
let toolchain = explicit_desc_or_dir_toolchain_old(cfg, m)?;
1265+
fn component_list(
1266+
cfg: &Cfg,
1267+
toolchain: Option<PartialToolchainDesc>,
1268+
installed_only: bool,
1269+
) -> Result<utils::ExitCode> {
1270+
let toolchain = explicit_desc_or_dir_toolchain(cfg, toolchain)?;
12651271
// downcasting required because the toolchain files can name any toolchain
12661272
let distributable = (&toolchain).try_into()?;
1267-
common::list_components(distributable, m.get_flag("installed"))?;
1273+
common::list_components(distributable, installed_only)?;
12681274
Ok(utils::ExitCode(0))
12691275
}
12701276

1271-
fn component_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1272-
let toolchain = explicit_desc_or_dir_toolchain_old(cfg, m)?;
1277+
fn component_add(
1278+
cfg: &Cfg,
1279+
components: Vec<String>,
1280+
toolchain: Option<PartialToolchainDesc>,
1281+
target: Option<&str>,
1282+
) -> Result<utils::ExitCode> {
1283+
let toolchain = explicit_desc_or_dir_toolchain(cfg, toolchain)?;
12731284
let distributable = DistributableToolchain::try_from(&toolchain)?;
1274-
let target = get_target(m, &distributable);
1285+
let target = get_target(target, &distributable);
12751286

1276-
for component in m.get_many::<String>("component").unwrap() {
1287+
for component in &components {
12771288
let new_component = Component::try_new(component, &distributable, target.as_ref())?;
12781289
distributable.add_component(new_component)?;
12791290
}
12801291

12811292
Ok(utils::ExitCode(0))
12821293
}
12831294

1284-
fn get_target(m: &ArgMatches, distributable: &DistributableToolchain<'_>) -> Option<TargetTriple> {
1285-
m.get_one::<String>("target")
1286-
.map(|s| &**s)
1295+
fn get_target(
1296+
target: Option<&str>,
1297+
distributable: &DistributableToolchain<'_>,
1298+
) -> Option<TargetTriple> {
1299+
target
12871300
.map(TargetTriple::new)
12881301
.or_else(|| Some(distributable.desc().target.clone()))
12891302
}
12901303

1291-
fn component_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1292-
let toolchain = explicit_desc_or_dir_toolchain_old(cfg, m)?;
1304+
fn component_remove(
1305+
cfg: &Cfg,
1306+
components: Vec<String>,
1307+
toolchain: Option<PartialToolchainDesc>,
1308+
target: Option<&str>,
1309+
) -> Result<utils::ExitCode> {
1310+
let toolchain = explicit_desc_or_dir_toolchain(cfg, toolchain)?;
12931311
let distributable = DistributableToolchain::try_from(&toolchain)?;
1294-
let target = get_target(m, &distributable);
1312+
let target = get_target(target, &distributable);
12951313

1296-
for component in m.get_many::<String>("component").unwrap() {
1314+
for component in &components {
12971315
let new_component = Component::try_new(component, &distributable, target.as_ref())?;
12981316
distributable.remove_component(new_component)?;
12991317
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ stdout = """
44
...
55
Add a component to a Rust toolchain
66
7-
Usage: rustup[EXE] component add [OPTIONS] <component>...
7+
Usage: rustup[EXE] component add [OPTIONS] <COMPONENT>...
88
99
Arguments:
10-
<component>...
10+
<COMPONENT>...
1111
1212
Options:
13-
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
13+
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
1414
information see `rustup help toolchain`
15-
--target <target>
15+
--target <TARGET>
1616
-h, --help Print help
1717
"""
1818
stderr = ""

tests/suite/cli-ui/rustup/rustup_component_cmd_list_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 = ["component","list","--help"]
2+
args = ["component", "list", "--help"]
33
stdout = """
44
...
55
List installed and available components
66
77
Usage: rustup[EXE] component list [OPTIONS]
88
99
Options:
10-
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
10+
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
1111
information see `rustup help toolchain`
1212
--installed List only installed components
1313
-h, --help Print help

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ stdout = """
44
...
55
Remove a component from a Rust toolchain
66
7-
Usage: rustup[EXE] component remove [OPTIONS] <component>...
7+
Usage: rustup[EXE] component remove [OPTIONS] <COMPONENT>...
88
99
Arguments:
10-
<component>...
10+
<COMPONENT>...
1111
1212
Options:
13-
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
13+
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
1414
information see `rustup help toolchain`
15-
--target <target>
15+
--target <TARGET>
1616
-h, --help Print help
1717
"""
1818
stderr = ""

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-
component Modify a toolchain's installed components
1312
override Modify toolchain overrides for directories
1413
run Run a command with an environment configured for a given toolchain
1514
which Display which binary will be run for a given command
@@ -24,6 +23,7 @@ Commands:
2423
default Set the default toolchain
2524
toolchain Modify or query the installed toolchains
2625
target Modify a toolchain's supported targets
26+
component Modify a toolchain's installed components
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-
component Modify a toolchain's installed components
1312
override Modify toolchain overrides for directories
1413
run Run a command with an environment configured for a given toolchain
1514
which Display which binary will be run for a given command
@@ -24,6 +23,7 @@ Commands:
2423
default Set the default toolchain
2524
toolchain Modify or query the installed toolchains
2625
target Modify a toolchain's supported targets
26+
component Modify a toolchain's installed components
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-
component Modify a toolchain's installed components
1312
override Modify toolchain overrides for directories
1413
run Run a command with an environment configured for a given toolchain
1514
which Display which binary will be run for a given command
@@ -24,6 +23,7 @@ Commands:
2423
default Set the default toolchain
2524
toolchain Modify or query the installed toolchains
2625
target Modify a toolchain's supported targets
26+
component Modify a toolchain's installed components
2727
help Print this message or the help of the given subcommand(s)
2828
2929
Arguments:

0 commit comments

Comments
 (0)