Skip to content

Commit 9266d5c

Browse files
authored
make AddValidator fluent, fixes #1965 (#1966)
1 parent 8374d5f commit 9266d5c

File tree

8 files changed

+28
-18
lines changed

8 files changed

+28
-18
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ System.CommandLine
55
public System.Boolean HasDefaultValue { get; }
66
public System.String HelpName { get; set; }
77
public System.Type ValueType { get; }
8-
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
98
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
109
public System.Object GetDefaultValue()
1110
public ParseResult Parse(System.String commandLine)
@@ -28,6 +27,7 @@ System.CommandLine
2827
public Argument<T> AddCompletions(System.String[] completions)
2928
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
3029
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
30+
public Argument<T> AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
3131
public struct ArgumentArity : System.ValueType, System.IEquatable<ArgumentArity>
3232
public static ArgumentArity ExactlyOne { get; }
3333
public static ArgumentArity OneOrMore { get; }
@@ -193,7 +193,6 @@ System.CommandLine
193193
public ArgumentArity Arity { get; set; }
194194
public System.Boolean IsRequired { get; set; }
195195
public System.Type ValueType { get; }
196-
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
197196
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
198197
public System.Boolean HasAliasIgnoringPrefix(System.String alias)
199198
public ParseResult Parse(System.String commandLine)
@@ -213,6 +212,7 @@ System.CommandLine
213212
public Option<T> AddCompletions(System.String[] completions)
214213
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
215214
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
215+
public Option<T> AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
216216
public static class OptionValidation
217217
public static Option<System.IO.FileInfo> AcceptExistingOnly(this Option<System.IO.FileInfo> option)
218218
public static Option<System.IO.DirectoryInfo> AcceptExistingOnly(this Option<System.IO.DirectoryInfo> option)

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ public void Argument_of_T_fluent_APIs_return_Argument_of_T()
792792
.AddCompletions("test")
793793
.AddCompletions(ctx => Array.Empty<string>())
794794
.AddCompletions(ctx => Array.Empty<CompletionItem>())
795+
.AddValidator(_ => { })
795796
.AcceptLegalFileNamesOnly()
796797
.AcceptLegalFilePathsOnly();
797798

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public void Option_of_T_fluent_APIs_return_Option_of_T()
394394
.AddCompletions("test")
395395
.AddCompletions(ctx => Array.Empty<string>())
396396
.AddCompletions(ctx => Array.Empty<CompletionItem>())
397+
.AddValidator(_ => { })
397398
.AcceptLegalFileNamesOnly()
398399
.AcceptLegalFilePathsOnly();
399400

src/System.CommandLine/Argument.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ private protected override string DefaultName
106106

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

109-
/// <summary>
110-
/// Adds a custom validator to the argument. Validators can be used
111-
/// to provide custom errors based on user input.
112-
/// </summary>
113-
/// <param name="validate">The action to validate the parsed argument.</param>
114-
public void AddValidator(Action<ArgumentResult> validate) => Validators.Add(validate);
115-
116109
/// <summary>
117110
/// Gets the default value for the argument.
118111
/// </summary>

src/System.CommandLine/Argument{T}.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ public Argument<T> AddCompletions(Func<CompletionContext, IEnumerable<Completion
153153
return this;
154154
}
155155

156+
/// <summary>
157+
/// Adds a custom validator to the argument. Validators can be used
158+
/// to provide custom errors based on user input.
159+
/// </summary>
160+
/// <param name="validate">The action to validate the parsed argument.</param>
161+
public Argument<T> AddValidator(Action<ArgumentResult> validate)
162+
{
163+
Validators.Add(validate);
164+
return this;
165+
}
166+
156167
/// <summary>
157168
/// Configures the argument to accept only the specified values, and to suggest them as command line completions.
158169
/// </summary>

src/System.CommandLine/Option.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@ public override string Name
101101

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

104-
/// <summary>
105-
/// Adds a validator that will be called when the option is matched by the parser.
106-
/// </summary>
107-
/// <param name="validate">An action used to validate the <see cref="OptionResult"/> produced during parsing.</param>
108-
public void AddValidator(Action<OptionResult> validate) => Validators.Add(validate);
109-
110104
/// <summary>
111105
/// Indicates whether a given alias exists on the option, regardless of its prefix.
112106
/// </summary>

src/System.CommandLine/OptionValidation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class OptionValidation
1818
/// <returns>The option being extended.</returns>
1919
public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
2020
{
21-
option.Argument.AddValidator(Validate.FileExists);
21+
option.Argument.Validators.Add(Validate.FileExists);
2222
return option;
2323
}
2424

@@ -29,7 +29,7 @@ public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
2929
/// <returns>The option being extended.</returns>
3030
public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo> option)
3131
{
32-
option.Argument.AddValidator(Validate.DirectoryExists);
32+
option.Argument.Validators.Add(Validate.DirectoryExists);
3333
return option;
3434
}
3535

@@ -40,7 +40,7 @@ public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo
4040
/// <returns>The option being extended.</returns>
4141
public static Option<FileSystemInfo> AcceptExistingOnly(this Option<FileSystemInfo> option)
4242
{
43-
option.Argument.AddValidator(Validate.FileOrDirectoryExists);
43+
option.Argument.Validators.Add(Validate.FileOrDirectoryExists);
4444
return option;
4545
}
4646

src/System.CommandLine/Option{T}.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ public Option<T> AddCompletions(Func<CompletionContext, IEnumerable<CompletionIt
132132
return this;
133133
}
134134

135+
/// <summary>
136+
/// Adds a validator that will be called when the option is matched by the parser.
137+
/// </summary>
138+
/// <param name="validate">An action used to validate the <see cref="OptionResult"/> produced during parsing.</param>
139+
public Option<T> AddValidator(Action<OptionResult> validate)
140+
{
141+
Validators.Add(validate);
142+
return this;
143+
}
144+
135145
/// <summary>
136146
/// Configures the option to accept only values representing legal file paths.
137147
/// </summary>

0 commit comments

Comments
 (0)