Skip to content

Commit ad85ef4

Browse files
committed
refactor(cli): Shrink main
I'm going to add more `config_configure`s and this will make it more obvious that these are done in early returns.
1 parent d325f9b commit ad85ef4

File tree

1 file changed

+105
-97
lines changed

1 file changed

+105
-97
lines changed

src/bin/cargo/cli.rs

Lines changed: 105 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -53,50 +53,7 @@ pub fn main(gctx: &mut GlobalContext) -> CliResult {
5353
.map(String::as_str)
5454
== Some("help")
5555
{
56-
let header = style::HEADER;
57-
let literal = style::LITERAL;
58-
let placeholder = style::PLACEHOLDER;
59-
60-
let options = CliUnstable::help();
61-
let max_length = options
62-
.iter()
63-
.filter(|(_, help)| help.is_some())
64-
.map(|(option_name, _)| option_name.len())
65-
.max()
66-
.unwrap_or(0);
67-
let z_flags = options
68-
.iter()
69-
.filter(|(_, help)| help.is_some())
70-
.map(|(opt, help)| {
71-
let opt = opt.replace("_", "-");
72-
let help = help.unwrap();
73-
format!(" {literal}-Z {opt:<max_length$}{literal:#} {help}")
74-
})
75-
.join("\n");
76-
drop_println!(
77-
gctx,
78-
"\
79-
{header}Available unstable (nightly-only) flags:{header:#}
80-
81-
{z_flags}
82-
83-
Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder:#}`",
84-
);
85-
if !gctx.nightly_features_allowed {
86-
drop_println!(
87-
gctx,
88-
"\nUnstable flags are only available on the nightly channel \
89-
of Cargo, but this is the `{}` channel.\n\
90-
{}",
91-
features::channel(),
92-
features::SEE_CHANNELS
93-
);
94-
}
95-
drop_println!(
96-
gctx,
97-
"\nSee https://doc.rust-lang.org/nightly/cargo/reference/unstable.html \
98-
for more information about these flags."
99-
);
56+
print_zhelp(gctx);
10057
return Ok(());
10158
}
10259

@@ -114,59 +71,7 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder
11471
}
11572

11673
if expanded_args.flag("list") {
117-
// Maps from commonly known external commands (not builtin to cargo)
118-
// to their description, for the help page. Reserved for external
119-
// subcommands that are core within the rust ecosystem (esp ones that
120-
// might become internal in the future).
121-
let known_external_command_descriptions = HashMap::from([
122-
(
123-
"clippy",
124-
"Checks a package to catch common mistakes and improve your Rust code.",
125-
),
126-
(
127-
"fmt",
128-
"Formats all bin and lib files of the current crate using rustfmt.",
129-
),
130-
]);
131-
drop_println!(
132-
gctx,
133-
color_print::cstr!("<green,bold>Installed Commands:</>")
134-
);
135-
for (name, command) in list_commands(gctx) {
136-
let known_external_desc = known_external_command_descriptions.get(name.as_str());
137-
let literal = style::LITERAL;
138-
match command {
139-
CommandInfo::BuiltIn { about } => {
140-
assert!(
141-
known_external_desc.is_none(),
142-
"known_external_commands shouldn't contain builtin `{name}`",
143-
);
144-
let summary = about.unwrap_or_default();
145-
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
146-
drop_println!(gctx, " {literal}{name:<20}{literal:#} {summary}");
147-
}
148-
CommandInfo::External { path } => {
149-
if let Some(desc) = known_external_desc {
150-
drop_println!(gctx, " {literal}{name:<20}{literal:#} {desc}");
151-
} else if is_verbose {
152-
drop_println!(
153-
gctx,
154-
" {literal}{name:<20}{literal:#} {}",
155-
path.display()
156-
);
157-
} else {
158-
drop_println!(gctx, " {literal}{name}{literal:#}");
159-
}
160-
}
161-
CommandInfo::Alias { target } => {
162-
drop_println!(
163-
gctx,
164-
" {literal}{name:<20}{literal:#} alias: {}",
165-
target.iter().join(" ")
166-
);
167-
}
168-
}
169-
}
74+
print_list(gctx, is_verbose);
17075
return Ok(());
17176
}
17277

@@ -185,6 +90,109 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder
18590
exec.exec(gctx, subcommand_args)
18691
}
18792

93+
fn print_zhelp(gctx: &GlobalContext) {
94+
let header = style::HEADER;
95+
let literal = style::LITERAL;
96+
let placeholder = style::PLACEHOLDER;
97+
98+
let options = CliUnstable::help();
99+
let max_length = options
100+
.iter()
101+
.filter(|(_, help)| help.is_some())
102+
.map(|(option_name, _)| option_name.len())
103+
.max()
104+
.unwrap_or(0);
105+
let z_flags = options
106+
.iter()
107+
.filter(|(_, help)| help.is_some())
108+
.map(|(opt, help)| {
109+
let opt = opt.replace("_", "-");
110+
let help = help.unwrap();
111+
format!(" {literal}-Z {opt:<max_length$}{literal:#} {help}")
112+
})
113+
.join("\n");
114+
drop_println!(
115+
gctx,
116+
"\
117+
{header}Available unstable (nightly-only) flags:{header:#}
118+
119+
{z_flags}
120+
121+
Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder:#}`",
122+
);
123+
if !gctx.nightly_features_allowed {
124+
drop_println!(
125+
gctx,
126+
"\nUnstable flags are only available on the nightly channel \
127+
of Cargo, but this is the `{}` channel.\n\
128+
{}",
129+
features::channel(),
130+
features::SEE_CHANNELS
131+
);
132+
}
133+
drop_println!(
134+
gctx,
135+
"\nSee https://doc.rust-lang.org/nightly/cargo/reference/unstable.html \
136+
for more information about these flags."
137+
);
138+
}
139+
140+
fn print_list(gctx: &GlobalContext, is_verbose: bool) {
141+
// Maps from commonly known external commands (not builtin to cargo)
142+
// to their description, for the help page. Reserved for external
143+
// subcommands that are core within the rust ecosystem (esp ones that
144+
// might become internal in the future).
145+
let known_external_command_descriptions = HashMap::from([
146+
(
147+
"clippy",
148+
"Checks a package to catch common mistakes and improve your Rust code.",
149+
),
150+
(
151+
"fmt",
152+
"Formats all bin and lib files of the current crate using rustfmt.",
153+
),
154+
]);
155+
drop_println!(
156+
gctx,
157+
color_print::cstr!("<green,bold>Installed Commands:</>")
158+
);
159+
for (name, command) in list_commands(gctx) {
160+
let known_external_desc = known_external_command_descriptions.get(name.as_str());
161+
let literal = style::LITERAL;
162+
match command {
163+
CommandInfo::BuiltIn { about } => {
164+
assert!(
165+
known_external_desc.is_none(),
166+
"known_external_commands shouldn't contain builtin `{name}`",
167+
);
168+
let summary = about.unwrap_or_default();
169+
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
170+
drop_println!(gctx, " {literal}{name:<20}{literal:#} {summary}");
171+
}
172+
CommandInfo::External { path } => {
173+
if let Some(desc) = known_external_desc {
174+
drop_println!(gctx, " {literal}{name:<20}{literal:#} {desc}");
175+
} else if is_verbose {
176+
drop_println!(
177+
gctx,
178+
" {literal}{name:<20}{literal:#} {}",
179+
path.display()
180+
);
181+
} else {
182+
drop_println!(gctx, " {literal}{name}{literal:#}");
183+
}
184+
}
185+
CommandInfo::Alias { target } => {
186+
drop_println!(
187+
gctx,
188+
" {literal}{name:<20}{literal:#} alias: {}",
189+
target.iter().join(" ")
190+
);
191+
}
192+
}
193+
}
194+
}
195+
188196
pub fn get_version_string(is_verbose: bool) -> String {
189197
let version = cargo::version();
190198
let mut version_string = format!("cargo {}\n", version);

0 commit comments

Comments
 (0)