diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs --- a/rust/rhg/src/error.rs +++ b/rust/rhg/src/error.rs @@ -33,6 +33,14 @@ } } +/// For now we don’t differenciate between invalid CLI args and valid for `hg` +/// but not supported yet by `rhg`. +impl From for CommandError { + fn from(_: clap::Error) -> Self { + CommandError::Unimplemented + } +} + impl From for CommandError { fn from(error: HgError) -> Self { match error { diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs --- a/rust/rhg/src/main.rs +++ b/rust/rhg/src/main.rs @@ -33,7 +33,7 @@ ) } -fn main() { +fn main_with_result(ui: &ui::Ui) -> Result<(), CommandError> { env_logger::init(); let app = App::new("rhg") .setting(AppSettings::AllowInvalidUtf8) @@ -43,12 +43,7 @@ let app = add_global_args(app); let app = add_subcommand_args(app); - let ui = ui::Ui::new(); - - let matches = app.clone().get_matches_safe().unwrap_or_else(|err| { - let _ = ui.writeln_stderr_str(&err.message); - std::process::exit(exitcode::UNIMPLEMENTED) - }); + let matches = app.clone().get_matches_safe()?; let (subcommand_name, subcommand_matches) = matches.subcommand(); let run = subcommand_run_fn(subcommand_name) @@ -69,16 +64,18 @@ }; let repo_path = value_of_global_arg("repository").map(Path::new); - let result = (|| -> Result<(), CommandError> { - let config_args = values_of_global_arg("config") - // `get_bytes_from_path` works for OsStr the same as for Path - .map(hg::utils::files::get_bytes_from_path); - let config = hg::config::Config::load(config_args)?; - run(&ui, &config, repo_path, args) - })(); + let config_args = values_of_global_arg("config") + // `get_bytes_from_path` works for OsStr the same as for Path + .map(hg::utils::files::get_bytes_from_path); + let config = hg::config::Config::load(config_args)?; + run(&ui, &config, repo_path, args) +} - let exit_code = match result { - Ok(_) => exitcode::OK, +fn main() { + let ui = ui::Ui::new(); + + let exit_code = match main_with_result(&ui) { + Ok(()) => exitcode::OK, // Exit with a specific code and no error message to let a potential // wrapper script fallback to Python-based Mercurial. diff --git a/rust/rhg/src/ui.rs b/rust/rhg/src/ui.rs --- a/rust/rhg/src/ui.rs +++ b/rust/rhg/src/ui.rs @@ -49,11 +49,6 @@ stderr.flush().or_else(handle_stderr_error) } - - /// Write string line to stderr - pub fn writeln_stderr_str(&self, s: &str) -> Result<(), UiError> { - self.write_stderr(&format!("{}\n", s).as_bytes()) - } } /// A buffered stdout writer for faster batch printing operations. diff --git a/tests/test-rhg.t b/tests/test-rhg.t --- a/tests/test-rhg.t +++ b/tests/test-rhg.t @@ -12,12 +12,6 @@ Unimplemented command $ rhg unimplemented-command - error: Found argument 'unimplemented-command' which wasn't expected, or isn't valid in this context - - USAGE: - rhg [OPTIONS] - - For more information try --help [252] Finding root