Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Replace ValidateSymbolResult delegate with Action<T>, fixes #1944
  • Loading branch information
adamsitnik committed Nov 4, 2022
commit bc3146830eb47073af3ad40cde8171eb6cdf1a56
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ System.CommandLine
public System.Boolean HasDefaultValue { get; }
public System.String HelpName { get; set; }
public System.Type ValueType { get; }
public System.Void AddValidator(System.CommandLine.Parsing.ValidateSymbolResult<System.CommandLine.Parsing.ArgumentResult> validate)
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Object GetDefaultValue()
public System.Void SetDefaultValue(System.Object value)
Expand Down Expand Up @@ -60,7 +60,7 @@ System.CommandLine
public System.Void AddCommand(Command command)
public System.Void AddGlobalOption(Option option)
public System.Void AddOption(Option option)
public System.Void AddValidator(System.CommandLine.Parsing.ValidateSymbolResult<System.CommandLine.Parsing.CommandResult> validate)
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.CommandResult> validate)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Collections.Generic.IEnumerator<Symbol> GetEnumerator()
public static class CommandExtensions
Expand Down Expand Up @@ -194,7 +194,7 @@ System.CommandLine
public ArgumentArity Arity { get; set; }
public System.Boolean IsRequired { get; set; }
public System.Type ValueType { get; }
public System.Void AddValidator(System.CommandLine.Parsing.ValidateSymbolResult<System.CommandLine.Parsing.OptionResult> validate)
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Boolean HasAliasIgnoringPrefix(System.String alias)
public System.Void SetDefaultValue(System.Object value)
Expand Down Expand Up @@ -474,8 +474,3 @@ System.CommandLine.Parsing
public System.IAsyncResult BeginInvoke(System.String tokenToReplace, ref System.Collections.Generic.IReadOnlyList<System.String> replacementTokens, ref System.String& errorMessage, System.AsyncCallback callback, System.Object object)
public System.Boolean EndInvoke(ref System.Collections.Generic.IReadOnlyList<System.String> replacementTokens, ref System.String& errorMessage, System.IAsyncResult result)
public System.Boolean Invoke(System.String tokenToReplace, ref System.Collections.Generic.IReadOnlyList<System.String> replacementTokens, ref System.String& errorMessage)
public delegate ValidateSymbolResult<in T> : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
.ctor(System.Object object, System.IntPtr method)
public System.IAsyncResult BeginInvoke(T symbolResult, System.AsyncCallback callback, System.Object object)
public System.Void EndInvoke(System.IAsyncResult result)
public System.Void Invoke(T symbolResult)
10 changes: 5 additions & 5 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class Argument : Symbol, IValueDescriptor
private ArgumentArity _arity;
private TryConvertArgument? _convertArguments;
private List<ICompletionSource>? _completions = null;
private List<ValidateSymbolResult<ArgumentResult>>? _validators = null;
private List<Action<ArgumentResult>>? _validators = null;

/// <summary>
/// Initializes a new instance of the Argument class.
Expand Down Expand Up @@ -104,14 +104,14 @@ private protected override string DefaultName
}
}

internal List<ValidateSymbolResult<ArgumentResult>> Validators => _validators ??= new ();
internal List<Action<ArgumentResult>> Validators => _validators ??= new ();

/// <summary>
/// Adds a custom <see cref="ValidateSymbolResult{ArgumentResult}"/> to the argument. Validators can be used
/// Adds a custom validator to the argument. Validators can be used
/// to provide custom errors based on user input.
/// </summary>
/// <param name="validate">The delegate to validate the parsed argument.</param>
public void AddValidator(ValidateSymbolResult<ArgumentResult> validate) => Validators.Add(validate);
/// <param name="validate">The action to validate the parsed argument.</param>
public void AddValidator(Action<ArgumentResult> validate) => Validators.Add(validate);

/// <summary>
/// Gets the default value for the argument.
Expand Down
10 changes: 5 additions & 5 deletions src/System.CommandLine/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Command : IdentifierSymbol, IEnumerable<Symbol>
private List<Argument>? _arguments;
private List<Option>? _options;
private List<Command>? _subcommands;
private List<ValidateSymbolResult<CommandResult>>? _validators;
private List<Action<CommandResult>>? _validators;

/// <summary>
/// Initializes a new instance of the Command class.
Expand Down Expand Up @@ -68,8 +68,8 @@ public IEnumerable<Symbol> Children
/// </summary>
public IReadOnlyList<Command> Subcommands => _subcommands is not null ? _subcommands : Array.Empty<Command>();

internal IReadOnlyList<ValidateSymbolResult<CommandResult>> Validators
=> _validators is not null ? _validators : Array.Empty<ValidateSymbolResult<CommandResult>>();
internal IReadOnlyList<Action<CommandResult>> Validators
=> _validators is not null ? _validators : Array.Empty<Action<CommandResult>>();

internal bool HasValidators => _validators is not null; // initialized by Add method, so when it's not null the Count is always > 0

Expand Down Expand Up @@ -140,8 +140,8 @@ public void AddGlobalOption(Option option)
/// Adds a custom validator to the command. Validators can be used
/// to create custom validation logic.
/// </summary>
/// <param name="validate">The delegate to validate the symbols during parsing.</param>
public void AddValidator(ValidateSymbolResult<CommandResult> validate) => (_validators ??= new()).Add(validate);
/// <param name="validate">The action to validate the symbols during parsing.</param>
public void AddValidator(Action<CommandResult> validate) => (_validators ??= new()).Add(validate);

/// <summary>
/// Gets or sets a value that indicates whether unmatched tokens should be treated as errors. For example,
Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace System.CommandLine
public abstract class Option : IdentifierSymbol, IValueDescriptor
{
private string? _name;
private List<ValidateSymbolResult<OptionResult>>? _validators;
private List<Action<OptionResult>>? _validators;
private readonly Argument _argument;

internal Option(
Expand Down Expand Up @@ -112,15 +112,15 @@ public override string Name
}
}

internal List<ValidateSymbolResult<OptionResult>> Validators => _validators ??= new();
internal List<Action<OptionResult>> Validators => _validators ??= new();

internal bool HasValidators => _validators is not null && _validators.Count > 0;

/// <summary>
/// Adds a validator that will be called when the option is matched by the parser.
/// </summary>
/// <param name="validate">A <see cref="ValidateSymbolResult{OptionResult}"/> delegate used to validate the <see cref="OptionResult"/> produced during parsing.</param>
public void AddValidator(ValidateSymbolResult<OptionResult> validate) => Validators.Add(validate);
/// <param name="validate">An action used to validate the <see cref="OptionResult"/> produced during parsing.</param>
public void AddValidator(Action<OptionResult> validate) => Validators.Add(validate);

/// <summary>
/// Indicates whether a given alias exists on the option, regardless of its prefix.
Expand Down
13 changes: 0 additions & 13 deletions src/System.CommandLine/Parsing/ValidateSymbolResult.cs

This file was deleted.