-
Notifications
You must be signed in to change notification settings - Fork 401
Remove Argument.AllowedValues, use Validator to implement the logic #1959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -83,7 +83,7 @@ public void OnlyTake(int numberOfTokens) | |||
|
|||
if (!string.IsNullOrWhiteSpace(ErrorMessage)) | |||
{ | |||
return new ParseError(ErrorMessage!, this); | |||
return new ParseError(ErrorMessage!, Parent is OptionResult option ? option : this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not obvious. Before this change, the SymbolResult
of ParseError
for an Argument with unmatched tokens owned by an Option was the OptionResult
, not ArgumentResult
:
argumentResult.Parent?.UnrecognizedArgumentError(argument) ?? |
but this was true only "non-custom errors" (user defined, coming from validators):
argumentResult.CustomError(argument); |
Without this change, one test was failing:
Failed System.CommandLine.Tests.ParseDiagramTests.Parse_result_diagram_displays_unmatched_tokens [283 ms]
Error Message:
Expected string to be
"[ command ![ -x <ar> ] ]", but
"[ command [ -x !<ar> ] ]" differs near "[ -" (index 10).
Since we hide the Option.Argument
from the user, I think it's reasonable to use the OptionResult
in such cases for all validations.
AcceptOnlyFromAmong is case-sensitive, unlike the Enum.Parse call in ArgumentConverter
|
If there's no test for case-insensitive enum parsing, we should definitely add one. |
src/System.CommandLine/Argument.cs
Outdated
|
||
/// <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); |
There was a problem hiding this comment.
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); |
src/System.CommandLine/Argument.cs
Outdated
|
||
/// <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); |
There was a problem hiding this comment.
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>
.
# Conflicts: # src/System.CommandLine/Argument.cs # src/System.CommandLine/Option.cs
I've addressed all the feedback, I am going to merge this PR now to unblock other PRs.
Please create an issue for that. We can just extend |
Already created #1960 a month ago.
Users can easily implement |
follow up to #1891 (comment)