Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
add a missing failing test and a fix for it
  • Loading branch information
adamsitnik committed Dec 15, 2022
commit 1cb1e9af19d6e38f93293439b6ef7e5e98ccfb7c
28 changes: 28 additions & 0 deletions src/System.CommandLine.Tests/ParsingValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ public void When_FromAmong_is_used_for_multiple_arguments_and_invalid_input_is_p
.Be(LocalizationResources.Instance.UnrecognizedArgument("not-key1", new[] { "key1", "key2" }));
}

[Fact]
public void When_FromAmong_is_used_multiple_times_only_the_most_recently_provided_values_are_taken_into_account()
{
Argument<string> argument = new("key");
argument.AcceptOnlyFromAmong("key1");

var command = new Command("set")
{
argument
};

var result = command.Parse("set key2");

result.Errors
.Should()
.ContainSingle()
.Which
.Message
.Should()
.Be(LocalizationResources.Instance.UnrecognizedArgument("key2", new[] { "key1" }));

argument.AcceptOnlyFromAmong("key2");

result = command.Parse("set key2");

result.Errors.Should().BeEmpty();
}

[Fact]
public void When_FromAmong_is_used_for_multiple_arguments_and_invalid_input_is_provided_for_the_second_one_then_the_error_is_informative()
{
Expand Down
3 changes: 1 addition & 2 deletions src/System.CommandLine/Argument{T}.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.CommandLine.Binding;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;
using System.IO;

Expand Down Expand Up @@ -181,6 +179,7 @@ public Argument<T> AcceptOnlyFromAmong(params string[] values)
{
if (values is not null && values.Length > 0)
{
Validators.Clear();
Validators.Add(UnrecognizedArgumentError);
CompletionSources.Clear();
CompletionSources.Add(values);
Expand Down