Skip to content

Issues with sub options #249

Open
Open
@wadewegner

Description

@wadewegner

I'm having an issue with the parser not figuring out my sub-options.

Here's the code in Main:

var invokedVerb = "";
var invokedVerbInstance = new object();

var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options,
    (verb, subOptions) =>
    {
        // if parsing succeeds the verb name and correct instance
        // will be passed to onVerbCommand delegate (string,object)
        invokedVerb = verb;
        if (!string.IsNullOrEmpty(invokedVerb))
        invokedVerbInstance = subOptions;
    }))
{
    Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
}

This is pretty much right from the wiki.

My options are defined as this:

class Options
{
    public Options()
    {
        CpVerb = new CPSubOptions { };
    }

    [VerbOption("cp", HelpText = "Interact with cp.")]
    public CPSubOptions CpVerb { get; set; }

    [HelpVerbOption]
    public string GetUsage(string verb)
    {
        return HelpText.AutoBuild(this, verb);
    }
}

class CPSubOptions
{
    [Option('n', "name", HelpText = "Get details name.")]
    public string Name { get; set; }
}

When I run with cp as the args, it works and the parser determines that CPSubOptions is the subOptions and invokedVerbInstance is set appropriately.

However, when I run with cp --name blah or cp -n blah, the subOptions is null, so it's not parsed properly.

Any help?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions