rhg: consistently use the command name given in clap::command!(<...>) macro
authorArseniy Alekseyev <aalekseyev@janestreet.com>
Fri, 13 Dec 2024 16:19:29 +0000
changeset 52523 021c1b1671e5
parent 52522 92c6c8ab6f94
child 52524 a0587c1b633a
rhg: consistently use the command name given in clap::command!(<...>) macro Before this patch there are 2 things the user controls: 1. the module/command name, specified in subcommand! macro 2. the command name, specified in clap::command! macro If these are out of sync, we get no compile error or a clear runtime error, but instead a confusing behavior where command line parser parses one thing, but running it doesn't work. This commit makes the clap::command! macro the sole authority determining the command name, so we don't have to worry about this weird behavior any more. It also makes it easy to validate agreement between (1) and (2) if we want it, but I didn't add the check because I'm not sure people necessarily want it.
rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs	Fri Dec 13 15:43:50 2024 +0000
+++ b/rust/rhg/src/main.rs	Fri Dec 13 16:19:29 2024 +0000
@@ -545,13 +545,15 @@
 }
 
 macro_rules! subcommand {
-    ($command: ident) => {
+    ($command: ident) => {{
+        let args = commands::$command::args();
+        let name = args.get_name().to_string();
         SubCommand {
-            args: commands::$command::args(),
+            args,
             run: commands::$command::run,
-            name: stringify!($command).to_string(),
+            name,
         }
-    };
+    }};
 }
 fn subcommands() -> Vec<SubCommand> {
     vec![