8000 CommandHandler.Create without name matching by jonsequitur · Pull Request #1012 · dotnet/command-line-api · GitHub
[go: up one dir, main page]

Skip to content

CommandHandler.Create without name matching #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jonsequitur
Copy link
Contributor
@jonsequitur jonsequitur commented Aug 6, 2020

The approach I'm experimenting with here is to avoid the most common gotcha in the System.CommandLine API, which is the name-based matching of options/arguments to their corresponding parameters in a command handler.

Given:

var stringOption = new Option<string>("--the-string");
var intOption = new Option<int>("--the-int");
var filesArg = new Argument<FileInfo[]>("the-files");
var command = new RootCommand
{
    stringOption,
    intOption,
    filesArg
};

Handler with name-based matching:

command.Handler = 
    // You have to specify the types and they have to match the options/arguments:
    CommandHandler.Create<string, int, FileInfo[]>( 
        // The names must match some alias of the symbol:
        (theString, theInt, theFiles) => 
        {
            
        });

Handler with the new proposed API:

command.Handler = 
    // Types are inferred:
    CommandHandler.Create(
        stringOption, intOption, filesArg,
        // The names don't matter:
        (s, i, fs) => 
        {
            
        });

Pros:

  • No more failed binding because of naming mismatches.
  • Type inference means it's all strongly-typed without having to repeat information, and no worries about a type mismatch.

Cons / design problems to be solved:

  • No current solution for complex model binding, since parameters map one-to-one with options/arguments, meaning we need a lot more overloads. (People have asked for this anyway since the complex model binding is non-obvious.)
  • No current solution for service parameters, e.g. IConsole, ParseResult, etc.

Please discuss.

@ohadschn
Copy link

The implementation looks exactly like my workaround here so I'm definitely on board.

@brettfo brettfo changed the base branch from master to main August 19, 2020 19:53
@jonsequitur jonsequitur force-pushed the CommandHandler.Create-without-name-matching branch from 3157e68 to fa4b0ca Compare August 28, 2020 17:22
@jonsequitur jonsequitur force-pushed the CommandHandler.Create-without-name-matching branch 2 times, most recently from c2c0051 to 67d3c63 Compare February 24, 2021 17:46
@jonsequitur jonsequitur changed the title [WIP] CommandHandler.Create without name matching CommandHandler.Create without name matching Mar 7, 2021
@jonsequitur jonsequitur requested a review from Keboo March 7, 2021 16:37

public interface IValueDescriptor<T> : IValueDescriptor
{
// FIX: (IValueDescriptor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this needs to be addressed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0