dotnet / dotnet/command-line-api
Mistyping CommandHandler.Create arguments should throw
- Lingua principale
- C#
- Stelle
- 3.7k
- Fork
- 428
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia leggendo CommandHandler.Create e l’implementazione di ICommandHandler, quindi traccia il modo in cui ParseResultVisitor associa le opzioni e gli argomenti analizzati ai parametri dell’handler. Determina se il matching rigoroso debba essere opt-in e definisci il comportamento per i tipi errati, mancanti ed estranei. Il lavoro è completato quando le discrepanze causano un errore immediato invece di produrre silenziosamente null.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- cli
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100