mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-28 15:26:25 +00:00
cli_util: always run custom global-args-processing function
The way I added support for processing custom global arguments - by using the existing `dispatch_fn` - meant that it wouldn't be run if `CliRunner::add_global_args()` was called before `CliRunner::add_subcommand()` and the custom subcommand wasn't run. That's clearly not what I intended. This commit fixes that by adding a separate list of functions for processing global args.
This commit is contained in:
parent
0ea8da064d
commit
0e90cdedbc
1 changed files with 13 additions and 8 deletions
|
@ -1927,11 +1927,14 @@ pub struct CliRunner {
|
|||
app: Command,
|
||||
store_factories: Option<StoreFactories>,
|
||||
dispatch_fn: CliDispatchFn,
|
||||
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
|
||||
}
|
||||
|
||||
type CliDispatchFn =
|
||||
Box<dyn FnOnce(&mut Ui, &CommandHelper, &ArgMatches) -> Result<(), CommandError>>;
|
||||
|
||||
type ProcessGlobalArgsFn = Box<dyn FnOnce(&mut Ui, &ArgMatches) -> Result<(), CommandError>>;
|
||||
|
||||
impl CliRunner {
|
||||
/// Initializes CLI environment and returns a builder. This should be called
|
||||
/// as early as possible.
|
||||
|
@ -1943,6 +1946,7 @@ impl CliRunner {
|
|||
app: crate::commands::default_app(),
|
||||
store_factories: None,
|
||||
dispatch_fn: Box::new(crate::commands::run_command),
|
||||
process_global_args_fns: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1977,15 +1981,13 @@ impl CliRunner {
|
|||
A: clap::Args,
|
||||
F: FnOnce(&mut Ui, A) -> Result<(), CommandError> + 'static,
|
||||
{
|
||||
let old_dispatch_fn = self.dispatch_fn;
|
||||
let new_dispatch_fn =
|
||||
move |ui: &mut Ui, command_helper: &CommandHelper, matches: &ArgMatches| {
|
||||
let custom_args = A::from_arg_matches(matches).unwrap();
|
||||
process_before(ui, custom_args)?;
|
||||
old_dispatch_fn(ui, command_helper, matches)
|
||||
};
|
||||
let process_global_args_fn = move |ui: &mut Ui, matches: &ArgMatches| {
|
||||
let custom_args = A::from_arg_matches(matches).unwrap();
|
||||
process_before(ui, custom_args)
|
||||
};
|
||||
self.app = A::augment_args(self.app);
|
||||
self.dispatch_fn = Box::new(new_dispatch_fn);
|
||||
self.process_global_args_fns
|
||||
.push(Box::new(process_global_args_fn));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -2006,6 +2008,9 @@ impl CliRunner {
|
|||
&string_args,
|
||||
&mut layered_configs,
|
||||
)?;
|
||||
for process_global_args_fn in self.process_global_args_fns {
|
||||
process_global_args_fn(ui, &matches)?;
|
||||
}
|
||||
|
||||
let maybe_workspace_loader = init_workspace_loader(&cwd, &args.global_args);
|
||||
if let Ok(loader) = &maybe_workspace_loader {
|
||||
|
|
Loading…
Reference in a new issue