From 8884c10542c104aa4386c0679966a7246ca366d6 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 14 Nov 2022 19:59:57 +0100 Subject: [PATCH] make AddValidator fluent, fixes #1965 --- ...System_CommandLine_api_is_not_changed.approved.txt | 4 ++-- src/System.CommandLine.Tests/ArgumentTests.cs | 1 + src/System.CommandLine.Tests/OptionTests.cs | 1 + src/System.CommandLine/Argument.cs | 7 ------- src/System.CommandLine/Argument{T}.cs | 11 +++++++++++ src/System.CommandLine/Option.cs | 6 ------ src/System.CommandLine/OptionValidation.cs | 6 +++--- src/System.CommandLine/Option{T}.cs | 10 ++++++++++ 8 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index 3600f22c07..0da540bf5f 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -5,7 +5,6 @@ System.CommandLine public System.Boolean HasDefaultValue { get; } public System.String HelpName { get; set; } public System.Type ValueType { get; } - public System.Void AddValidator(System.Action validate) public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.Object GetDefaultValue() public ParseResult Parse(System.String commandLine) @@ -28,6 +27,7 @@ System.CommandLine public Argument AddCompletions(System.String[] completions) public Argument AddCompletions(System.Func> completionsDelegate) public Argument AddCompletions(System.Func> completionsDelegate) + public Argument AddValidator(System.Action validate) public struct ArgumentArity : System.ValueType, System.IEquatable public static ArgumentArity ExactlyOne { get; } public static ArgumentArity OneOrMore { get; } @@ -193,7 +193,6 @@ System.CommandLine public ArgumentArity Arity { get; set; } public System.Boolean IsRequired { get; set; } public System.Type ValueType { get; } - public System.Void AddValidator(System.Action validate) public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.Boolean HasAliasIgnoringPrefix(System.String alias) public ParseResult Parse(System.String commandLine) @@ -213,6 +212,7 @@ System.CommandLine public Option AddCompletions(System.String[] completions) public Option AddCompletions(System.Func> completionsDelegate) public Option AddCompletions(System.Func> completionsDelegate) + public Option AddValidator(System.Action validate) public static class OptionValidation public static Option AcceptExistingOnly(this Option option) public static Option AcceptExistingOnly(this Option option) diff --git a/src/System.CommandLine.Tests/ArgumentTests.cs b/src/System.CommandLine.Tests/ArgumentTests.cs index 4dd5626246..d9d8b88ab4 100644 --- a/src/System.CommandLine.Tests/ArgumentTests.cs +++ b/src/System.CommandLine.Tests/ArgumentTests.cs @@ -792,6 +792,7 @@ public void Argument_of_T_fluent_APIs_return_Argument_of_T() .AddCompletions("test") .AddCompletions(ctx => Array.Empty()) .AddCompletions(ctx => Array.Empty()) + .AddValidator(_ => { }) .AcceptLegalFileNamesOnly() .AcceptLegalFilePathsOnly(); diff --git a/src/System.CommandLine.Tests/OptionTests.cs b/src/System.CommandLine.Tests/OptionTests.cs index 4871e84828..bcc8bd5990 100644 --- a/src/System.CommandLine.Tests/OptionTests.cs +++ b/src/System.CommandLine.Tests/OptionTests.cs @@ -394,6 +394,7 @@ public void Option_of_T_fluent_APIs_return_Option_of_T() .AddCompletions("test") .AddCompletions(ctx => Array.Empty()) .AddCompletions(ctx => Array.Empty()) + .AddValidator(_ => { }) .AcceptLegalFileNamesOnly() .AcceptLegalFilePathsOnly(); diff --git a/src/System.CommandLine/Argument.cs b/src/System.CommandLine/Argument.cs index 59de758e9b..6a91ced17f 100644 --- a/src/System.CommandLine/Argument.cs +++ b/src/System.CommandLine/Argument.cs @@ -106,13 +106,6 @@ private protected override string DefaultName internal List> Validators => _validators ??= new (); - /// - /// Adds a custom validator to the argument. Validators can be used - /// to provide custom errors based on user input. - /// - /// The action to validate the parsed argument. - public void AddValidator(Action validate) => Validators.Add(validate); - /// /// Gets the default value for the argument. /// diff --git a/src/System.CommandLine/Argument{T}.cs b/src/System.CommandLine/Argument{T}.cs index 015cb407d8..a17b18b414 100644 --- a/src/System.CommandLine/Argument{T}.cs +++ b/src/System.CommandLine/Argument{T}.cs @@ -153,6 +153,17 @@ public Argument AddCompletions(Func + /// Adds a custom validator to the argument. Validators can be used + /// to provide custom errors based on user input. + /// + /// The action to validate the parsed argument. + public Argument AddValidator(Action validate) + { + Validators.Add(validate); + return this; + } + /// /// Configures the argument to accept only the specified values, and to suggest them as command line completions. /// diff --git a/src/System.CommandLine/Option.cs b/src/System.CommandLine/Option.cs index 48a984d563..3a4447fb89 100644 --- a/src/System.CommandLine/Option.cs +++ b/src/System.CommandLine/Option.cs @@ -101,12 +101,6 @@ public override string Name internal bool HasValidators => _validators is not null && _validators.Count > 0; - /// - /// Adds a validator that will be called when the option is matched by the parser. - /// - /// An action used to validate the produced during parsing. - public void AddValidator(Action validate) => Validators.Add(validate); - /// /// Indicates whether a given alias exists on the option, regardless of its prefix. /// diff --git a/src/System.CommandLine/OptionValidation.cs b/src/System.CommandLine/OptionValidation.cs index 4637028c08..c0305ec9fa 100644 --- a/src/System.CommandLine/OptionValidation.cs +++ b/src/System.CommandLine/OptionValidation.cs @@ -18,7 +18,7 @@ public static class OptionValidation /// The option being extended. public static Option AcceptExistingOnly(this Option option) { - option.Argument.AddValidator(Validate.FileExists); + option.Argument.Validators.Add(Validate.FileExists); return option; } @@ -29,7 +29,7 @@ public static Option AcceptExistingOnly(this Option option) /// The option being extended. public static Option AcceptExistingOnly(this Option option) { - option.Argument.AddValidator(Validate.DirectoryExists); + option.Argument.Validators.Add(Validate.DirectoryExists); return option; } @@ -40,7 +40,7 @@ public static Option AcceptExistingOnly(this OptionThe option being extended. public static Option AcceptExistingOnly(this Option option) { - option.Argument.AddValidator(Validate.FileOrDirectoryExists); + option.Argument.Validators.Add(Validate.FileOrDirectoryExists); return option; } diff --git a/src/System.CommandLine/Option{T}.cs b/src/System.CommandLine/Option{T}.cs index c0750c56d1..153afa3105 100644 --- a/src/System.CommandLine/Option{T}.cs +++ b/src/System.CommandLine/Option{T}.cs @@ -132,6 +132,16 @@ public Option AddCompletions(Func + /// Adds a validator that will be called when the option is matched by the parser. + /// + /// An action used to validate the produced during parsing. + public Option AddValidator(Action validate) + { + Validators.Add(validate); + return this; + } + /// /// Configures the option to accept only values representing legal file paths. ///