From d2f971390ef270a73d87c90e3e70278021faf341 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sat, 11 Aug 2012 14:45:45 -0400 Subject: [PATCH] virsh: Convert ' --help' to 'help ' 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 ' output, but maybe this can be a starting point for someone. --- tools/virsh.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/virsh.c b/tools/virsh.c index 64e2e18a15..e7b01596be 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -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 ' */ + 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; -- GitLab