Skip to content

Commit 03ee7b5

Browse files
committed
Move verbose help parsing to main
To remove a side effect (process exit) when parsing config.
1 parent 97e7252 commit 03ee7b5

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/bootstrap/src/bin/main.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ use std::str::FromStr;
1111
use std::{env, process};
1212

1313
use bootstrap::{
14-
find_recent_config_change_ids, human_readable_changes, t, Build, Config, Subcommand,
14+
find_recent_config_change_ids, human_readable_changes, t, Build, Config, Flags, Subcommand,
1515
CONFIG_CHANGE_HISTORY,
1616
};
1717

1818
fn main() {
1919
let args = env::args().skip(1).collect::<Vec<_>>();
2020
let config = Config::parse(&args);
2121

22+
if Flags::try_parse_verbose_help(&args) {
23+
return;
24+
}
25+
2226
let mut build_lock;
2327
let _build_lock_guard;
2428

src/bootstrap/src/core/config/flags.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ pub struct Flags {
183183
}
184184

185185
impl Flags {
186-
pub fn parse(args: &[String]) -> Self {
187-
let first = String::from("x.py");
188-
let it = std::iter::once(&first).chain(args.iter());
186+
/// Check if `<cmd> -h -v` was passed.
187+
/// If yes, print the available paths and return `true`.
188+
pub fn try_parse_verbose_help(args: &[String]) -> bool {
189189
// We need to check for `<cmd> -h -v`, in which case we list the paths
190190
#[derive(Parser)]
191191
#[command(disable_help_flag(true))]
@@ -198,24 +198,34 @@ impl Flags {
198198
cmd: Kind,
199199
}
200200
if let Ok(HelpVerboseOnly { help: true, verbose: 1.., cmd: subcommand }) =
201-
HelpVerboseOnly::try_parse_from(it.clone())
201+
HelpVerboseOnly::try_parse_from(normalize_args(args))
202202
{
203203
println!("NOTE: updating submodules before printing available paths");
204-
let config = Config::parse(&[String::from("build")]);
204+
let config = Config::parse(Self::parse(&[String::from("build")]));
205205
let build = Build::new(config);
206206
let paths = Builder::get_help(&build, subcommand);
207207
if let Some(s) = paths {
208208
println!("{s}");
209209
} else {
210210
panic!("No paths available for subcommand `{}`", subcommand.as_str());
211211
}
212-
crate::exit!(0);
212+
true
213+
} else {
214+
false
213215
}
216+
}
214217

215-
Flags::parse_from(it)
218+
pub fn parse(args: &[String]) -> Self {
219+
Flags::parse_from(normalize_args(args))
216220
}
217221
}
218222

223+
fn normalize_args(args: &[String]) -> Vec<String> {
224+
let first = String::from("x.py");
225+
let it = std::iter::once(first).chain(args.iter().cloned());
226+
it.collect()
227+
}
228+
219229
#[derive(Debug, Clone, Default, clap::Subcommand)]
220230
pub enum Subcommand {
221231
#[command(aliases = ["b"], long_about = "\n

src/bootstrap/src/core/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[allow(clippy::module_inception)]
22
mod config;
3-
pub(crate) mod flags;
3+
pub mod flags;
44
#[cfg(test)]
55
mod tests;
66

src/bootstrap/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod core;
4343
mod utils;
4444

4545
pub use core::builder::PathSet;
46-
pub use core::config::flags::Subcommand;
46+
pub use core::config::flags::{Flags, Subcommand};
4747
pub use core::config::Config;
4848

4949
pub use utils::change_tracker::{

0 commit comments

Comments
 (0)