[Libguestfs] [PATCH] fish: show synopsis if command syntax is wrong

Hu Tao
Fri, 28 Nov 2014 09:55:51 -0000

This patch lets guestfish show command synopsis if the syntax of command issued
by user is wrong, rather than telling user that the number of parameters is
wrong.

Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 fish/cmds-gperf.h |  1 +
 generator/fish.ml | 33 ++++++++++++---------------------
 2 files changed, 13 insertions(+), 21 deletions(-)

diff --git a/fish/cmds-gperf.h b/fish/cmds-gperf.h
index 74db69d..bcb3b5d 100644
--- a/fish/cmds-gperf.h
+++ b/fish/cmds-gperf.h
@@ -25,6 +25,7 @@
 struct command_entry {
   const char *name;             /* Short name. */
   const char *help;             /* Online help. */
+  const char *synopsis;         /* Synopsis. */

   /* The run_* function. */
   int (*run) (const char *cmd, size_t argc, char *argv[]);
diff --git a/generator/fish.ml b/generator/fish.ml
index 3f53ffa..951b376 100644
--- a/generator/fish.ml
+++ b/generator/fish.ml
@@ -190,6 +190,7 @@ Guestfish will prompt for these separately."
       pr "struct command_entry %s_cmd_entry = {\n" name;
       pr "  .name = \"%s\",\n" name2;
       pr "  .help = \"%s\",\n" (c_quote text);
+      pr "  .synopsis = \"%s\",\n" (c_quote synopsis);
       pr "  .run = run_%s\n" name;
       pr "};\n";
       pr "\n";
@@ -393,30 +394,14 @@ Guestfish will prompt for these separately."

       if argc_minimum = argc_maximum then (
         pr "  if (argc != %d) {\n" argc_minimum;
-        if argc_minimum = 0 then (
-          pr "    fprintf (stderr, _(\"%%s should have no
parameters\\n\"), cmd);\n";
-        ) else (
-          pr "    fprintf (stderr, ngettext(\"%%s should have %%d
parameter\\n\",\n";
-          pr "                              \"%%s should have %%d
parameters\\n\",\n";
-          pr "                              %d),\n"
-            argc_minimum;
-          pr "                     cmd, %d);\n"
-            argc_minimum;
-        )
+          pr "    ret = -2;\n";
       ) else if argc_minimum = 0 then (
         pr "  if (argc > %d) {\n" argc_maximum;
-        pr "    fprintf (stderr, ngettext(\"%%s should have at most
%%d parameter\\n\",\n";
-        pr "                              \"%%s should have at most
%%d parameters\\n\",\n";
-        pr "                              %d),\n"
-          argc_maximum;
-        pr "                     cmd, %d);\n"
-          argc_maximum;
+        pr "    ret = -2;\n";
       ) else (
         pr "  if (argc < %d || argc > %d) {\n" argc_minimum
argc_maximum;
-        pr "    fprintf (stderr, _(\"%%s should have %%d-%%d
parameter(s)\\n\"), cmd, %d, %d);\n"
-          argc_minimum argc_maximum;
+        pr "    ret = -2;\n";
       );
-      pr "    fprintf (stderr, _(\"type 'help %%s' for help
on %%s\\n\"), cmd, cmd);\n";
       pr "    goto out_noargs;\n";
       pr "  }\n";

@@ -694,10 +679,16 @@ Guestfish will prompt for these separately."
   pr "run_action (const char *cmd, size_t argc, char *argv[])\n";
   pr "{\n";
   pr "  const struct command_table *ct;\n";
+  pr "  int ret = -1;\n";
   pr "\n";
   pr "  ct = lookup_fish_command (cmd, strlen (cmd));\n";
-  pr "  if (ct)\n";
-  pr "    return ct->entry->run (cmd, argc, argv);\n";
+  pr "  if (ct) {\n";
+  pr "    ret = ct->entry->run (cmd, argc, argv);\n";
+  pr "    if (ret == -2) {\n";
+  pr "      fprintf (stderr, _(\"usage: %%s\\n\"),
ct->entry->synopsis);\n";
+  pr "      fprintf (stderr, _(\"type 'help %%s' for more
help on %%s\\n\"), cmd, cmd);\n";
+  pr "    }\n";
+  pr "  }\n";
   pr "  else {\n";
   pr "    fprintf (stderr, _(\"%%s: unknown command\\n\"),
cmd);\n";
   pr "    if (command_num == 1)\n";
--
1.9.3