Skip to content

Fluent APIs are not allowed in non-builder types #1994

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 8 commits into from
Dec 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ System.CommandLine
.ctor(Func<System.CommandLine.Parsing.ArgumentResult,T> parse, System.Boolean isDefault = False)
public System.Boolean HasDefaultValue { get; }
public System.Type ValueType { get; }
public Argument<T> AcceptLegalFileNamesOnly()
public Argument<T> AcceptLegalFilePathsOnly()
public Argument<T> AcceptOnlyFromAmong(System.String[] values)
public System.Void AcceptLegalFileNamesOnly()
public System.Void AcceptLegalFilePathsOnly()
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 Expand Up @@ -198,9 +198,9 @@ System.CommandLine
.ctor(System.String[] aliases, Func<System.CommandLine.Parsing.ArgumentResult,T> parseArgument, System.Boolean isDefault = False, System.String description = null)
.ctor(System.String name, Func<T> defaultValueFactory, System.String description = null)
.ctor(System.String[] aliases, Func<T> defaultValueFactory, System.String description = null)
public Option<T> AcceptLegalFileNamesOnly()
public Option<T> AcceptLegalFilePathsOnly()
public Option<T> AcceptOnlyFromAmong(System.String[] values)
public System.Void AcceptLegalFileNamesOnly()
public System.Void AcceptLegalFilePathsOnly()
public System.Void AcceptOnlyFromAmong(System.String[] values)
public System.Void SetDefaultValue(T value)
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
public static class OptionValidation
Expand Down
16 changes: 3 additions & 13 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 @@ -784,17 +785,6 @@ public void Argument_of_enum_can_limit_enum_members_as_valid_values()
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
}

[Fact]
public void Argument_of_T_fluent_APIs_return_Argument_of_T()
{
Argument<string> argument = new Argument<string>("--path")
.AcceptOnlyFromAmong("text")
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

argument.Should().BeOfType<Argument<string>>();
}

protected override Symbol CreateSymbol(string name)
{
return new Argument<string>(name);
Expand Down
10 changes: 8 additions & 2 deletions src/System.CommandLine.Tests/CompletionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ public void When_position_is_unspecified_in_string_command_line_ending_with_a_sp
[Fact]
public void When_position_is_greater_than_input_length_in_a_string_command_line_then_it_returns_empty()
{
Option<string> option1 = new ("--option1");
option1.AcceptOnlyFromAmong("apple", "banana", "cherry", "durian");

var command = new Command("the-command")
{
new Argument<string>(),
new Option<string>("--option1").AcceptOnlyFromAmong("apple", "banana", "cherry", "durian"),
option1,
new Option<string>("--option2")
};

Expand Down Expand Up @@ -176,9 +179,12 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
[Fact]
public void When_position_is_unspecified_in_array_command_line_and_final_token_matches_an_argument_then_it_returns_the_argument_value()
{
Option<string> option1 = new("--option1");
option1.AcceptOnlyFromAmong("apple", "banana", "cherry", "durian");

var command = new Command("the-command")
{
new Option<string>("--option1").AcceptOnlyFromAmong("apple", "banana", "cherry", "durian"),
option1,
new Option<string>("--option2"),
new Argument<string>()
};
Expand Down
60 changes: 34 additions & 26 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ public void Parser_options_can_supply_context_sensitive_matches()
{
var parser = new RootCommand
{
new Option<string>("--bread").AcceptOnlyFromAmong("wheat", "sourdough", "rye"),
new Option<string>("--cheese").AcceptOnlyFromAmong("provolone", "cheddar", "cream cheese")
CreateOptionWithAcceptOnlyFromAmong(name: "--bread", "wheat", "sourdough", "rye"),
CreateOptionWithAcceptOnlyFromAmong(name: "--cheese", "provolone", "cheddar", "cream cheese")
};

var commandLine = "--bread";
Expand Down Expand Up @@ -546,8 +546,8 @@ public void Argument_completions_can_be_based_on_the_proximate_option()
var parser = new Parser(
new Command("outer")
{
new Option<string>("--one").AcceptOnlyFromAmong("one-a", "one-b"),
new Option<string>("--two").AcceptOnlyFromAmong("two-a", "two-b")
CreateOptionWithAcceptOnlyFromAmong(name: "--one", "one-a", "one-b"),
CreateOptionWithAcceptOnlyFromAmong(name: "--two", "two-a", "two-b")
});

var commandLine = "outer --two";
Expand Down Expand Up @@ -648,12 +648,9 @@ public void When_caller_does_the_tokenizing_then_argument_completions_are_based_
{
var command = new Command("outer")
{
new Option<string>("one")
.AcceptOnlyFromAmong("one-a", "one-b", "one-c"),
new Option<string>("two")
.AcceptOnlyFromAmong("two-a", "two-b", "two-c"),
new Option<string>("three")
.AcceptOnlyFromAmong("three-a", "three-b", "three-c")
CreateOptionWithAcceptOnlyFromAmong(name: "one", "one-a", "one-b", "one-c"),
CreateOptionWithAcceptOnlyFromAmong(name: "two", "two-a", "two-b", "two-c"),
CreateOptionWithAcceptOnlyFromAmong(name: "three", "three-a", "three-b", "three-c")
};

var parser = new CommandLineBuilder(new RootCommand
Expand All @@ -675,12 +672,9 @@ public void When_parsing_from_array_then_argument_completions_are_based_on_the_p
{
var command = new Command("outer")
{
new Option<string>("one")
.AcceptOnlyFromAmong("one-a", "one-b", "one-c"),
new Option<string>("two")
.AcceptOnlyFromAmong("two-a", "two-b", "two-c"),
new Option<string>("three")
.AcceptOnlyFromAmong("three-a", "three-b", "three-c")
CreateOptionWithAcceptOnlyFromAmong(name: "one", "one-a", "one-b", "one-c"),
CreateOptionWithAcceptOnlyFromAmong(name: "two", "two-a", "two-b", "two-c"),
CreateOptionWithAcceptOnlyFromAmong(name: "three", "three-a", "three-b", "three-c")
};

var result = command.Parse("outer two b");
Expand All @@ -698,15 +692,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 +719,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 All @@ -750,8 +744,8 @@ public void When_parsing_from_text_if_the_proximate_option_is_completed_then_com
{
var command = new RootCommand
{
new Option<string>("--framework").AcceptOnlyFromAmong("net7.0"),
new Option<string>("--language").AcceptOnlyFromAmong("C#"),
CreateOptionWithAcceptOnlyFromAmong(name: "--framework", "net7.0"),
CreateOptionWithAcceptOnlyFromAmong(name: "--language", "C#"),
new Option<string>("--langVersion")
};
var parser = new CommandLineBuilder(command).Build();
Expand All @@ -767,8 +761,8 @@ public void When_parsing_from_array_if_the_proximate_option_is_completed_then_co
{
var command = new RootCommand
{
new Option<string>("--framework").AcceptOnlyFromAmong("net7.0"),
new Option<string>("--language").AcceptOnlyFromAmong("C#"),
CreateOptionWithAcceptOnlyFromAmong(name: "--framework", "net7.0"),
CreateOptionWithAcceptOnlyFromAmong(name: "--language", "C#"),
new Option<string>("--langVersion")
};
var parser = new CommandLineBuilder(command).Build();
Expand Down Expand Up @@ -968,5 +962,19 @@ 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 static Argument<string> CreateArgumentWithAcceptOnlyFromAmong(params string[] values)
{
Argument<string> argument = new();
argument.AcceptOnlyFromAmong(values);
return argument;
}

private static Option<string> CreateOptionWithAcceptOnlyFromAmong(string name, params string[] values)
{
Option<string> option = new(name);
option.AcceptOnlyFromAmong(values);
return option;
}
}
}
15 changes: 2 additions & 13 deletions src/System.CommandLine.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
[Fact]
public void Option_of_enum_can_limit_enum_members_as_valid_values()
{
var option = new Option<ConsoleColor>("--color")
.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
Option<ConsoleColor> option = new("--color");
option.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());

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

Expand All @@ -368,17 +368,6 @@ public void Option_of_enum_can_limit_enum_members_as_valid_values()
.Should()
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
}

[Fact]
public void Option_of_T_fluent_APIs_return_Option_of_T()
{
Option<string> option = new Option<string>("--path")
.AcceptOnlyFromAmong("text")
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

option.Should().BeOfType<Option<string>>();
}

protected override Symbol CreateSymbol(string name) => new Option<string>(name);
}
Expand Down
5 changes: 4 additions & 1 deletion src/System.CommandLine.Tests/ParseDiagramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public void Parse_result_diagram_helps_explain_parse_operation()
[Fact]
public void Parse_result_diagram_displays_unmatched_tokens()
{
Option<string> option = new ("-x");
option.AcceptOnlyFromAmong("arg1", "arg2", "arg3");

var command = new Command("command")
{
new Option<string>("-x").AcceptOnlyFromAmong("arg1", "arg2", "arg3")
option
};

var result = command.Parse("command -x ar");
Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ public void Parser_root_Options_can_be_specified_multiple_times_and_their_argume
[Fact]
public void Options_can_be_specified_multiple_times_and_their_arguments_are_collated()
{
var animalsOption = new Option<string[]>(new[] { "-a", "--animals" })
.AcceptOnlyFromAmong("dog", "cat", "sheep");
var animalsOption = new Option<string[]>(new[] { "-a", "--animals" });
animalsOption.AcceptOnlyFromAmong("dog", "cat", "sheep");
var vegetablesOption = new Option<string[]>(new[] { "-v", "--vegetables" });
var parser = new Parser(
new Command("the-command") {
Expand Down
Loading