[libvirt] [PATCH 2/3] virsh: Convert '<cmd> --help' to 'help <cmd>'

Doug Goldstein cardoe at gentoo.org
Mon Aug 13 18:50:47 UTC 2012


On Mon, Aug 13, 2012 at 9:28 AM, Cole Robinson <crobinso at redhat.com> wrote:
> 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 | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
>
> diff --git a/tools/virsh.c b/tools/virsh.c
> index 64e2e18..324f789 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -1189,12 +1189,18 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint32_t *opts_need_arg,
>      return 0;
>  }
>
> +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;
> --
> 1.7.11.2
>

I love this feature so much. Changes look good. ACK.

-- 
Doug Goldstein




More information about the libvir-list mailing list