Skip to content

Commit

Permalink
cli: just parse config args in helper function, apply them by caller
Browse files Browse the repository at this point in the history
I'm going to add config post-processing stage in order to enable config
variables conditionally, and handle/parse_early_args() doesn't have enough
information to evaluate the condition.

The deprecation warning could be emitted by parse_early_args(), but it's
probably better to print it after --color option is applied.
  • Loading branch information
yuja committed Dec 18, 2024
1 parent 2b9ffdf commit 8863b94
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3310,12 +3310,10 @@ fn resolve_aliases(
}

/// Parse args that must be interpreted early, e.g. before printing help.
fn handle_early_args(
ui: &mut Ui,
fn parse_early_args(
app: &Command,
args: &[String],
config: &mut StackedConfig,
) -> Result<(), CommandError> {
) -> Result<(EarlyArgs, Vec<ConfigLayer>), CommandError> {
// ignore_errors() bypasses errors like missing subcommand
let early_matches = app
.clone()
Expand All @@ -3334,15 +3332,7 @@ fn handle_early_args(
.try_get_matches_from(args)?;
let args = EarlyArgs::from_arg_matches(&early_matches).unwrap();

let old_layers_len = config.layers().len();
if !args.config_toml.is_empty() {
writeln!(
ui.warning_default(),
"--config-toml is deprecated; use --config or --config-file instead."
)?;
}
config.extend_layers(parse_config_args(&args.merged_config_args(&early_matches))?);

let mut config_layers = parse_config_args(&args.merged_config_args(&early_matches))?;
// Command arguments overrides any other configuration including the
// variables loaded from --config* arguments.
let mut layer = ConfigLayer::empty(ConfigSource::CommandArg);
Expand All @@ -3356,13 +3346,9 @@ fn handle_early_args(
layer.set_value("ui.paginate", "never").unwrap();
}
if !layer.is_empty() {
config.add_layer(layer);
}

if config.layers().len() != old_layers_len {
ui.reset(config)?;
config_layers.push(layer);
}
Ok(())
Ok((args, config_layers))
}

fn handle_shell_completion(
Expand Down Expand Up @@ -3431,14 +3417,11 @@ pub fn expand_args(
resolve_aliases(ui, config, app, string_args)
}

pub fn parse_args(
ui: &mut Ui,
fn parse_args(
app: &Command,
tracing_subscription: &TracingSubscription,
string_args: &[String],
config: &mut StackedConfig,
) -> Result<(ArgMatches, Args), CommandError> {
handle_early_args(ui, app, string_args, config)?;
let matches = app
.clone()
.arg_required_else_help(true)
Expand Down Expand Up @@ -3664,14 +3647,19 @@ impl CliRunner {
}

let string_args = expand_args(ui, &self.app, env::args_os(), &config)?;
let (matches, args) = parse_args(
ui,
&self.app,
&self.tracing_subscription,
&string_args,
&mut config,
)
.map_err(|err| map_clap_cli_error(err, ui, &config))?;
let (args, config_layers) = parse_early_args(&self.app, &string_args)?;
if !config_layers.is_empty() {
config.extend_layers(config_layers);
ui.reset(&config)?;
}
if !args.config_toml.is_empty() {
writeln!(
ui.warning_default(),
"--config-toml is deprecated; use --config or --config-file instead."
)?;
}
let (matches, args) = parse_args(&self.app, &self.tracing_subscription, &string_args)
.map_err(|err| map_clap_cli_error(err, ui, &config))?;
for process_global_args_fn in self.process_global_args_fns {
process_global_args_fn(ui, &matches)?;
}
Expand Down

0 comments on commit 8863b94

Please sign in to comment.