Move verbose help parsing to main

To remove a side effect (process exit) when parsing config.
This commit is contained in:
Jakub Beránek 2024-08-09 16:19:19 +02:00
parent 97e72524a3
commit 03ee7b5999
4 changed files with 25 additions and 11 deletions

View file

@ -11,7 +11,7 @@ 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,
};
@ -19,6 +19,10 @@ 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;

View file

@ -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))]
@ -198,10 +198,10 @@ 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 {
@ -209,11 +209,21 @@ impl Flags {
} 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)]

View file

@ -1,6 +1,6 @@
#[allow(clippy::module_inception)]
mod config;
pub(crate) mod flags;
pub mod flags;
#[cfg(test)]
mod tests;

View file

@ -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::{