Skip to content

Commit

Permalink
Move verbose help parsing to main
Browse files Browse the repository at this point in the history
To remove a side effect (process exit) when parsing config.
  • Loading branch information
Kobzol committed Aug 9, 2024
1 parent 97e7252 commit 03ee7b5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/bootstrap/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ use std::str::FromStr;
use std::{env, process};

use bootstrap::{
find_recent_config_change_ids, human_readable_changes, t, Build, Config, Subcommand,
find_recent_config_change_ids, human_readable_changes, t, Build, Config, Flags, Subcommand,
CONFIG_CHANGE_HISTORY,
};

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

if Flags::try_parse_verbose_help(&args) {
return;
}

let mut build_lock;
let _build_lock_guard;

Expand Down
24 changes: 17 additions & 7 deletions src/bootstrap/src/core/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ pub struct Flags {
}

impl Flags {
pub fn parse(args: &[String]) -> Self {
let first = String::from("x.py");
let it = std::iter::once(&first).chain(args.iter());
/// Check if `<cmd> -h -v` was passed.
/// If yes, print the available paths and return `true`.
pub fn try_parse_verbose_help(args: &[String]) -> bool {
// We need to check for `<cmd> -h -v`, in which case we list the paths
#[derive(Parser)]
#[command(disable_help_flag(true))]
Expand All @@ -198,24 +198,34 @@ impl Flags {
cmd: Kind,
}
if let Ok(HelpVerboseOnly { help: true, verbose: 1.., cmd: subcommand }) =
HelpVerboseOnly::try_parse_from(it.clone())
HelpVerboseOnly::try_parse_from(normalize_args(args))
{
println!("NOTE: updating submodules before printing available paths");
let config = Config::parse(&[String::from("build")]);
let config = Config::parse(Self::parse(&[String::from("build")]));
let build = Build::new(config);
let paths = Builder::get_help(&build, subcommand);
if let Some(s) = paths {
println!("{s}");
} else {
panic!("No paths available for subcommand `{}`", subcommand.as_str());
}
crate::exit!(0);
true
} else {
false
}
}

Flags::parse_from(it)
pub fn parse(args: &[String]) -> Self {
Flags::parse_from(normalize_args(args))
}
}

fn normalize_args(args: &[String]) -> Vec<String> {
let first = String::from("x.py");
let it = std::iter::once(first).chain(args.iter().cloned());
it.collect()
}

#[derive(Debug, Clone, Default, clap::Subcommand)]
pub enum Subcommand {
#[command(aliases = ["b"], long_about = "\n
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(clippy::module_inception)]
mod config;
pub(crate) mod flags;
pub mod flags;
#[cfg(test)]
mod tests;

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod core;
mod utils;

pub use core::builder::PathSet;
pub use core::config::flags::Subcommand;
pub use core::config::flags::{Flags, Subcommand};
pub use core::config::Config;

pub use utils::change_tracker::{
Expand Down

0 comments on commit 03ee7b5

Please sign in to comment.