You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Change default for Option.AllowMultipleArgumentsPerToken to false #1199
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.
changed the title [-]Change default for Option.AllowMultipleArgumentsPerToken to true[/-][+]Change default for Option.AllowMultipleArgumentsPerToken to false[/+]on Feb 23, 2021
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.
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).
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).
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:
//--------------------------------------------------------varsingleValueSingleArgPerToken=newOption<string>("--svsa"){AllowMultipleArgumentsPerToken=false};varresult=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.");//--------------------------------------------------------varsingleValueMultiArgsPerToken=newOption<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");//--------------------------------------------------------varmultiValueMultiArgsPerToken=newOption<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");//--------------------------------------------------------varmultiValueSingleArgPerToken=newOption<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");
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.
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.
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.
Activity
Pxtl commentedon Feb 23, 2021
Uh, am I reading this right? Heading doesn't match description - aren't you saying that
AllowMultipleArgumentsPerTokenshould befalseby default?jonsequitur commentedon Feb 23, 2021
Thank you for catching this! 😄
[-]Change default for Option.AllowMultipleArgumentsPerToken to true[/-][+]Change default for Option.AllowMultipleArgumentsPerToken to false[/+]fix dotnet#1199
fix #1199 (#1342)
database64128 commentedon Jul 27, 2021
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.
jonsequitur commentedon Jul 27, 2021
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 val2and--opt val1 val2), and that this PR aligned System.CommandLine to the clap behavior in that you have to opt into the latter (usingmultiple(true)orAllowMultipleArgumentsPerToken = true).database64128 commentedon Jul 27, 2021
.multiple(true), multiple occurrences and multiple values are allowed.--opt val1 --opt val2and--opt val1 val2will be accepted.--opt val1 --opt val2 sub-command.--opt val1 val2 sub-command,sub-commandis interpreted as a value of--opt.System.CommandLinebehavior whereAllowMultipleArgumentsPerToken = true..number_of_values(2), only one occurrence of--optis allowed and it takes exactly 2 values.--opt val1 val2will be accepted.--opt val1 --opt val2contains 2 occurrences of--optand will be rejected.--opt val1 val2 sub-command..multiple(true).number_of_values(1), multiple occurrences of--optare allowed, each occurrence takes exactly 1 value.--opt val1 --opt val2will be accepted.--opt val1 --opt val2 sub-command.AllowMultipleArgumentsPerToken = false.I'd argue we revert this change to keep the default aligned with
.multiple(true). SettingAllowMultipleArgumentsPerToken = falseis the equivalent of adding.number_of_values(1).jonsequitur commentedon Jul 27, 2021
Are you taking into account
Option.Arity? It looks to be comparable to clap'snumber_of_valuesAPI.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>versusOption<IEnumerable<string>>.AllowMultipleArgumentsPerTokenaffects 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:
🔼 Update System.CommandLine
ptr727 commentedon Jan 7, 2022
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.
jonsequitur commentedon Jan 7, 2022
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.
ptr727 commentedon Jan 7, 2022
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.
voroninp commentedon Sep 19, 2022
@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) ...'