Skip to content

Why Option exposes IsRequired while it also exposes Arity? #2082

Closed

Description

@adamsitnik

Currently there are two ways of saying that given Option is required:

  • by setting option.IsRequired to true
  • by setting option.Arity to an arity with MinimumNumberOfValues >= 1.

We also have two different errors for both cases: OptionRequiredArgumentMissing and RequiredOptionWasNotProvided.

I need to understand it better to provide good implementation for #2052

Activity

  1. added
    question
    on Mar 7, 2023
  2. jonsequitur commented on Mar 7, 2023

    @jonsequitur
    Contributor

    Option.Arity describes how many argument tokens the option requires if the option is provided. It only has significance when the option is present on the command line being parsed.

    image

    Option.IsRequired indicates whether an option must be provided. Only when it's provided are the arity rules meaningful.

    image

  3. adamsitnik commented on Mar 8, 2023

    @adamsitnik
    MemberAuthor

    @jonsequitur how does it work when it comes to Argument, which exposes Arity but no IsRequired?

    Currently the RequiredArgumentMissing error is reported for the CommandResult which owns Argument, rather than for the ArgumentResult itself (it's not created at all).
    Because of that, ParseResult.FindResultFor(argument) returns null and ParseResult.GetValue(argument) returns default.

    Argument<int> argument = new ("arg");
    Command command = new("name") { argument };
    
    ParseResult parseResult = command.Parse("");
    foreach (var error in parseResult.Errors)
        Console.WriteLine($"{error.Message}, SymbolResult: {error.SymbolResult}");
    
    Console.WriteLine($"Value: {parseResult.GetValue(argument)}");
    Required argument missing for command: 'name'., SymbolResult: CommandResult: name
    Value: 0

    This seems to be by design:

    [Fact]
    public void String_defaults_to_null_when_not_specified()
    {
    var argument = new Argument<string>("arg");
    var command = new Command("mycommand")
    {
    argument
    };
    var result = command.Parse("mycommand");
    result.GetValue(argument)
    .Should()
    .BeNull();
    }

    but it's not intuitive for me (we have a parse error related to given argument, but it's not thrown when value is being requested).

  4. jonsequitur commented on Mar 8, 2023

    @jonsequitur
    Contributor

    @jonsequitur how does it work when it comes to Argument, which exposes Arity but no IsRequired?

    Options are optional by default, whereas arguments are required by default.

    The test does look wrong. The default arity for that argument is {1,1} which means there should be an error there.

  5. adamsitnik commented on Mar 8, 2023

    @adamsitnik
    MemberAuthor

    @jonsequitur big thanks for all the answers, they were very helpful!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Parser and Binderquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions