Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 ParseResult Parse(System.String commandLine)
Expand All @@ -28,6 +27,7 @@ System.CommandLine
public Argument<T> AddCompletions(System.String[] completions)
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
public Argument<T> AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
public struct ArgumentArity : System.ValueType, System.IEquatable<ArgumentArity>
public static ArgumentArity ExactlyOne { get; }
public static ArgumentArity OneOrMore { get; }
Expand Down Expand Up @@ -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<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 ParseResult Parse(System.String commandLine)
Expand All @@ -213,6 +212,7 @@ System.CommandLine
public Option<T> AddCompletions(System.String[] completions)
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
public Option<T> AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
public static class OptionValidation
public static Option<System.IO.FileInfo> AcceptExistingOnly(this Option<System.IO.FileInfo> option)
public static Option<System.IO.DirectoryInfo> AcceptExistingOnly(this Option<System.IO.DirectoryInfo> option)
Expand Down
1 change: 1 addition & 0 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ public void Argument_of_T_fluent_APIs_return_Argument_of_T()
.AddCompletions("test")
.AddCompletions(ctx => Array.Empty<string>())
.AddCompletions(ctx => Array.Empty<CompletionItem>())
.AddValidator(_ => { })
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

Expand Down
1 change: 1 addition & 0 deletions src/System.CommandLine.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public void Option_of_T_fluent_APIs_return_Option_of_T()
.AddCompletions("test")
.AddCompletions(ctx => Array.Empty<string>())
.AddCompletions(ctx => Array.Empty<CompletionItem>())
.AddValidator(_ => { })
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

Expand Down
7 changes: 0 additions & 7 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ private protected override string DefaultName

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

/// <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);

/// <summary>
/// Gets the default value for the argument.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/System.CommandLine/Argument{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ public Argument<T> AddCompletions(Func<CompletionContext, IEnumerable<Completion
return this;
}

/// <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 Argument<T> AddValidator(Action<ArgumentResult> validate)
{
Validators.Add(validate);
return this;
}

/// <summary>
/// Configures the argument to accept only the specified values, and to suggest them as command line completions.
/// </summary>
Expand Down
6 changes: 0 additions & 6 deletions src/System.CommandLine/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ public override string Name

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">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.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine/OptionValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class OptionValidation
/// <returns>The option being extended.</returns>
public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
{
option.Argument.AddValidator(Validate.FileExists);
option.Argument.Validators.Add(Validate.FileExists);
return option;
}

Expand All @@ -29,7 +29,7 @@ public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
/// <returns>The option being extended.</returns>
public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo> option)
{
option.Argument.AddValidator(Validate.DirectoryExists);
option.Argument.Validators.Add(Validate.DirectoryExists);
return option;
}

Expand All @@ -40,7 +40,7 @@ public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo
/// <returns>The option being extended.</returns>
public static Option<FileSystemInfo> AcceptExistingOnly(this Option<FileSystemInfo> option)
{
option.Argument.AddValidator(Validate.FileOrDirectoryExists);
option.Argument.Validators.Add(Validate.FileOrDirectoryExists);
return option;
}

Expand Down
10 changes: 10 additions & 0 deletions src/System.CommandLine/Option{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public Option<T> AddCompletions(Func<CompletionContext, IEnumerable<CompletionIt
return this;
}

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

/// <summary>
/// Configures the option to accept only values representing legal file paths.
/// </summary>
Expand Down