Skip to content

Change default for Option.AllowMultipleArgumentsPerToken to false #1199

Closed

Description

@jonsequitur

The behavior of allowing multiple tokens to parse to a single option is uncommon in practice. This is currently enabled by default but should probably be toggled to disabled by default.

Related discussion: #1162.

Activity

  1. added this to the 2.0 GA milestone on Feb 16, 2021
  2. Pxtl commented on Feb 23, 2021

    @Pxtl

    Uh, am I reading this right? Heading doesn't match description - aren't you saying that AllowMultipleArgumentsPerToken should be false by default?

  3. jonsequitur commented on Feb 23, 2021

    @jonsequitur
    ContributorAuthor

    Thank you for catching this! 😄

  4. changed the title [-]Change default for Option.AllowMultipleArgumentsPerToken to true[/-] [+]Change default for Option.AllowMultipleArgumentsPerToken to false[/+] on Feb 23, 2021
  5. self-assigned this
    on Jul 4, 2021
  6. added a commit that references this issue on Jul 6, 2021
    28ad591
  7. added a commit that references this issue on Jul 7, 2021
    db332c6
  8. database64128 commented on Jul 27, 2021

    @database64128
    Contributor

    clap, the command line argument parser used by many popular Rust CLI apps, accepts both multiple occurrences (--opt val1 --opt val2) and multiple values (--opt val1 val2) by default with .multiple(true), and can be limited to one value per occurrence with .number_of_values(1). The behavior is well documented.

    I think this is too much of a breaking change for existing apps. And it's not really helpful in solving the ambiguity issue. I'd like to see this change reverted.

  9. jonsequitur commented on Jul 27, 2021

    @jonsequitur
    ContributorAuthor

    Can you clarify the difference between the clap and System.CommandLine capabilities? It looks to me like both of them support both option occurrence patterns (--opt val1 --opt val2 and --opt val1 val2), and that this PR aligned System.CommandLine to the clap behavior in that you have to opt into the latter (using multiple(true) or AllowMultipleArgumentsPerToken = true).

  10. database64128 commented on Jul 27, 2021

    @database64128
    Contributor

    Can you clarify the difference between the clap and System.CommandLine capabilities? It looks to me like both of them support both option occurrence patterns (--opt val1 --opt val2 and --opt val1 val2), and that this PR aligned System.CommandLine to the clap behavior in that you have to opt into the latter (using multiple(true) or AllowMultipleArgumentsPerToken = true).

    • With only .multiple(true), multiple occurrences and multiple values are allowed.
      • Both --opt val1 --opt val2 and --opt val1 val2 will be accepted.
      • There's no ambiguity in --opt val1 --opt val2 sub-command.
      • In --opt val1 val2 sub-command, sub-command is interpreted as a value of --opt.
      • This aligns with the previous System.CommandLine behavior where AllowMultipleArgumentsPerToken = true.
    • With only .number_of_values(2), only one occurrence of --opt is allowed and it takes exactly 2 values.
      • Only --opt val1 val2 will be accepted. --opt val1 --opt val2 contains 2 occurrences of --opt and will be rejected.
      • There's no ambiguity in --opt val1 val2 sub-command.
    • With .multiple(true).number_of_values(1), multiple occurrences of --opt are allowed, each occurrence takes exactly 1 value.
      • Only --opt val1 --opt val2 will be accepted.
      • There's no ambiguity in --opt val1 --opt val2 sub-command.
      • This aligns with the new default behavior where AllowMultipleArgumentsPerToken = false.

    I'd argue we revert this change to keep the default aligned with .multiple(true). Setting AllowMultipleArgumentsPerToken = false is the equivalent of adding .number_of_values(1).

  11. jonsequitur commented on Jul 27, 2021

    @jonsequitur
    ContributorAuthor

    Are you taking into account Option.Arity? It looks to be comparable to clap's number_of_values API.

    In System.CommandLine, the number of values allowed is orthogonal to the number of times an option token can be specified. This definitely needs and will get better documentation. But the number of values, i.e. arity, affects the binding to a the target type, like Option<string> versus Option<IEnumerable<string>>. AllowMultipleArgumentsPerToken affects whether the option token, e.g. -a, needs to be repeated on the command line in order to add (max arity > 1) or overwrite (max arity = 1) values.

    The following illustrates the different combinations of behaviors:

    //--------------------------------------------------------
    var singleValueSingleArgPerToken = new Option<string>("--svsa") { AllowMultipleArgumentsPerToken = false };
    
    var result = singleValueSingleArgPerToken.Parse("--svsa value1");
    result.ValueForOption(singleValueSingleArgPerToken).Should().Be("value1");
    result = singleValueSingleArgPerToken.Parse("--svsa value1 --svsa value2");
    result
        .Invoking(r => r.ValueForOption(singleValueSingleArgPerToken))
        .Should()
        .Throw<InvalidOperationException>()
        .Which
        .Message
        .Should().Be("Option '--svsa' expects a single argument but 2 were provided.");
    
    //--------------------------------------------------------
    var singleValueMultiArgsPerToken = new Option<string>("--svma") { AllowMultipleArgumentsPerToken = true };
    
    // When allowing multiple arguments per token, the last value wins. 
    result = singleValueMultiArgsPerToken.Parse("--svma value1 --svma value2");
    result.ValueForOption(singleValueMultiArgsPerToken).Should().Be("value2");
    
    // When the token isn't repeated though, the next value is not consumed by this option. (It might be consumed as an argument by a parent command.)
    result = singleValueMultiArgsPerToken.Parse("--svma value1 value2");
    result.ValueForOption(singleValueMultiArgsPerToken).Should().Be("value1");
    
    //--------------------------------------------------------
    var multiValueMultiArgsPerToken = new Option<IEnumerable<string>>("--mvma") { AllowMultipleArgumentsPerToken = true };
    
    result = multiValueMultiArgsPerToken.Parse("--mvma value1 --mvma value2");
    result.ValueForOption(multiValueMultiArgsPerToken).Should().BeEquivalentTo("value1", "value2");
    
    result = multiValueMultiArgsPerToken.Parse("--mvma value1 value2");
    result.ValueForOption(multiValueMultiArgsPerToken).Should().BeEquivalentTo("value1", "value2");
    
    //--------------------------------------------------------
    var multiValueSingleArgPerToken = new Option<IEnumerable<string>>("--mvsa") { AllowMultipleArgumentsPerToken = false };
    
    result = multiValueSingleArgPerToken.Parse("--mvsa value1 --mvsa value2");
    result.ValueForOption(multiValueSingleArgPerToken).Should().BeEquivalentTo("value1", "value2");
    
    result = multiValueSingleArgPerToken.Parse("--mvsa value1 value2");
    result.ValueForOption(multiValueSingleArgPerToken).Should().BeEquivalentTo("value1");
  12. added a commit that references this issue on Aug 3, 2021
  13. ptr727 commented on Jan 7, 2022

    @ptr727

    FYI, this change broke my code that use a list type.
    I expect any iterator type to automatically expect a variable number of following values, I should not have to manually enable it.

  14. jonsequitur commented on Jan 7, 2022

    @jonsequitur
    ContributorAuthor

    The requirement to respecify the option token (-x 1 -x 2) is a lot more common than allowing multiple arguments per option token (-x 1 2), hence the change to the default.

    The latter leads to the ambiguity described in #1162. The change in behavior avoids this user-facing bug, but we've left the option to use the old behavior in order to allow developers to meet their backwards compatibility needs.

  15. ptr727 commented on Jan 7, 2022

    @ptr727

    I understand the problem of using options after a multi-arg option, which is why the multi-args are typically last, or the parser evaluates each argument for not starting with an option token.
    Anyway, thx, behavior restored to maintain backwards compatibility.

  16. voroninp commented on Sep 19, 2022

    @voroninp

    @jonsequitur , thanks for detailed explanation, but what should be the setting for the option which expects an array of tuples (locations in my case)?

    `cmd --locations (x,y) (x,y) ...'

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions