Cysharp / Cysharp/ConsoleAppFramework

Aliasing an option as itself causes a compile error

Open Beginner friendly
#240 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.2k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

I recently got into the habit of adding aliases to all of my commands, even if it did not change the default parameter name. I suppose it made my XML documentation look more like the help menu and made all of the parameters consistent. But I noticed if you add an alias that is the same as the name the generator automatically creates, you get a compile error.

For example, if you build this code:

using ConsoleAppFramework;

var app = ConsoleApp.Create();
app.Add<MyCommand>();

app.Run(args);

class MyCommand
{
    /// <param name="target">--target, The path to the file that is to be updated.</param>
    [Command("update-file")]
    public async Task Command(string target) { }
}

You will get this error:

ConsoleApp.Builder.g.cs(85,25): Error CS0152 : The switch statement contains multiple cases with the label value '"--target"'

And the generator will have made this code:

for (int i = 0; i < commandArgs.Length; i++)
{
    var name = commandArgs[i];

    switch (name)
    {
        case "--target":
        case "--target": // Error CS0152
        {
            if (!TryIncrementIndex(ref i, commandArgs.Length)) { ThrowArgumentParseFailed("target", commandArgs[i]); } else { arg0 = commandArgs[i]; }
            arg0Parsed = true;
            continue;
        }

It looks like the generator doesn't avoid adding an alias to the parsed command info if the alias matches the default name:

https://github.com/Cysharp/ConsoleAppFramework/blob/7f8ed06f1e1c762e5f9a1477eeb1f7e89ea0b32e/src/ConsoleAppFramework/Parser.cs#L865-L870

I would like to ask whether this is actually considered a bug, since I know the way I am using aliases is strange.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/ConsoleAppFramework/Parser.cs around lines 865-870 and reproduce the issue with the C# example from the report. Check the generated ConsoleApp.Builder.g.cs switch for duplicate "--target" cases; done means the aliasing example compiles without duplicate case labels.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.