Skip to content

Adding proposed fix for FromAmong #1843

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

Merged
merged 1 commit into from
Sep 6, 2022
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
18 changes: 18 additions & 0 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,24 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
}
}

[Fact]
public void Argument_of_enum_can_limit_enum_members_as_valid_values()
{
var argument = new Argument<ConsoleColor>()
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
Command command = new("set-color")
{
argument
};

var result = command.Parse("set-color Fuschia");

result.Errors
.Select(e => e.Message)
.Should()
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
}

protected override Symbol CreateSymbol(string name)
{
return new Argument<string>(name);
Expand Down
16 changes: 15 additions & 1 deletion src/System.CommandLine.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// 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 FluentAssertions;
using System.CommandLine.Parsing;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace System.CommandLine.Tests
Expand Down Expand Up @@ -370,6 +370,20 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
.Should()
.BeFalse();
}

[Fact]
public void Option_of_enum_can_limit_enum_members_as_valid_values()
{
var option = new Option<ConsoleColor>("--color")
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());

var result = option.Parse("--color Fuschia");

result.Errors
.Select(e => e.Message)
.Should()
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
}

protected override Symbol CreateSymbol(string name) => new Option<string>(name);
}
Expand Down
2 changes: 2 additions & 0 deletions src/System.CommandLine/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public static TArgument FromAmong<TArgument>(
params string[] values)
where TArgument : Argument
{
argument.AllowedValues?.Clear();
argument.AddAllowedValues(values);
argument.Completions.Clear();
argument.Completions.Add(values);

return argument;
Expand Down
2 changes: 2 additions & 0 deletions src/System.CommandLine/OptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public static TOption FromAmong<TOption>(
params string[] values)
where TOption : Option
{
option.Argument.AllowedValues?.Clear();
option.Argument.AddAllowedValues(values);
option.Argument.Completions.Clear();
option.Argument.Completions.Add(values);

return option;
Expand Down