# HG changeset patch # User Antoine Cezar # Date 2020-07-07 08:35:15 # Node ID 18f8d3b31baa4145c1ce85b8f2d935061c9b3e09 # Parent 513b3ef277a39dd758a328d1fe5c15f4e1700318 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 diff --git a/rust/Cargo.lock b/rust/Cargo.lock --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -58,7 +58,7 @@ dependencies = [ [[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 @@ name = "hg-core" 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 @@ dependencies = [ 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 @@ source = "registry+https://github.com/ru "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" diff --git a/rust/rhg/Cargo.toml b/rust/rhg/Cargo.toml --- a/rust/rhg/Cargo.toml +++ b/rust/rhg/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" [dependencies] hg-core = { path = "../hg-core"} +clap = "2.33.1" 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 @@ -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(), + } } diff --git a/tests/test-rhg.t b/tests/test-rhg.t new file mode 100644 --- /dev/null +++ b/tests/test-rhg.t @@ -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]