Parser command line command not working?

I defined the parameters as follows:

public class ArgumentsHeader
{
    [VerbOption("configure", HelpText = "Sets configuration on server.")]
    public ServerConfigurationArguments ServerConfigurationArguments { get; set; }

    [HelpVerbOption]
    public string GetUsage(string s)
    {
        return HelpText.AutoBuild(this, s);//always just 'help' or null showing up here.
    }
}
public class ServerConfigurationArguments : ArgumentsBase
{
    [Option('f', "filename", HelpText = "Path to JSON configuration file", DefaultValue = "config.json", Required = true)]
    public string PathToConfig { get; set; }
}

Then parse them as follows:

        string invokedVerb = null;
        object invokedVerbInstance = null;


        var parser = new Parser(x =>
        {
            x.MutuallyExclusive = true;
        });
        var options = new ArgumentsHeader();
        if (!parser.ParseArguments(args, options,
            (verb, subOptions) =>
            {
                // if parsing succeeds the verb name and correct instance
                // will be passed to onVerbCommand delegate (string,object)
                invokedVerb = verb;
                invokedVerbInstance = subOptions;
            }))
        {
            Exit(ExitStatus.InvalidArguments);
        }

But if I try to run exe using "help configure", it just prints all the help, and in the GetUsage (string) method, only the "help" command appears in the debugger.

Is this a mistake or what?

+4
source share
3 answers

This is mistake.

I checked a program similar to yours and had the same (wrong) behavior, and then switched to the command line project itself, had the same thing, but I think I found the problem.

Parser, , ( commandLine.Parser):

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
                // +++ FIX
                // var verb = args.FirstOrDefault(); // This looks wrong as the first element is always the help command itself
                var verb = args.Length == 1 ? null : args[1]; // Skip the help command and use next argument as verb
                // --- FIX
                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;
    }

, DLL Parser, , . ...

+5

NuGet, . , HelpText.AutoBuild. Verb HelpText.AutoBuild.

public class ArgumentsHeader
{
    public string[] args { get; set; } = new string[0];

    [VerbOption("configure", HelpText = "Sets configuration on server.")]
    public ServerConfigurationArguments ServerConfigurationArguments { get; set; } = new ServerConfigurationArguments();

    [HelpVerbOption]
    public string GetUsage(string verb)
    {
        if (verb?.ToLower() == "help" && args.Length > 1)
        {
            verb = args[1];
        }

        return HelpText.AutoBuild(this, verb);
    }
}

args.

 var options = new ArgumentsHeader { args = args };
+2
-1

Source: https://habr.com/ru/post/1673490/


All Articles