Skip to content

ArgumentResult.OnlyTake distribute not consumed tokens in unexpected way #2734

@PetSerAl

Description

@PetSerAl

Reproduce

#:package System.CommandLine@2.0.0

using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;

Func<ArgumentResult, string[]> takeTwo = r => {
    r.OnlyTake(2);
    return r.Tokens.Select(t => t.Value).ToArray();
};
RootCommand root = new() {
    new Argument<string[]>("arg1") { Arity = new(1, 15), CustomParser = takeTwo },
    new Argument<string[]>("arg2") { Arity = new(1, 3), CustomParser = takeTwo },
    new Argument<string[]>("arg3") { Arity = new(1, 3), CustomParser = takeTwo },
    new Argument<string[]>("arg4") { Arity = new(1, 3), CustomParser = takeTwo },
    new Argument<string[]>("arg5") { Arity = new(1, 3), CustomParser = takeTwo },
    new Argument<string[]>("arg6") { Arity = new(1, 10) },
};
var result = root.Parse(Enumerable.Range(1, 20).Select(i => i.ToString()).ToArray());
foreach(Argument<string[]> a in root.Arguments) {
    Console.WriteLine($"{a.Name}: {string.Join(", ", result.GetRequiredValue(a))}");
}

https://lab.razor.fyi/#rZPRahQxFIYpCtVcFZ_gMHqRwBB2t60XnW5xWVQopZbdwYIiGKenY9iZpD1J3C7LvIM3vkpfQ_EBfBbJ7mypWvHCvUz-_P_5Q76wH5uMnZAtSdWycI--bj7eu1DFRJUI45nzWMuhrWtlzo60wWc92ZEdxoLTpmz17JfV7dN_V-SJoij9duJIm8uMsRfBFPsDKkONxo_Qhcqn4DxpU759dwBeTTCfWugDQf8A5gwAgOQrU81yNUHeE9lyC30gAyRzO0Hj5BgrLDz30eTla1UFFDK3AyI14yJjTcZG1vq2JpC1HvpgcMpFO8TgFFa99m8K8URR2U0EzGFA2s9aUzeF7q5IYRict3W8MBL0b9o36b8ie3dFbv9P4vbaE3fWnri79sSndz5NR0Rrk7FPioAWkEWirPULOJE_N6FGUh8qlCNlSoyuXkesMNIRIy1zO14M4-IWSyJj55ZQFR_5n51AgTbLQSvRrQAbWuNshfKUtMf4UfiTZK7ksaqx2YP5MkMeWm14kkKStsXlS_QjvAya8GzBNVdCNElkmh3eo2DePHxw_f3L52_nW_ffb1xt_AQ

Expected output

arg1: 1, 2
arg2: 3, 4
arg3: 5, 6
arg4: 7, 8
arg5: 9, 10
arg6: 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Actual output

arg1: 1, 2
arg2: 16, 17
arg3: 19, 20
arg4: 4, 5
arg5: 7, 8
arg6: 10, 11, 12, 13, 14, 15, 18, 3, 6, 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions