]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Convert '<cmd> --help' to 'help <cmd>'
authorCole Robinson <crobinso@redhat.com>
Sat, 11 Aug 2012 18:45:45 +0000 (14:45 -0400)
committerCole Robinson <crobinso@redhat.com>
Mon, 13 Aug 2012 19:33:44 +0000 (15:33 -0400)
Often times I find myself halfway through typing a long command when
I want to see 'help' output. I instinctively append '--help' to the
command I'm typing, only to get an error:

    $ virsh vol-create-as foo --help
    error: command 'vol-create-as' doesn't support option --help

This patch makes --help work in a pretty hacky way. One missing piece
here is that --help isn't listed as an option in the actual 'help <cmd>'
output, but maybe this can be a starting point for someone.

tools/virsh.c

index 64e2e18a15c62831b1a218f1aa8d3ee20282b0c4..e7b01596beec58c98cccd184388bc0dcef8a8250 100644 (file)
@@ -1189,12 +1189,18 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint32_t *opts_need_arg,
     return 0;
 }
 
+static vshCmdOptDef helpopt = {"help", VSH_OT_BOOL, 0,
+                               N_("print help for this function")};
 static const vshCmdOptDef *
 vshCmddefGetOption(vshControl *ctl, const vshCmdDef *cmd, const char *name,
                    uint32_t *opts_seen, int *opt_index)
 {
     int i;
 
+    if (STREQ(name, helpopt.name)) {
+        return &helpopt;
+    }
+
     for (i = 0; cmd->opts && cmd->opts[i].name; i++) {
         const vshCmdOptDef *opt = &cmd->opts[i];
 
@@ -2053,6 +2059,25 @@ get_data:
         /* command parsed -- allocate new struct for the command */
         if (cmd) {
             vshCmd *c = vshMalloc(ctl, sizeof(vshCmd));
+            vshCmdOpt *tmpopt = first;
+
+            /* if we encountered --help, replace parsed command with
+             * 'help <cmdname>' */
+            for (tmpopt = first; tmpopt; tmpopt = tmpopt->next) {
+                if (STRNEQ(tmpopt->def->name, "help"))
+                    continue;
+
+                vshCommandOptFree(first);
+                first = vshMalloc(ctl, sizeof(vshCmdOpt));
+                first->def = &(opts_help[0]);
+                first->data = vshStrdup(ctl, cmd->name);
+                first->next = NULL;
+
+                cmd = vshCmddefSearch("help");
+                opts_required = 0;
+                opts_seen = 0;
+                break;
+            }
 
             c->opts = first;
             c->def = cmd;