dotnet / dotnet/command-line-api

Problems with Options "expects a single argument but 2 were provided" although not multiple arguments provided

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

Description

I created a small and simple snipped for demonstrating the issue
```csharp
static int Main(string[] args)
{
// Create a root command with some options
var rootCommand = new RootCommand
{
new Option(
new [] { "--CssFile", "-css"},
getDefaultValue: () => "42",
description: "An option whose argument is parsed as a string"),
new Option(
new [] {"--CollectionUri", "--Collection", "-c"},
getDefaultValue: () => "82",
"An option whose argument is parsed as a string"),
};

rootCommand.Description = "My sample app";

// Note that the parameters of the handler method are matched according to the names of the options
rootCommand.Handler = CommandHandler.Create((cssFile, collectionUri) =>
{
Console.WriteLine($"The value for --CssFile is: {cssFile}");
Console.WriteLine($"The value for --Collection is: {collectionUri}");
});

// Parse the incoming args and invoke the handler
return rootCommand.InvokeAsync(args).Result;
}
```

checking the usage looks just fine
```
ConsoleApp.CommandLine:
My sample app

Usage:
ConsoleApp.CommandLine [options]

Options:
-css, --CssFile An option whose argument is parsed as an int [default: 42]
-c, --Collection, --CollectionUri An option whose argument is parsed as a int [default: 82]
--version Show version information
-?, -h, --help Show help and usage information
```

running with these Arguments
`--CssFile "Default.css" --Collection "https://example.org/segment"` or
`--CssFile "Default.css" -c "https://example.org/segment"`

produces the expected result
```
The value for --CssFile is: Default.css
The value for --Collection is: https://example.org/segment
```

running with Arguments
`-css "Default.css" -c "https://example.org/segment"` or
`-css "Default.css" --Collection "https://example.org/segment"`
produces
```
Option '-c' expects a single argument but 2 were provided.
Option '-c' expects a single argument but 2 were provided.
Unrecognized command or argument 'Default.css'
```

am I doing something wrong?

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.