Skip to content

Issues with sub options #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wadewegner opened this issue Oct 1, 2015 · 6 comments
Open

Issues with sub options #249

wadewegner opened this issue Oct 1, 2015 · 6 comments

Comments

@wadewegner
Copy link

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?

@nemec
Copy link
Collaborator

nemec commented Oct 4, 2015

What version are you using? Based on the stable branch, your sample code works perfectly (for me). Based on Master, it fails to compile since the library has gone through some major changes for 2.0.

@gsscoder
Copy link
Owner

gsscoder commented Oct 5, 2015

📎 Please read: #250. Thanks for collaboration and patience! 😅
Giacomo
P.S.: valid only for me, if a main contrib. is available he's obviously free to support devs/users of the project.

@alexanderfast
Copy link
Collaborator

@wadewegner did you figure it out? I also tried your code and as @nemec I get the expected result; subOptions is not null and Name has a value.

@Jamby93
Copy link

Jamby93 commented Apr 18, 2016

Same issue here with an almost identical class configuration. subOptions passed to callback is null, although options.subOption is valid with correct values (options.CpVerb in your case).

@brandorf
Copy link

I'm seeing this as well with latest version from nuget.

@nemec
Copy link
Collaborator

nemec commented Apr 29, 2016

@Jamby93 @brandorf latest stable or prerelease? I just tested again with 1.9.71 from NuGet, copy and pasting the above, and it works perfectly. VS 2015, .Net 4.5.2 in case you're running something different.

The exact code: https://gist.github.com/nemec/bc9d78e47d7a7d8a72a9711488b3bfef
Image of debug breakpoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants