diff rust/rhg/src/main.rs @ 45050: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 47997afadf08
line wrap: on
line diff
--- 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(),
+    }
 }