Skip to content

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

Open
@BobSilent

Description

@BobSilent

I created a small and simple snipped for demonstrating the issue

static int Main(string[] args)
{
    // Create a root command with some options
    var rootCommand = new RootCommand
    {
        new Option<string>(
                new [] { "--CssFile", "-css"},
                getDefaultValue: () => "42",
                description: "An option whose argument is parsed as a string"),
        new Option<string>(
                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<string, string>((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 <CssFile>                            An option whose argument is parsed as an int [default: 42]
  -c, --Collection, --CollectionUri <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?

Activity

jonsequitur

jonsequitur commented on Jan 20, 2021

@jonsequitur
Contributor

What's happening here is POSIX-style argument unbundling. The parser sees the token -css as possibly being equivalent to -c -s -s. Since there's no valid symbol matching -s, though, this could be a bug. We'll want to take a look at whether changing this behavior presents a forward/backward compatibility issue for command line tools that add or remove symbols over time. But I suspect this is a bug.

That said, how important is the -css symbol? --css would be more common for multi-letter option names and would avoid this problem.

BobSilent

BobSilent commented on Jan 21, 2021

@BobSilent
ContributorAuthor

I think at the moment i can live with your suggestion and change the -css to --css.
maybe it would worth to spend here a little bit of documentation for best practices or "do's and don'ts"

jonsequitur

jonsequitur commented on Jan 29, 2021

@jonsequitur
Contributor

FYI @BobSilent, your original issue should be resolved by #1177. Packages will be published shortly on the daily feed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonsequitur@BobSilent

        Issue actions

          Problems with Options "expects a single argument but 2 were provided" although not multiple arguments provided · Issue #1169 · dotnet/command-line-api