Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
delay List<Validators> allocation
  • Loading branch information
adamsitnik committed Nov 10, 2022
commit 2411731afc3166cc00276cec38434f364c22eaaf
5 changes: 3 additions & 2 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ private protected override string DefaultName
}
}

internal List<Action<ArgumentResult>> Validators => _validators ??= new ();
internal IReadOnlyList<Action<ArgumentResult>> Validators
=> _validators is null ? Array.Empty<Action<ArgumentResult>>() : _validators;

/// <summary>
/// Adds a custom validator to the argument. Validators can be used
/// to provide custom errors based on user input.
/// </summary>
/// <param name="validate">The action to validate the parsed argument.</param>
public void AddValidator(Action<ArgumentResult> validate) => Validators.Add(validate);
public void AddValidator(Action<ArgumentResult> validate) => (_validators ??= new()).Add(validate);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places that call Validator.Add, which would now add the Action to the Array.Empty<Action>.

Validators.Add(validate);

option.Argument.Validators.Add(Validate.FileExists);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that #1966 is merged, this shoud go on Argument<T>.


/// <summary>
/// Gets the default value for the argument.
Expand Down