dotnet / dotnet/command-line-api

Inconsistent user intention guesses in error messages between single-dash option name vs. double-dash option name

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

Description

System.CommandLine version: 2.0.0-beta6.25320.118

Having a CLI with only a single-dash option `-s` that accepts exactly one value, and providing a commandline `-s foo bar` with an superfluous value "_bar_" causes this error message:
```
Commandline: -s foo bar
'bar' was not matched. Did you mean one of the following?
-h
-s
Unrecognized command or argument 'bar'.
```
Note that the suggestion/guess about the users intention is `-h` followed by `-s`.

Code with single-dash option name:
```C#
var singleDashOption = new Option("-s") { Arity = ArgumentArity.ExactlyOne };

var rootCmd = new RootCommand
{
singleDashOption
};

rootCmd.SetAction(parseResult => Console.WriteLine($"Value: {parseResult.GetValue(singleDashOption)}"));

var cmdLine = "-s foo bar";
Console.WriteLine($"Commandline: {cmdLine}");
rootCmd.Parse(cmdLine).Invoke();
```


However, now change the option name from the single-dash name `-s` to the double-dash name `--str`, and the error message is guessing a different user intention:
```
Commandline: --str foo bar
'bar' was not matched. Did you mean one of the following?
-h
Unrecognized command or argument 'bar'.
```
Note the absence of `--str` in the guess of the user intention, which looks inconsistent with the error message given when the option was having a single-dash name.

Same code, but with double-dash option name:

```C#
var doubleDashOption = new Option("--str") { Arity = ArgumentArity.ExactlyOne };

var rootCmd = new RootCommand
{
doubleDashOption
};

rootCmd.SetAction(parseResult => Console.WriteLine($"Value: {parseResult.GetValue(doubleDashOption)}"));

var cmdLine = "--str foo bar";
Console.WriteLine($"Commandline: {cmdLine}");
rootCmd.Parse(cmdLine).Invoke();
```


------
Update:

For completeness sake, these are the user intention guesses in the error messages for option names `--s` and `-str` as well:
```
Commandline: --s foo bar
'bar' was not matched. Did you mean one of the following?
-h
--s
```

```
Commandline: -str foo bar
'bar' was not matched. Did you mean one of the following?
-h
-str
```

Thus, getting the (slightly) different user intention guess requires the option name to be double-dash _and_ consist of multiple letters.

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.