dotnet / dotnet/command-line-api
Mistyping CommandHandler.Create arguments should throw
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
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.
```csharp
command.AddOption(new Option("--file") {
Argument = new Argument().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.
Contributor guide
Assessment
This issue has not been assessed yet.