changeset 46630:21d3b40b4c0e

rhg: Remove error message on unsupported CLI arguments Like in other "unsupported" cases that return a specific exit code Differential Revision: https://phab.mercurial-scm.org/D10002
author Simon Sapin <simon.sapin@octobus.net>
date Fri, 12 Feb 2021 16:54:30 +0100
parents 8d20abed6a1e
children 80840b651721
files rust/rhg/src/error.rs rust/rhg/src/main.rs rust/rhg/src/ui.rs tests/test-rhg.t
diffstat 4 files changed, 21 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/rust/rhg/src/error.rs	Tue Jan 05 21:46:21 2021 +0100
+++ b/rust/rhg/src/error.rs	Fri Feb 12 16:54:30 2021 +0100
@@ -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<clap::Error> for CommandError {
+    fn from(_: clap::Error) -> Self {
+        CommandError::Unimplemented
+    }
+}
+
 impl From<HgError> for CommandError {
     fn from(error: HgError) -> Self {
         match error {
--- a/rust/rhg/src/main.rs	Tue Jan 05 21:46:21 2021 +0100
+++ b/rust/rhg/src/main.rs	Fri Feb 12 16:54:30 2021 +0100
@@ -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.
--- a/rust/rhg/src/ui.rs	Tue Jan 05 21:46:21 2021 +0100
+++ b/rust/rhg/src/ui.rs	Fri Feb 12 16:54:30 2021 +0100
@@ -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.
--- a/tests/test-rhg.t	Tue Jan 05 21:46:21 2021 +0100
+++ b/tests/test-rhg.t	Fri Feb 12 16:54:30 2021 +0100
@@ -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] <SUBCOMMAND>
-  
-  For more information try --help
   [252]
 
 Finding root