Skip to content

Mistyping CommandHandler.Create arguments should throw #622

Open
@christianrondeau

Description

@christianrondeau

If you create an argument of one type and then specify another in the command handler, it will silently pass null, which makes it hard to catch and then to diagnose.

command.AddOption(new Option("--file") {
    Argument = new Argument<FileInfo>().ExistingOnly()
});

// ...

command.Handler = CommandHandler.Create((string file) =>
{
    // ...
});

Here the FileInfo cannot be cast to string and then fails silently.

I would propose strict arguments matching, i.e. if an argument is of the wrong type, missing or extraneous, it would immediately throw. Since this could potentially break usages, if you prefer this could also be an option, like command.Handler = CommandHandler.Create(...).Strict(), which would set a flag on the ICommandHandler. The ParseResultVisitor would then check that there's a 1:1 match between the handler and the options/arguments.

Since I already have a few PRs waiting, I'll wait for feedback before providing a PR, but I'm ready to make one if you believe this makes sense.

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