changeset 45051:18f8d3b31baa

rhg: add a limited `rhg root` subcommand Clap has been choosen for argument parsing for the following reasons: - it's a wildly used and maintained crate - it can deal with OS encoding making it suitable for any encoding - it supports nonambiguous prefix matching as already available in hg - it will soon allow for structopts-style declarative pattern instead of the currently used builder pattern Differential Revision: https://phab.mercurial-scm.org/D8613
author Antoine Cezar <antoine.cezar@octobus.net>
date Tue, 07 Jul 2020 14:05:15 +0530
parents 513b3ef277a3
children 93e8e6e0b5fb
files rust/Cargo.lock rust/rhg/Cargo.toml rust/rhg/src/main.rs tests/test-rhg.t
diffstat 4 files changed, 66 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Fri Jun 05 09:01:35 2020 +0200
+++ b/rust/Cargo.lock	Tue Jul 07 14:05:15 2020 +0530
@@ -58,7 +58,7 @@
 
 [[package]]
 name = "clap"
-version = "2.33.0"
+version = "2.33.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -203,7 +203,7 @@
 version = "0.1.0"
 dependencies = [
  "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
- "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "crossbeam 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "hex 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -490,6 +490,7 @@
 name = "rhg"
 version = "0.1.0"
 dependencies = [
+ "clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "hg-core 0.1.0",
 ]
 
@@ -657,7 +658,7 @@
 "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
 "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
 "checksum chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
-"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
+"checksum clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
 "checksum colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
 "checksum cpython 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bfaf3847ab963e40c4f6dd8d6be279bdf74007ae2413786a0dcbb28c52139a95"
 "checksum crossbeam 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e"
--- a/rust/rhg/Cargo.toml	Fri Jun 05 09:01:35 2020 +0200
+++ b/rust/rhg/Cargo.toml	Tue Jul 07 14:05:15 2020 +0530
@@ -6,4 +6,5 @@
 
 [dependencies]
 hg-core = { path = "../hg-core"}
+clap = "2.33.1"
 
--- a/rust/rhg/src/main.rs	Fri Jun 05 09:01:35 2020 +0200
+++ b/rust/rhg/src/main.rs	Tue Jul 07 14:05:15 2020 +0530
@@ -1,8 +1,42 @@
+use clap::App;
+use clap::AppSettings;
+use clap::SubCommand;
+
 mod commands;
 mod error;
 mod exitcode;
 mod ui;
+use commands::Command;
 
 fn main() {
-    std::process::exit(exitcode::UNIMPLEMENTED_COMMAND)
+    let mut app = App::new("rhg")
+        .setting(AppSettings::AllowInvalidUtf8)
+        .setting(AppSettings::SubcommandRequired)
+        .setting(AppSettings::VersionlessSubcommands)
+        .version("0.0.1")
+        .subcommand(
+            SubCommand::with_name("root").about(commands::root::HELP_TEXT),
+        );
+
+    let matches = app.clone().get_matches_safe().unwrap_or_else(|_| {
+        std::process::exit(exitcode::UNIMPLEMENTED_COMMAND)
+    });
+
+    let command_result = match matches.subcommand_name() {
+        Some(name) => match name {
+            "root" => commands::root::RootCommand::new().run(),
+            _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND),
+        },
+        _ => {
+            match app.print_help() {
+                Ok(_) => std::process::exit(exitcode::OK),
+                Err(_) => std::process::exit(exitcode::ABORT),
+            };
+        }
+    };
+
+    match command_result {
+        Ok(_) => std::process::exit(exitcode::OK),
+        Err(e) => e.exit(),
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-rhg.t	Tue Jul 07 14:05:15 2020 +0530
@@ -0,0 +1,26 @@
+#require rust
+
+  $ rhg() {
+  > if [ -f "$RUNTESTDIR/../rust/target/debug/rhg" ]; then
+  >   "$RUNTESTDIR/../rust/target/debug/rhg" "$@"
+  > else
+  >   echo "skipped: Cannot find rhg. Try to run cargo build in rust/rhg."
+  >   exit 80
+  > fi
+  > }
+  $ rhg unimplemented-command
+  [252]
+  $ rhg root
+  abort: no repository found in '$TESTTMP' (.hg not found)!
+  [255]
+  $ hg init repository
+  $ cd repository
+  $ rhg root
+  $TESTTMP/repository
+  $ rhg root > /dev/full
+  abort: No space left on device (os error 28)
+  [255]
+  $ rm -rf `pwd`
+  $ rhg root
+  abort: error getting current working directory: $ENOENT$
+  [255]