Skip to content

Handler name-binding is not documented enough #859

Open
@Sejsel

Description

@Sejsel

The fact that CommandHandler.Create lambda parameter names have to match option names does not seem to be documented enough.

The only mention on the wiki seems to be here, which is next to a code example that does not even use a lambda function, so it may not be obvious on the first glance. There seems to be a lot of issues where people have run into it (#835, #782, #526, #628, ...), including me right now. I am still not even convinced this is a good way to design this API. It also seems like there are not enough errors thrown if names/types are mismatched (there are some open issues relating to this).

One thing that I believe would help a lot is explicitly mentioning this behavior in the Your first app with System.CommandLine sample.

Perhaps updating it like this would be enough:

Before

    rootCommand.Description = "My sample app";

    rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
    {
        Console.WriteLine($"The value for --int-option is: {intOption}");
        Console.WriteLine($"The value for --bool-option is: {boolOption}");
        Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
    });

After

    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<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
    {
        Console.WriteLine($"The value for --int-option is: {intOption}");
        Console.WriteLine($"The value for --bool-option is: {boolOption}");
        Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
    });

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions