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

cli: pass UserSettings in to expand_args() and parse_args(), update in place

This is a temporary workaround. I think UserSettings can be constructed after
user, repo, and --config-toml values are all set. parse_args() will probably
return config::Config or its wrapper instead of mutating UserSettings.
This commit is contained in:
Yuya Nishihara 2023-01-04 18:24:30 +09:00
parent d7911ff279
commit 825d00e62a

View file

@ -1601,6 +1601,7 @@ fn handle_early_args(
ui: &mut Ui, ui: &mut Ui,
app: &clap::Command, app: &clap::Command,
args: &[String], args: &[String],
settings: &mut UserSettings,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
// ignore_errors() bypasses errors like "--help" or missing subcommand // ignore_errors() bypasses errors like "--help" or missing subcommand
let early_matches = app.clone().ignore_errors(true).get_matches_from(args); let early_matches = app.clone().ignore_errors(true).get_matches_from(args);
@ -1613,17 +1614,16 @@ fn handle_early_args(
ui.set_pagination(crate::ui::PaginationChoice::No); ui.set_pagination(crate::ui::PaginationChoice::No);
} }
if !args.config_toml.is_empty() { if !args.config_toml.is_empty() {
let mut settings = ui.settings().clone();
settings.incorporate_toml_strings(&args.config_toml)?; settings.incorporate_toml_strings(&args.config_toml)?;
ui.reset(settings); ui.reset(settings.clone()); // TODO: disown UserSettings from Ui
} }
Ok(()) Ok(())
} }
pub fn expand_args( pub fn expand_args(
ui: &Ui,
app: &clap::Command, app: &clap::Command,
args_os: ArgsOs, args_os: ArgsOs,
settings: &UserSettings,
) -> Result<Vec<String>, CommandError> { ) -> Result<Vec<String>, CommandError> {
let mut string_args: Vec<String> = vec![]; let mut string_args: Vec<String> = vec![];
for arg_os in args_os { for arg_os in args_os {
@ -1634,7 +1634,7 @@ pub fn expand_args(
} }
} }
resolve_aliases(ui.settings(), app, &string_args) resolve_aliases(settings, app, &string_args)
} }
pub fn parse_args( pub fn parse_args(
@ -1642,10 +1642,11 @@ pub fn parse_args(
app: &clap::Command, app: &clap::Command,
tracing_subscription: &TracingSubscription, tracing_subscription: &TracingSubscription,
string_args: &[String], string_args: &[String],
settings: &mut UserSettings,
) -> Result<(ArgMatches, Args), CommandError> { ) -> Result<(ArgMatches, Args), CommandError> {
// TODO: read user configs from the repo pointed to by -R. // TODO: read user configs from the repo pointed to by -R.
handle_early_args(ui, app, string_args)?; handle_early_args(ui, app, string_args, settings)?;
let matches = app.clone().try_get_matches_from(string_args)?; let matches = app.clone().try_get_matches_from(string_args)?;
let args: Args = Args::from_arg_matches(&matches).unwrap(); let args: Args = Args::from_arg_matches(&matches).unwrap();
@ -1774,9 +1775,17 @@ impl CliRunner {
pub fn run(self, ui: &mut Ui) -> Result<(), CommandError> { pub fn run(self, ui: &mut Ui) -> Result<(), CommandError> {
let cwd = env::current_dir().unwrap(); // TODO: maybe map_err to CommandError? let cwd = env::current_dir().unwrap(); // TODO: maybe map_err to CommandError?
ui.reset(crate::config::read_config()?); let mut settings = crate::config::read_config()?;
let string_args = expand_args(ui, &self.app, std::env::args_os())?; ui.reset(settings.clone()); // TODO: disown UserSettings from Ui
let (matches, args) = parse_args(ui, &self.app, &self.tracing_subscription, &string_args)?; let string_args = expand_args(&self.app, std::env::args_os(), &settings)?;
let (matches, args) = parse_args(
ui,
&self.app,
&self.tracing_subscription,
&string_args,
&mut settings,
)?;
// TODO: maybe instantiate UserSettings here
let command_helper = CommandHelper::new( let command_helper = CommandHelper::new(
self.app, self.app,
cwd, cwd,