Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Command validators don't seem to use the default value factory of an Option

Aperta
#2,841 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
csharp
Ambito
cli

Direzione di ricerca

Inizia tracciando come i Validators a livello di comando usano result.GetValue(_multiplier) e come viene applicato il DefaultValueFactory di un'Option. Confronta questo comportamento con i validatori a livello di opzione e con GetValueOrDefault(); in questo modo viene stabilita la gestione prevista delle opzioni omesse e la discrepanza osservata è coperta dal comportamento di convalida pertinente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I've been on 2.0.0-beta4 for a while, and only recently gotten around to update to 2.0.10. One of the more interresting changes has been how validations work. It bugged me that I only had a single error message return and had to join multiple ones myself if there was more than one issue; but new API with result.AddError makes this a lot nicer.

However, I noticed that my old command-level validator didn't work anymore:

internal sealed class MySubCommand : Command
{
  private readonly Option<int> _multiplier = new("-m", "--multiplier") { Description = "Value multiplier, must be a positive non-zero value", DefaultValueFactory = _ => 1);
  public MySubCommand() : base("mysub", "Babies first subcommand")
  {
    Add(_multiplier);
    Validators.Add(result =>
    {
      if (result.GetValue(_multiplier) <= 0)
        result.AddError("Multiplier must be greater than 0.");
    });
    SetAction(Handle);
  }
  public int Handle(ParseResult parseResult) { /* ... */ }
}

As it turns out, that would return 0 (the default value for int) rather than what DefaultValueFactory would give me.
The -m argument is generally optional; but when it's specified I need it to be positive/non-zero.

In my case though, the fix is simple: Put the validation on the option itself (which wasn't a thing before; or I just overlooked it):

-    Validators.Add(result =>
+   _multiplier.Validators.Add(result =>
     {
       if (result.GetValue(_multiplier) <= 0)
         result.AddError("Multiplier must be greater than 0.");
     });

(Which could even go and use result.GetValueOrDefault<int>() instead, since it's specific to the option that way.)

In 2.0.0-beta4, this was simply:

AddValidator(result =>
{
  if (result.GetValueForOption(_multiplier) <= 0)
    result.ErrorMessage = "Multiplier must be greater than 0.";
});

(Which felt straight-forward to migrate over, since there was no real mention of this behavior in the 2.0.0-beta5 migration guide. And the fact that I had to keep my own error list if I did more than one validation in there; but I omitted that for brevity.)

And that made me wonder: Since command-level validations are intended for cross-argument checks (like, if related arguments like ranges or from/to etc. are passed; whether they work in combination), wouldn't this potentially cause subtle bugs if someone expected the result to be as produced by the DefaultValueFactory? Is this the intended behavior of the command-level validator?

Lingua principale
C#
Stelle
3.7k
Fork
434
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/command-line-api

Tutte le issue di dotnet/command-line-api

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.