Skip to content

Duplicate "--version" keys #1691

Open
Open
@mmckechney

Description

@mmckechney

After updating to the latest release (System.CommandLine v2.0.0-beta3.22114.1), I am now getting the following error at runtime:

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: --version
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.CommandLine.Parsing.StringExtensions.ValidTokens(Command command)
   at System.CommandLine.Parsing.StringExtensions.Tokenize(IReadOnlyList`1 args, CommandLineConfiguration configuration)
   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)

No other changes were made to the app that was previously working. I do use System.CommandLine.NamingConventionBinder to be compatible with the older CommandHandler.Create model, could this be the problem?

Activity

jonsequitur

jonsequitur commented on Mar 29, 2022

@jonsequitur
Contributor

Can you provide a code sample to reproduce this?

mmckechney

mmckechney commented on Mar 29, 2022

@mmckechney
Author

Full sample code can be found here: https://gist.github.com/mmckechney/7bd92a4417458fb8b919b44bac5c5b89

In creating this sample I discovered the root of the problem: The use of the CommandLineBuilder to add additional help context breaks when you include UseDefaults() with the latest Beta3 release.

//This throws exception with: "One or more errors occurred. (An item with the same key has already been added. Key: --version)'"
  var parser = new CommandLineBuilder(rootCommand)
                .UseDefaults()
                .UseHelp(ctx =>
                {
                    ctx.HelpBuilder.CustomizeLayout(_ => System.CommandLine.Help.HelpBuilder.Default
                                             .GetLayout()
                                             .Prepend(
                                                 _ => _.Output.WriteLine("**Adding extra help here**")
                                             ));
                }).Build();

However: I also noticed that if you remove the UseDefaults(), the program runs, but Prepend help text does not get displayed. Is there a new method to prepend to your help message or is this a bug?

//This runs, but the help output is not modified as expected
  var parser = new CommandLineBuilder(rootCommand)
                .UseHelp(ctx =>
                {
                    ctx.HelpBuilder.CustomizeLayout(_ => System.CommandLine.Help.HelpBuilder.Default
                                             .Prepend(
                                                 _ => _.Output.WriteLine("**Adding extra help here**")
                                             ));
                }).Build();
added this to the 2.0 GA milestone on Apr 10, 2022
zachgharst

zachgharst commented on May 21, 2022

@zachgharst

Is there an update on this other than the milestone tag? Do we have any way to work around this? I'm experiencing the same problem. I just want to add a logo and copyright before description.

jonsequitur

jonsequitur commented on May 22, 2022

@jonsequitur
Contributor

I'm not seeing this issue when calling Invoke on the Parser returned from CommandLineBuilder.Build.

I do see it when I call Invoke directly on the RootCommand.

That should allow people to work around this while we sort out the issue.

modified the milestones: 2.0 GA, 2.1 on Jun 9, 2022
perlun

perlun commented on Oct 12, 2022

@perlun

For reference, this was also spotted by another user some months later: #1791 (comment)

perlun

perlun commented on Oct 12, 2022

@perlun

I am running into this in my application, but perhaps for slightly different reasons than the rest of you. I have a custom --version option defined like this (heavily simplified example):

var versionOption = new Option<VoidObject>(new[] { "--version", "-v" }, "Show version information");

var rootCommand = new RootCommand { ... };
rootCommand.AddOption(versionOption);

return new CommandLineBuilder(rootCommand)
    .UseDefaults() // <-- this triggers the problem
    .Build()
    .Invoke(args, console);

Changing from .UseDefaults() to .UseHelp() works in my case (but some of my application's options still causes other exceptions, likely because of some other incompatibility since the upgrade).

5 remaining items

Loading
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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @notes2c@jonsequitur@perlun@zachgharst@mmckechney

        Issue actions

          Duplicate "--version" keys · Issue #1691 · dotnet/command-line-api