dotnet / dotnet/command-line-api

Symbols in string are parsed in command

Open
#1,452 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Version="2.0.0-beta1.21308.1"

Given:
`new Option(alias: "--option")`, or
`new Argument(name: "argument")`

The following:
`command --option "-h"`, and
`command "-h"`

yield the action for the help command, displaying help text to the user. Attempting to pass a string containing another option parses that option instead of treating it as a string.

Expected:
`option` and `argument` parameters to the command handler contain `"-h"`.

Actual:
The string is parsed as a command.

Also if it wasn't clear this isn't exclusive to the help command. If I specify `new Option(alias: "-i")` and run with `--option \"-i\""` it will try to parse that as my '-i' option instead of a string to the '--option' option.

Is there a way to ignore parsing for a particular symbol?

Full program:
```csharp
private static async Task Main(string[] args)
{
void commandHandler(string argument, string option, IConsole console)
{
console.Out.WriteLine($"Argument: {argument}");
console.Out.WriteLine($"Option: {option}");
}

var command = new Command("command")
{
new Argument(name: "argument"),
new Option(alias: "--option")
};

command.Handler = CommandHandler.Create(commandHandler);

var root = new RootCommand()
{
command
};

// return await root.InvokeAsync("command \"-h\" --option \"opt\""); // triggers help
// return await root.InvokeAsync("command \"arg\" --option \"-h\""); // triggers help
// return await root.InvokeAsync("command \"arg\" --option \"opt\""); // arg, opt
return await root.InvokeAsync(args);
}
```

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.