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
Next Next commit
Argument.AcceptOnlyFromAmong should return void
  • Loading branch information
adamsitnik committed Dec 15, 2022
commit 29b72d0ea09fa2823919e0bde5ffdf733fece062
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ System.CommandLine
public System.Type ValueType { get; }
public Argument<T> AcceptLegalFileNamesOnly()
public Argument<T> AcceptLegalFilePathsOnly()
public Argument<T> AcceptOnlyFromAmong(System.String[] values)
public System.Void AcceptOnlyFromAmong(System.String[] values)
public System.Void SetDefaultValue(T value)
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
public System.Void SetDefaultValueFactory(Func<System.CommandLine.Parsing.ArgumentResult,T> defaultValueFactory)
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,9 @@ 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>()
.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
var argument = new Argument<ConsoleColor>();
argument.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());

Command command = new("set-color")
{
argument
Expand All @@ -788,7 +789,6 @@ public void Argument_of_enum_can_limit_enum_members_as_valid_values()
public void Argument_of_T_fluent_APIs_return_Argument_of_T()
{
Argument<string> argument = new Argument<string>("--path")
.AcceptOnlyFromAmong("text")
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

Expand Down
19 changes: 13 additions & 6 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,15 @@ public void When_parsing_from_text_then_argument_completions_are_based_on_the_pr
{
new Command("one")
{
new Argument<string>().AcceptOnlyFromAmong("one-a", "one-b", "one-c")
CreateArgumentWithAcceptOnlyFromAmong("one-a", "one-b", "one-c")
},
new Command("two")
{
new Argument<string>().AcceptOnlyFromAmong("two-a", "two-b", "two-c")
CreateArgumentWithAcceptOnlyFromAmong("two-a", "two-b", "two-c")
},
new Command("three")
{
new Argument<string>().AcceptOnlyFromAmong("three-a", "three-b", "three-c")
CreateArgumentWithAcceptOnlyFromAmong("three-a", "three-b", "three-c")
}
};

Expand All @@ -725,15 +725,15 @@ public void When_parsing_from_array_then_argument_completions_are_based_on_the_p
{
new Command("one")
{
new Argument<string>().AcceptOnlyFromAmong("one-a", "one-b", "one-c")
CreateArgumentWithAcceptOnlyFromAmong("one-a", "one-b", "one-c")
},
new Command("two")
{
new Argument<string>().AcceptOnlyFromAmong("two-a", "two-b", "two-c")
CreateArgumentWithAcceptOnlyFromAmong("two-a", "two-b", "two-c")
},
new Command("three")
{
new Argument<string>().AcceptOnlyFromAmong("three-a", "three-b", "three-c")
CreateArgumentWithAcceptOnlyFromAmong("three-a", "three-b", "three-c")
}
};

Expand Down Expand Up @@ -968,5 +968,12 @@ public void When_option_completions_are_available_then_they_are_suggested_when_a
.Be(
$"Cannot parse argument 'SleepyDay' for option '--day' as expected type 'System.DayOfWeek'. Did you mean one of the following?{NewLine}Friday{NewLine}Monday{NewLine}Saturday{NewLine}Sunday{NewLine}Thursday{NewLine}Tuesday{NewLine}Wednesday");
}

private Argument<string> CreateArgumentWithAcceptOnlyFromAmong(params string[] values)
{
Argument<string> argument = new();
argument.AcceptOnlyFromAmong(values);
return argument;
}
}
}
27 changes: 18 additions & 9 deletions src/System.CommandLine.Tests/ParsingValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ public void When_FromAmong_is_used_then_the_OptionResult_ErrorMessage_is_set()
[Fact] // https://github.com/dotnet/command-line-api/issues/1475
public void When_FromAmong_is_used_then_the_ArgumentResult_ErrorMessage_is_set()
{
var option = new Argument<string>().AcceptOnlyFromAmong("a", "b");
var command = new Command("test") { option };
var argument = new Argument<string>();
argument.AcceptOnlyFromAmong("a", "b");

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

var parseResult = command.Parse("test c");

parseResult.FindResultFor(option)
parseResult.FindResultFor(argument)
.ErrorMessage
.Should()
.Be(parseResult.Errors.Single().Message)
Expand All @@ -90,8 +92,8 @@ public void When_FromAmong_is_used_for_multiple_arguments_and_valid_input_is_pro
{
var command = new Command("set")
{
new Argument<string>("key").AcceptOnlyFromAmong("key1", "key2"),
new Argument<string>("value").AcceptOnlyFromAmong("value1", "value2")
CreateArgumentWithAcceptOnlyFromAmong(name: "key", "key1", "key2"),
CreateArgumentWithAcceptOnlyFromAmong(name : "value", "value1", "value2")
};

var result = command.Parse("set key1 value1");
Expand All @@ -104,8 +106,8 @@ public void When_FromAmong_is_used_for_multiple_arguments_and_invalid_input_is_p
{
var command = new Command("set")
{
new Argument<string>("key").AcceptOnlyFromAmong("key1", "key2"),
new Argument<string>("value").AcceptOnlyFromAmong("value1", "value2")
CreateArgumentWithAcceptOnlyFromAmong(name : "key", "key1", "key2"),
CreateArgumentWithAcceptOnlyFromAmong(name : "value", "value1", "value2")
};

var result = command.Parse("set not-key1 value1");
Expand Down Expand Up @@ -152,8 +154,8 @@ public void When_FromAmong_is_used_for_multiple_arguments_and_invalid_input_is_p
{
var command = new Command("set")
{
new Argument<string>("key").AcceptOnlyFromAmong("key1", "key2"),
new Argument<string>("value").AcceptOnlyFromAmong("value1", "value2")
CreateArgumentWithAcceptOnlyFromAmong(name : "key", "key1", "key2"),
CreateArgumentWithAcceptOnlyFromAmong(name : "value", "value1", "value2")
};

var result = command.Parse("set key1 not-value1");
Expand Down Expand Up @@ -1233,5 +1235,12 @@ internal void When_there_is_an_arity_error_then_further_errors_are_not_reported(
.Should()
.Be("Required argument missing for option: '-o'.");
}

private Argument<string> CreateArgumentWithAcceptOnlyFromAmong(string name, params string[] values)
{
Argument<string> argument = new(name);
argument.AcceptOnlyFromAmong(values);
return argument;
}
}
}
5 changes: 1 addition & 4 deletions src/System.CommandLine/Argument{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ public void SetDefaultValueFactory(Func<ArgumentResult, T> defaultValueFactory)
/// Configures the argument to accept only the specified values, and to suggest them as command line completions.
/// </summary>
/// <param name="values">The values that are allowed for the argument.</param>
/// <returns>The configured argument.</returns>
public Argument<T> AcceptOnlyFromAmong(params string[] values)
public void AcceptOnlyFromAmong(params string[] values)
{
if (values is not null && values.Length > 0)
{
Expand All @@ -185,8 +184,6 @@ public Argument<T> AcceptOnlyFromAmong(params string[] values)
CompletionSources.Add(values);
}

return this;

void UnrecognizedArgumentError(ArgumentResult argumentResult)
{
for (var i = 0; i < argumentResult.Tokens.Count; i++)
Expand Down