]> xenbits.xensource.com Git - libvirt.git/commitdiff
vshCmddefHelp: Refactor and fix printing of help for _STRING/_INT arguments
authorPeter Krempa <pkrempa@redhat.com>
Tue, 5 Mar 2024 15:53:55 +0000 (16:53 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 13 Mar 2024 14:02:52 +0000 (15:02 +0100)
Use the new flags to do the decisions which will also fix the case when
an _INT option is required but non-positional.

This fixes the help for the 'timeout' argument of 'daemon-timeout'
virt-admin command:

     SYNOPSIS
  -    daemon-timeout <timeout>
  +    daemon-timeout --timeout <number>

[...]
     OPTIONS
  -    [--timeout] <number>  number of seconds the daemon will run without any active connection
  +    --timeout <number>  number of seconds the daemon will run without any active connection

Resolves: https://issues.redhat.com/browse/RHEL-25993
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tools/vsh.c

index 1907e778f2fa47233ef35dd6c2b178ed8c5d308c..b640a7a833de31bc63dd9c7368fb4185d6adaade 100644 (file)
@@ -642,31 +642,33 @@ vshCmddefHelp(const vshCmdDef *def)
     if (def->opts) {
         const vshCmdOptDef *opt;
         for (opt = def->opts; opt->name; opt++) {
-            bool required_option = opt->flags & VSH_OFLAG_REQ;
 
             switch (opt->type) {
             case VSH_OT_BOOL:
                 fprintf(stdout, " [--%s]", opt->name);
                 break;
 
+            case VSH_OT_STRING:
+            case VSH_OT_DATA:
             case VSH_OT_INT:
-                if (required_option) {
-                    fprintf(stdout, " <%s>", opt->name);
+                if (opt->required) {
+                    fprintf(stdout, " ");
                 } else {
-                    fprintf(stdout, _(" [--%1$s <number>]"), opt->name);
+                    fprintf(stdout, " [");
                 }
-                break;
 
-            case VSH_OT_STRING:
-                fprintf(stdout, _(" [--%1$s <string>]"), opt->name);
-                break;
-
-            case VSH_OT_DATA:
-                if (required_option) {
-                    fprintf(stdout, " <%s>", opt->name);
+                if (opt->positional) {
+                    fprintf(stdout, "<%s>", opt->name);
                 } else {
-                    fprintf(stdout, " [<%s>]", opt->name);
+                    if (opt->type == VSH_OT_INT) {
+                        fprintf(stdout, _("--%1$s <number>"), opt->name);
+                    } else {
+                        fprintf(stdout, _("--%1$s <string>"), opt->name);
+                    }
                 }
+
+                if (!opt->required)
+                    fprintf(stdout, "]");
                 break;
 
             case VSH_OT_ARGV:
@@ -704,7 +706,6 @@ vshCmddefHelp(const vshCmdDef *def)
         const vshCmdOptDef *opt;
         fputs(_("\n  OPTIONS\n"), stdout);
         for (opt = def->opts; opt->name; opt++) {
-            bool required_option = opt->flags & VSH_OFLAG_REQ;
             g_autofree char *optstr = NULL;
 
             switch (opt->type) {
@@ -713,7 +714,7 @@ vshCmddefHelp(const vshCmdDef *def)
                 break;
 
             case VSH_OT_INT:
-                if (required_option) {
+                if (opt->positional) {
                     optstr = g_strdup_printf(_("[--%1$s] <number>"), opt->name);
                 } else {
                     optstr = g_strdup_printf(_("--%1$s <number>"), opt->name);
@@ -721,11 +722,12 @@ vshCmddefHelp(const vshCmdDef *def)
                 break;
 
             case VSH_OT_STRING:
-                optstr = g_strdup_printf(_("--%1$s <string>"), opt->name);
-                break;
-
             case VSH_OT_DATA:
-                optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
+                if (opt->positional) {
+                    optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
+                } else {
+                    optstr = g_strdup_printf(_("--%1$s <string>"), opt->name);
+                }
                 break;
 
             case VSH_OT_ARGV: