Skip to content

Commit 92bd70e

Browse files
UnrecognizedArgument error should mention first invalid argument, not the last one (#2029)
Co-authored-by: Jon Sequeira <[email protected]>
1 parent 17ba875 commit 92bd70e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/System.CommandLine.Tests/ParsingValidationTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ public void When_FromAmong_is_used_for_multiple_arguments_and_invalid_input_is_p
170170
.Be(LocalizationResources.Instance.UnrecognizedArgument("not-value1", new[] { "value1", "value2" }));
171171
}
172172

173+
[Fact]
174+
public void When_FromAmong_is_used_and_multiple_invalid_inputs_are_provided_the_error_mentions_first_invalid_argument()
175+
{
176+
Option<string[]> option = new(new[] { "--columns" });
177+
option.AcceptOnlyFromAmong("author", "language", "tags", "type");
178+
option.Arity = new ArgumentArity(1, 4);
179+
option.AllowMultipleArgumentsPerToken = true;
180+
181+
var command = new Command("list")
182+
{
183+
option
184+
};
185+
186+
var result = command.Parse("list --columns c1 c2");
187+
188+
// Currently there is no possibility for a single validator to produce multiple errors,
189+
// so only the first one is checked.
190+
result.Errors[0]
191+
.Message
192+
.Should()
193+
.Be(LocalizationResources.Instance.UnrecognizedArgument("c1", new[] { "author", "language", "tags", "type" }));
194+
}
195+
173196
[Fact]
174197
public void When_a_required_argument_is_not_supplied_then_an_error_is_returned()
175198
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ void UnrecognizedArgumentError(ArgumentResult argumentResult)
195195
if (Array.IndexOf(values, token.Value) < 0)
196196
{
197197
argumentResult.ErrorMessage = argumentResult.LocalizationResources.UnrecognizedArgument(token.Value, values);
198+
break;
198199
}
199200
}
200201
}

0 commit comments

Comments
 (0)