Skip to content

Non-generic GetValueForOption does not work with boolean flag #1647

Closed

Description

@einarmo

Reproduction:

var opt = new Option<bool>(new[] { "-b" }, "Some description");

var cmd = new RootCommand
{
    opt
};

var res = cmd.Parse("-b");
Assert.True((bool)res.GetValueForOption((Option)opt));

It works if you pass "true", e.g. -b true, or if you use the generic method directly.

I ended up adding this code to work around the issue:

// Here I only know that opt is some Option<>

if (opt.GetType() == typeof(Option<>).MakeGenericType(typeof(bool)))
{
    res = bindingContext.ParseResult.GetValueForOption((Option<bool>)opt);
}

Activity

  1. added
    bug
    on Feb 18, 2022
  2. self-assigned this
    on Feb 18, 2022
  3. added a commit that references this issue on Feb 19, 2022
    37116cc
  4. added this to the 2.0 GA milestone on Feb 19, 2022
  5. shaggygi commented on Feb 24, 2022

    @shaggygi
    Contributor

    @jonsequitur Had a related question on GetValueForOption...

    I have UseExceptionHandler setup to try and catch random errors.

    What is need to search for a particular global option and get its value when an exception hits? I've tried a few things, but keeps returning nulls.

    Parser parser = new CommandLineBuilder(new MyRootCommand())
        .UseDefaults()
        .UseExceptionHandler((exception, context) => {
    
        // Need to check and get a value for an optional global option here.
    }

    Any help is appreciated.

  6. jonsequitur commented on Feb 24, 2022

    @jonsequitur
    Contributor

    You should be able to use context.ParseResult.GetValueForOption(theOption).

  7. shaggygi commented on Feb 24, 2022

    @shaggygi
    Contributor

    @jonsequitur Okay. I see what I was doing wrong. As usual, I truly appreciate your help (and this API 😄). Thx

  8. added a commit that references this issue on Mar 6, 2024
    c2a4654
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Parser and Binderbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions