Skip to content

Parser.Parse throws ArgumentOutOfRangeException with more than two unspecified arguments #1711

Closed

Description

@sbergen

Here is a minimal reproduction

var command = new Command("command")
{
    new Argument<bool>("a"),
    new Argument<int>("b"),
    new Argument<string>("c"),
    new Argument<double>("d"),
};

new Parser(command).Parse(new[] { "a" });

Here 1/4 arguments are passed (3 missing). The same will reproduce with e.g. 1/5 or 2/5 arguments, but not 1/3 or 2/4.
The types of the arguments also don't seem to matter.

Expected result: The method completes without throwing exceptions.
Actual result: An exception is thrown:

System.ArgumentOutOfRangeException
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.CommandLine.Parsing.ParseResultVisitor.ValidateAndConvertArgumentResults(IReadOnlyList`1 arguments, Int32 commandArgumentResultCount) in .../src/System.CommandLine/Parsing/ParseResultVisitor.cs:line 294
   at System.CommandLine.Parsing.ParseResultVisitor.Stop() in .../src/System.CommandLine/Parsing/ParseResultVisitor.cs:line 251
   at System.CommandLine.Parsing.ParseResultVisitor.Visit(SyntaxNode node) in .../src/System.CommandLine/Parsing/ParseResultVisitor.cs:line 56
   at System.CommandLine.Parsing.Parser.Parse(IReadOnlyList`1 arguments, String rawInput) in .../src/System.CommandLine/Parsing/Parser.cs:line 64
   at System.CommandLine.Tests.MyTest.MyTestCase() in .../src/System.CommandLine.Tests/MyTest.cs:line 19

The above stack trace is from current main branch.

Activity

  1. added a commit that references this issue on Apr 21, 2022
    a413bfa
  2. added a commit that references this issue on Apr 21, 2022
    689dff5
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions