Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HelpVerbOption is not working - Command Line Parser C#

I have a class:

class Options
{
    // Remainder omitted (verb1, verb2, verb3)
    [HelpVerbOption]
    public string GetUsage(string verb)
    {
        return HelpText.AutoBuild(this, verb);
    }
}

The docs say:

[...] The parser will pass null to master class GetUsage(string) also if the user requested the help index with:

$ git help

or the verb command if the user requested explicitly instructions on how to use a particular verb:

$ git help commit

[...]

Then, I typed MyApp.exe help verb1, but I could see only the base help (that looked like I typed the wrong verb, or help verb, or something). Rather, I expect it to show the help message related to specified verb. Why isn't it working properly?

like image 700
murzagurskiy Avatar asked Aug 22 '16 12:08

murzagurskiy


1 Answers

For me it works using the mentioned approach, but only if I call my app without the --help-option (for instance MyApp batch). When I use MyApp --help batch the behaviour is as described by you.

However we can´t seem to get the same to work for the help-option.

EDIT: I managed to get this working by modifying the code of Commandline.Parser.cs with the following:

private bool TryParseHelpVerb(string[] args, object options, Pair<MethodInfo, HelpVerbOptionAttribute> helpInfo, OptionMap optionMap)
{
    var helpWriter = _settings.HelpWriter;
    if (helpInfo != null && helpWriter != null)
    {
        if (string.Compare(args[0], helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
        {
            // User explicitly requested help
            var verb = args.FirstOrDefault(); // <----- change this to args[1];
            if (verb != null)
            {
                var verbOption = optionMap[verb];
                if (verbOption != null)
                {
                    if (verbOption.GetValue(options) == null)
                    {
                        // We need to create an instance also to render help
                        verbOption.CreateInstance(options);
                    }
                }
            }
            DisplayHelpVerbText(options, helpInfo, verb);
            return true;
        }
    }

    return false;
}

The problem appears at the line

var verb = args.FirstOrDefault();

As the very first argument (args[0]) is interpreteted as the verb or better the action (as described in the docs) verb will allways be help here. So we replace this by args[1] which contains the actual verb, for example commit.

EDIT2: To make this work for --help also we should also trim the first arg (args[0]) from the --character

if (string.Compare(args[0].Trim('-'), helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
like image 152
MakePeaceGreatAgain Avatar answered Nov 18 '22 07:11

MakePeaceGreatAgain