dotnet / dotnet/command-line-api

'ArgumentResult.OnlyTake()' doesn't work with ArgumentArity.MaximumNumberOfValues

Open
#1,814 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

This same test [Custom_parser_can_pass_on_remaining_tokens](https://github.com/dotnet/command-line-api/blob/main/src/System.CommandLine.Tests/ArgumentTests.cs#L518) where a custom parser tries uses `.OnlyTake()` (`ArgumentResult.PassedOnTokens`) doesn't work (PassedOnTokens just disappear) if the first argument has ArgumentArity.MaximumNumberOfValues:

```cs
[Fact]
public void When_custom_parser_with_max_arity_passes_on_tokens_the_argument_result_tokens_reflect_the_change()
{

// Argument1 can accept up to 5, but we'll OnlyTake 3
var argument1 = new Argument(
"one",
result =>
{
result.OnlyTake(3);

return new[]
{
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
})
{ Arity = new ArgumentArity(0, 5) } ; /// THIS is new

var argument2 = new Argument(
"two",
result => result.Tokens.Select(t => t.Value).Select(int.Parse).ToArray());
var command = new RootCommand
{
argument1,
argument2
};

var parseResult = command.Parse("1 2 3 4 5 6 7 8");

// 1/2/3 works.
parseResult.FindResultFor(argument1)
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "1", "2", "3" },
options => options.WithStrictOrdering());

// 4/5/6 disappear - argument2 get only tokens 6/7/8
parseResult.FindResultFor(argument2)
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
options => options.WithStrictOrdering());
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.