ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: store ArgMatches in CommandHelper

This will make it easily available everywhere so we can get more
detailed information about arguments passed to commands. In
particular, I think it can be useful for displaying different hints
depending on whether the user passed `-r @` or if the `@` revision was
from the defaults.
This commit is contained in:
Martin von Zweigbergk 2023-04-29 06:31:03 -07:00 committed by Martin von Zweigbergk
parent 0628379eed
commit 9230fd93bb
2 changed files with 19 additions and 15 deletions

View file

@ -356,6 +356,7 @@ pub struct CommandHelper {
app: Command,
cwd: PathBuf,
string_args: Vec<String>,
matches: ArgMatches,
global_args: GlobalArgs,
settings: UserSettings,
layered_configs: LayeredConfigs,
@ -369,6 +370,7 @@ impl CommandHelper {
app: Command,
cwd: PathBuf,
string_args: Vec<String>,
matches: ArgMatches,
global_args: GlobalArgs,
settings: UserSettings,
layered_configs: LayeredConfigs,
@ -379,6 +381,7 @@ impl CommandHelper {
app,
cwd,
string_args,
matches,
global_args,
settings,
layered_configs,
@ -399,6 +402,10 @@ impl CommandHelper {
&self.string_args
}
pub fn matches(&self) -> &ArgMatches {
&self.matches
}
pub fn global_args(&self) -> &GlobalArgs {
&self.global_args
}
@ -2274,8 +2281,7 @@ pub struct CliRunner {
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
}
type CliDispatchFn =
Box<dyn FnOnce(&mut Ui, &CommandHelper, &ArgMatches) -> Result<(), CommandError>>;
type CliDispatchFn = Box<dyn FnOnce(&mut Ui, &CommandHelper) -> Result<(), CommandError>>;
type ProcessGlobalArgsFn = Box<dyn FnOnce(&mut Ui, &ArgMatches) -> Result<(), CommandError>>;
@ -2314,11 +2320,11 @@ impl CliRunner {
{
let old_dispatch_fn = self.dispatch_fn;
let new_dispatch_fn =
move |ui: &mut Ui, command_helper: &CommandHelper, matches: &ArgMatches| {
match C::from_arg_matches(matches) {
Ok(command) => custom_dispatch_fn(ui, command_helper, command),
Err(_) => old_dispatch_fn(ui, command_helper, matches),
}
move |ui: &mut Ui, command_helper: &CommandHelper| match C::from_arg_matches(
command_helper.matches(),
) {
Ok(command) => custom_dispatch_fn(ui, command_helper, command),
Err(_) => old_dispatch_fn(ui, command_helper),
};
self.app = C::augment_subcommands(self.app);
self.dispatch_fn = Box::new(new_dispatch_fn);
@ -2374,13 +2380,14 @@ impl CliRunner {
self.app,
cwd,
string_args,
matches,
args.global_args,
settings,
layered_configs,
maybe_workspace_loader,
self.store_factories.unwrap_or_default(),
);
(self.dispatch_fn)(ui, &command_helper, &matches)
(self.dispatch_fn)(ui, &command_helper)
}
#[must_use]

View file

@ -26,7 +26,7 @@ use std::sync::Arc;
use std::{fs, io};
use clap::builder::NonEmptyStringValueParser;
use clap::{ArgGroup, ArgMatches, Command, CommandFactory, FromArgMatches, Subcommand};
use clap::{ArgGroup, Command, CommandFactory, FromArgMatches, Subcommand};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId, TreeValue};
@ -3476,12 +3476,9 @@ pub fn default_app() -> Command {
Commands::augment_subcommands(Args::command())
}
pub fn run_command(
ui: &mut Ui,
command_helper: &CommandHelper,
matches: &ArgMatches,
) -> Result<(), CommandError> {
let derived_subcommands: Commands = Commands::from_arg_matches(matches).unwrap();
pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), CommandError> {
let derived_subcommands: Commands =
Commands::from_arg_matches(command_helper.matches()).unwrap();
match &derived_subcommands {
Commands::Version(sub_args) => cmd_version(ui, command_helper, sub_args),
Commands::Init(sub_args) => cmd_init(ui, command_helper, sub_args),