Skip to content

Issues with sub options #48

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
ericnewton76 opened this issue Nov 4, 2017 · 6 comments
Open

Issues with sub options #48

ericnewton76 opened this issue Nov 4, 2017 · 6 comments

Comments

@ericnewton76
Copy link
Member

Issue by wadewegner
Thursday Oct 01, 2015 at 18:07 GMT
Originally opened as gsscoder/commandline#249


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?

@ericnewton76
Copy link
Member Author

Comment by nemec
Sunday Oct 04, 2015 at 06:50 GMT


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.

@ericnewton76
Copy link
Member Author

Comment by gsscoder
Monday Oct 05, 2015 at 22:43 GMT


📎 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.

@ericnewton76
Copy link
Member Author

Comment by Mizipzor
Tuesday Feb 02, 2016 at 20:25 GMT


@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.

@ericnewton76
Copy link
Member Author

Comment by Jamby93
Monday Apr 18, 2016 at 19:59 GMT


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).

@ericnewton76
Copy link
Member Author

Comment by brandorf
Wednesday Apr 27, 2016 at 19:16 GMT


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

@ericnewton76
Copy link
Member Author

Comment by nemec
Friday Apr 29, 2016 at 00:08 GMT


@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

1 participant