Skip to content

Commit 1258719

Browse files
authored
Validation should not stop on the first reported error (#2170)
* add failing test that reproduces the SDK scenario * don't exit the loop that validates all options when one option has errors, just continue and validate next option
1 parent c75eab4 commit 1258719

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,38 @@ public void Custom_parser_can_check_another_option_result_for_custom_errors(stri
488488
.Be("one two");
489489
}
490490

491+
[Fact]
492+
public void Validation_reports_all_parse_errors()
493+
{
494+
CliOption<string> firstOptionWithError = new("--first-option-with-error");
495+
firstOptionWithError.Validators.Add(optionResult => optionResult.AddError("first error"));
496+
CliOption<string> secondOptionWithError = new("--second-option-with-error")
497+
{
498+
CustomParser = r =>
499+
{
500+
r.AddError("second error");
501+
return r.Tokens[0].Value;
502+
}
503+
};
504+
505+
CliCommand command = new ("cmd")
506+
{
507+
firstOptionWithError,
508+
secondOptionWithError
509+
};
510+
511+
ParseResult parseResult = command.Parse("cmd --first-option-with-error value1 --second-option-with-error value2");
512+
513+
OptionResult firstOptionResult = parseResult.GetResult(firstOptionWithError);
514+
firstOptionResult.Errors.Single().Message.Should().Be("first error");
515+
516+
OptionResult secondOptionResult = parseResult.GetResult(secondOptionWithError);
517+
secondOptionResult.Errors.Single().Message.Should().Be("second error");
518+
519+
parseResult.Errors.Should().Contain(error => error.SymbolResult == firstOptionResult);
520+
parseResult.Errors.Should().Contain(error => error.SymbolResult == secondOptionResult);
521+
}
522+
491523
[Fact]
492524
public void When_custom_conversion_fails_then_an_option_does_not_accept_further_arguments()
493525
{

src/System.CommandLine/Parsing/CommandResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void ValidateOptions(bool completeValidation)
140140

141141
if (errorsBefore != SymbolResultTree.ErrorCount)
142142
{
143-
break;
143+
continue;
144144
}
145145
}
146146

0 commit comments

Comments
 (0)