8000 Create a Roslyn analyzer to enforce matching argument/option names with command handler parameter names · Issue #872 · dotnet/command-line-api · GitHub
[go: up one dir, main page]

Skip to content
Create a Roslyn analyzer to enforce matching argument/option names with command handler parameter names #872
Closed
@jonsequitur

Description

@jonsequitur

The most common issue people encounter with the System.CommandLine API is when option or argument names/aliases do not match the parameters in a command handler:

var command= new RootCommand
{
    new Option<int>("--aaa")
};
command.Handler = CommandHandler.Create<int>(zzz => /*  zzz is always going to be 0  */); 

We haven't seen an approach that enforces a relationship between the parameters and the parser at compile time without an enormous increase in the complexity of the code, so we took a more dynamic approach, based on ASP.NET MVC controller methods. The consequence is that these mismatches can't be flagged by the compiler, and until 57C5 you're aware of the convention, it's easy to get this wrong.

Programmatic validation that all parameters are matchable on all command handlers at runtime has been proposed. This approach would be very expensive, especially when complex objects rather than scalar values are being bound, and since this won't vary after the code is compiled, it's not ideal to pay this cost every time a program runs.

An analyzer might be a more appropriate tool for the job, since it only needs to be validated when the code is being written. It could also include quick fixes for renaming parameters to match options and arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0