dotnet / dotnet/command-line-api

Having a sub-command with a name that matches its parent crashes the parser

Open
#2,260 2 comments 0 reactions 0 assignees View on GitHub
Area-Parser and Binder bug
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

```csharp
using System.CommandLine;

var rootCommand = new RootCommand();
var cmd = new Command("test");
var cmd2 = new Command("test");

cmd.AddCommand(cmd2);
rootCommand.AddCommand(cmd);

return await rootCommand.InvokeAsync(args);
```

When trying to build the command structure above, the parser throws the below exception:

```
Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: test
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at System.CommandLine.Parsing.StringExtensions.ValidTokens(Command command)
at System.CommandLine.Parsing.StringExtensions.Tokenize(IReadOnlyList`1 args, CommandLineConfiguration configuration, Boolean inferRootCommand)
at System.CommandLine.Parsing.Parser.Parse(IReadOnlyList`1 arguments, String rawInput)
at System.CommandLine.CommandExtensions.GetDefaultInvocationPipeline(Command command, String[] args)
at System.CommandLine.CommandExtensions.InvokeAsync(Command command, String[] args, IConsole console)
at Program.$(String[] args) in ...
```

System.CommandLine version: `2.0.0-beta4.22272.1`

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.