From 0e81d71fbbb146ee448594e3dcdf9b45b4acdf2a Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Sat, 12 Mar 2022 11:28:09 -0800 Subject: [PATCH 01/11] wip --- .../CommandLine/Perf_Parser_ParseResult.cs | 2 +- .../CommandLine/Perf_Parser_TypoCorrection.cs | 2 +- .../ParameterBindingTests.cs | 2 +- .../CompletionContextTests.cs | 20 ++++---- .../DirectiveTests.cs | 20 ++++---- src/System.CommandLine.Tests/ParserTests.cs | 51 +++++++++---------- src/System.CommandLine/Option.cs | 4 ++ 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs index f3ca5fcc65..bf48c05412 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs @@ -20,7 +20,7 @@ public class Perf_Parser_ParseResult public Perf_Parser_ParseResult() { - var option = new Option("-opt"); + var option = new Option("-opt"); _testParser = new CommandLineBuilder(new RootCommand { option }) diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_TypoCorrection.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_TypoCorrection.cs index 6532821c1f..63a4996c95 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_TypoCorrection.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_TypoCorrection.cs @@ -22,7 +22,7 @@ public class Perf_Parser_TypoCorrection public Perf_Parser_TypoCorrection() { - var option = new Option("--0123456789"); + var option = new Option("--0123456789"); _testParser = new CommandLineBuilder(new RootCommand { option }) .UseTypoCorrections() diff --git a/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs b/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs index 2f0530845b..74b7ecedbc 100644 --- a/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs +++ b/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs @@ -51,7 +51,7 @@ void Execute(string firstName) var command = new Command("command") { - new Option("--first-name", arity: ArgumentArity.ExactlyOne) + new Option("--first-name") }; command.Handler = CommandHandler.Create(Execute); diff --git a/src/System.CommandLine.Tests/CompletionContextTests.cs b/src/System.CommandLine.Tests/CompletionContextTests.cs index 507dd38545..d1a371368c 100644 --- a/src/System.CommandLine.Tests/CompletionContextTests.cs +++ b/src/System.CommandLine.Tests/CompletionContextTests.cs @@ -76,8 +76,8 @@ public void When_position_is_unspecified_in_string_command_line_not_ending_with_ { var command = new Command("the-command") { - new Option("--option1"), - new Option("--option2") + new Option("--option1"), + new Option("--option2") }; string textToMatch = command.Parse("the-command t") @@ -92,8 +92,8 @@ public void When_position_is_unspecified_in_string_command_line_ending_with_a_sp { var command = new Command("the-command") { - new Option("--option1"), - new Option("--option2") + new Option("--option1"), + new Option("--option2") }; var commandLine = "the-command t"; @@ -130,8 +130,8 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_i { var command = new Command("the-command") { - new Option("--option1"), - new Option("--option2") + new Option("--option1"), + new Option("--option2") }; string textToMatch = command.Parse("the-command", "opt") @@ -146,8 +146,8 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m { var command = new Command("the-command") { - new Option("--option1"), - new Option("--option2") + new Option("--option1"), + new Option("--option2") }; string textToMatch = command.Parse(new[] { "the-command" }) @@ -162,8 +162,8 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m { var command = new Command("the-command") { - new Option("--option1"), - new Option("--option2") + new Option("--option1"), + new Option("--option2") }; string textToMatch = command.Parse("the-command", "--option1") diff --git a/src/System.CommandLine.Tests/DirectiveTests.cs b/src/System.CommandLine.Tests/DirectiveTests.cs index 37ea2573d5..9bddbe564b 100644 --- a/src/System.CommandLine.Tests/DirectiveTests.cs +++ b/src/System.CommandLine.Tests/DirectiveTests.cs @@ -14,7 +14,7 @@ public class DirectiveTests [Fact] public void Directives_should_not_be_considered_as_unmatched_tokens() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse($"{RootCommand.ExecutableName} [parse] -y"); @@ -24,7 +24,7 @@ public void Directives_should_not_be_considered_as_unmatched_tokens() [Fact] public void Raw_tokens_still_hold_directives() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse("[parse] -y"); @@ -35,7 +35,7 @@ public void Raw_tokens_still_hold_directives() [Fact] public void Directives_should_parse_into_the_directives_collection() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse("[parse] -y"); @@ -45,7 +45,7 @@ public void Directives_should_parse_into_the_directives_collection() [Fact] public void Multiple_directives_are_allowed() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse("[parse] [suggest] -y"); @@ -56,7 +56,7 @@ public void Multiple_directives_are_allowed() [Fact] public void Directives_must_be_the_first_argument() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse("-y [suggest]"); @@ -72,7 +72,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo string expectedKey, string expectedValue) { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse($"{directive} -y"); @@ -83,7 +83,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo [Fact] public void Directives_without_a_value_specified_have_a_value_of_empty_string() { - var option = new Option("-y"); + var option = new Option("-y"); var result = option.Parse("[parse] -y"); @@ -96,7 +96,7 @@ public void Directives_without_a_value_specified_have_a_value_of_empty_string() [InlineData("[:value]")] public void Directives_must_have_a_non_empty_key(string directive) { - var option = new Option("-a"); + var option = new Option("-a"); var result = option.Parse($"{directive} -a"); @@ -110,7 +110,7 @@ public void Directives_must_have_a_non_empty_key(string directive) [InlineData("[parse ]")] public void Directives_cannot_contain_spaces(object value) { - var option = new Option("-a"); + var option = new Option("-a"); var result = option.Parse($"{value} -a"); @@ -120,7 +120,7 @@ public void Directives_cannot_contain_spaces(object value) [Fact] public void When_a_directive_is_specified_more_than_once_then_its_values_are_aggregated() { - var option = new Option("-a"); + var option = new Option("-a"); var result = option.Parse("[directive:one] [directive:two] -a"); diff --git a/src/System.CommandLine.Tests/ParserTests.cs b/src/System.CommandLine.Tests/ParserTests.cs index 0e7eaea8bf..0c68f14d23 100644 --- a/src/System.CommandLine.Tests/ParserTests.cs +++ b/src/System.CommandLine.Tests/ParserTests.cs @@ -11,9 +11,6 @@ using System.Linq; using FluentAssertions.Common; using Xunit; -using System.ComponentModel; -using System.Globalization; -using System.Threading.Tasks; using Xunit.Abstractions; namespace System.CommandLine.Tests @@ -30,8 +27,8 @@ public ParserTests(ITestOutputHelper output) [Fact] public void An_option_can_be_checked_by_object_instance() { - var option = new Option("--flag"); - var option2 = new Option("--flag2"); + var option = new Option("--flag"); + var option2 = new Option("--flag2"); var result = new Parser(new RootCommand { option, option2 }) .Parse("--flag"); @@ -42,9 +39,9 @@ public void An_option_can_be_checked_by_object_instance() [Fact] public void Two_options_are_parsed_correctly() { - var optionOne = new Option(new[] { "-o", "--one" }); + var optionOne = new Option(new[] { "-o", "--one" }); - var optionTwo = new Option(new[] { "-t", "--two" }); + var optionTwo = new Option(new[] { "-t", "--two" }); var result = new Parser( new RootCommand @@ -101,7 +98,7 @@ public void Long_form_options_can_be_specified_using_equals_delimiter() [Fact] public void Short_form_options_can_be_specified_using_colon_delimiter() { - var option = new Option("-x") { Arity = ArgumentArity.ExactlyOne }; + var option = new Option("-x"); var result = option.Parse("-x:some-value"); @@ -126,9 +123,9 @@ public void Long_form_options_can_be_specified_using_colon_delimiter() public void Option_short_forms_can_be_bundled() { var command = new Command("the-command"); - command.AddOption(new Option("-x")); - command.AddOption(new Option("-y")); - command.AddOption(new Option("-z")); + command.AddOption(new Option("-x")); + command.AddOption(new Option("-y")); + command.AddOption(new Option("-z")); var result = command.Parse("the-command -xyz"); @@ -146,9 +143,9 @@ public void Options_short_forms_do_not_get_unbundled_if_unbundling_is_turned_off { new Command("the-command") { - new Option("-x"), - new Option("-y"), - new Option("-z") + new Option("-x"), + new Option("-y"), + new Option("-z") } }) .EnablePosixBundling(false) @@ -167,10 +164,10 @@ public void Option_long_forms_do_not_get_unbundled() var parser = new Parser( new Command("the-command") { - new Option("--xyz"), - new Option("-x"), - new Option("-y"), - new Option("-z") + new Option("--xyz"), + new Option("-x"), + new Option("-y"), + new Option("-z") }); var result = parser.Parse("the-command --xyz"); @@ -186,7 +183,7 @@ public void Option_long_forms_do_not_get_unbundled() public void Options_do_not_get_unbundled_unless_all_resulting_options_would_be_valid_for_the_current_command() { var outer = new Command("outer"); - outer.AddOption(new Option("-a")); + outer.AddOption(new Option("-a")); var inner = new Command("inner") { new Argument @@ -194,8 +191,8 @@ public void Options_do_not_get_unbundled_unless_all_resulting_options_would_be_v Arity = ArgumentArity.ZeroOrMore } }; - inner.AddOption(new Option("-b")); - inner.AddOption(new Option("-c")); + inner.AddOption(new Option("-b")); + inner.AddOption(new Option("-c")); outer.AddCommand(inner); var parser = new Parser(outer); @@ -213,8 +210,8 @@ public void Options_do_not_get_unbundled_unless_all_resulting_options_would_be_v public void Required_option_arguments_are_not_unbundled() { var optionA = new Option("-a"); - var optionB = new Option("-b"); - var optionC = new Option("-c"); + var optionB = new Option("-b"); + var optionC = new Option("-c"); var command = new RootCommand { @@ -795,7 +792,7 @@ public void A_root_command_can_be_omitted_from_the_parsed_args() { new Command("inner") { - new Option("-x") { Arity = ArgumentArity.ExactlyOne } + new Option("-x") } }; @@ -812,7 +809,7 @@ public void A_root_command_can_match_a_full_path_to_an_executable() { new Command("inner") { - new Option("-x") { Arity = ArgumentArity.ExactlyOne } + new Option("-x") } }; @@ -830,7 +827,7 @@ public void A_renamed_RootCommand_can_be_omitted_from_the_parsed_args() { new Command("inner") { - new Option("-x") { Arity = ArgumentArity.ExactlyOne } + new Option("-x") } }; rootCommand.Name = "outer"; @@ -1395,7 +1392,7 @@ public void When_a_command_line_has_unmatched_tokens_they_are_not_applied_to_sub { TreatUnmatchedTokensAsErrors = false }; - var optionX = new Option("-x") { Arity = ArgumentArity.ExactlyOne }; + var optionX = new Option("-x"); command.AddOption(optionX); var optionY = new Option("-y") { Arity = ArgumentArity.ExactlyOne }; command.AddOption(optionY); diff --git a/src/System.CommandLine/Option.cs b/src/System.CommandLine/Option.cs index 77ff4c47c1..508d8bfb4f 100644 --- a/src/System.CommandLine/Option.cs +++ b/src/System.CommandLine/Option.cs @@ -19,6 +19,10 @@ public class Option : IdentifierSymbol, IValueDescriptor private List>? _validators; private Argument? _argument; + public Option(string name) : this(name, description: null){} + + public Option(string name, string description) : this(name, description, argumentType: null){} + /// /// Initializes a new instance of the class. /// From 55c90353f5aa792c449a058ec4c7d39e9b660ca1 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Sun, 13 Mar 2022 17:07:44 -0700 Subject: [PATCH 02/11] wip --- .../TableRenderingTests.cs | 16 +-- .../Binding/TypeConversionTests.cs | 115 +----------------- .../CompletionTests.cs | 16 +-- .../GlobalOptionTests.cs | 18 --- .../Help/HelpBuilderTests.cs | 70 ++++++----- .../VersionOptionTests.cs | 2 +- src/System.CommandLine/Argument.cs | 1 + src/System.CommandLine/Option.cs | 16 ++- 8 files changed, 74 insertions(+), 180 deletions(-) diff --git a/src/System.CommandLine.Rendering.Tests/TableRenderingTests.cs b/src/System.CommandLine.Rendering.Tests/TableRenderingTests.cs index f8574deed5..91febb9353 100644 --- a/src/System.CommandLine.Rendering.Tests/TableRenderingTests.cs +++ b/src/System.CommandLine.Rendering.Tests/TableRenderingTests.cs @@ -34,8 +34,8 @@ public TableRenderingTests(ITestOutputHelper output) public void A_row_is_written_for_each_item_and_a_header_for_each_column(OutputMode outputMode) { var options = new[] { - new Option("-s", "a short option"), - new Option("--very-long", "a long option") + new Option("-s", "a short option"), + new Option("--very-long", "a long option") }; var view = new OptionsHelpView(options); @@ -57,8 +57,8 @@ public void A_row_is_written_for_each_item_and_a_header_for_each_column_in_file_ { var options = new[] { - new Option("-s", "a short option"), - new Option("--very-long", "a long option") + new Option("-s", "a short option"), + new Option("--very-long", "a long option") }; var view = new OptionsHelpView(options); @@ -80,8 +80,8 @@ public void A_row_is_written_for_each_item_and_a_header_for_each_column_in_file_ public void Column_widths_are_aligned_to_the_longest_cell(OutputMode outputMode) { var options = new[] { - new Option("-s", "an option"), - new Option("--very-long", "an option") + new Option("-s", "an option"), + new Option("--very-long", "an option") }; var view = new OptionsHelpView(options); @@ -101,8 +101,8 @@ public void Column_widths_are_aligned_to_the_longest_cell(OutputMode outputMode) public void Column_widths_are_aligned_to_the_longest_cell_in_file_mode() { var options = new[] { - new Option("-s", "an option"), - new Option("--very-long", "an option") + new Option("-s", "an option"), + new Option("--very-long", "an option") }; var view = new OptionsHelpView(options); diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 56474021db..37290428b5 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -235,41 +235,7 @@ public void Generic_option_bool_parses_when_passed_to_non_generic_GetValueForOpt parseResult.GetValueForOption((Option)option).Should().Be(true); } - - [Fact] - public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value() - { - var option = new Option("-x", arity: ArgumentArity.ZeroOrOne); - - var command = new Command("the-command") - { - option - }; - - var result = command.Parse("the-command -x the-argument"); - - result.GetValueForOption(option) - .Should() - .Be("the-argument"); - } - - [Fact] - public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value() - { - var option = new Option("-x", arity: ArgumentArity.ExactlyOne); - - var command = new Command("the-command") - { - option - }; - - var result = command.Parse("the-command -x the-argument"); - - result.GetValueForOption(option) - .Should() - .Be("the-argument"); - } - + [Fact] public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws() { @@ -291,81 +257,7 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get .Should() .Be("Required argument missing for option: '-x'."); } - - [Fact] - public void When_zero_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_returns_an_empty_sequence_of_strings() - { - var option = new Option("-x", arity: ArgumentArity.ZeroOrMore); - - var command = new Command("the-command") - { - option - }; - - var result = command.Parse("the-command -x"); - - result.GetValueForOption(option) - .Should() - .BeAssignableTo>() - .Which - .Should() - .BeEmpty(); - } - - [Fact] - public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_throws() - { - var option = new Option("-x", arity: ArgumentArity.OneOrMore); - - var command = new Command("the-command") - { - option - }; - - var result = command.Parse("the-command -x"); - - Action getValue = () => result.GetValueForOption(option); - - getValue.Should() - .Throw() - .Which - .Message - .Should() - .Be("Required argument missing for option: '-x'."); - } - - [Fact] - public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_multiple_arguments_parses_as_a_sequence_of_strings() - { - var option = new Option("-x", arity: ArgumentArity.ZeroOrMore); - - var command = new Command("the-command") - { - option - }; - - command.Parse("the-command -x arg1 -x arg2") - .GetValueForOption(option) - .Should() - .BeEquivalentTo(new[] { "arg1", "arg2" }); - } - - [Fact] - public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_one_argument_parses_as_a_sequence_of_strings() - { - var option = new Option("-x", arity: ArgumentArity.ZeroOrMore); - - var command = new Command("the-command") - { - option - }; - - command.Parse("the-command -x arg1") - .GetValueForOption(option) - .Should() - .BeEquivalentTo(new[] { "arg1" }); - } - + [Theory] [InlineData("c -a o c c")] [InlineData("c c -a o c")] @@ -856,7 +748,8 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types( int maxArity, Type argumentType) { - var option = new Option("--items", argumentType: argumentType, arity: new ArgumentArity(minArity, maxArity)); + var arity = new ArgumentArity(minArity, maxArity); + var option = new Option("--items", argumentType: argumentType, arity: arity); var command = new RootCommand { diff --git a/src/System.CommandLine.Tests/CompletionTests.cs b/src/System.CommandLine.Tests/CompletionTests.cs index 15554e8443..c8abfa162f 100644 --- a/src/System.CommandLine.Tests/CompletionTests.cs +++ b/src/System.CommandLine.Tests/CompletionTests.cs @@ -41,9 +41,9 @@ public void Command_GetCompletions_returns_available_option_aliases() { var command = new Command("command") { - new Option("--one", "option one"), - new Option("--two", "option two"), - new Option("--three", "option three") + new Option("--one", "option one"), + new Option("--two", "option two"), + new Option("--three", "option three") }; var completions = command.GetCompletions(); @@ -59,8 +59,8 @@ public void Command_GetCompletions_returns_available_option_aliases_for_global_o { var subcommand = new Command("command") { - new Option("--one", "option one"), - new Option("--two", "option two") + new Option("--one", "option one"), + new Option("--two", "option two") }; var rootCommand = new RootCommand @@ -68,7 +68,7 @@ public void Command_GetCompletions_returns_available_option_aliases_for_global_o subcommand }; - rootCommand.AddGlobalOption(new Option("--three", "option three")); + rootCommand.AddGlobalOption(new Option("--three", "option three")); var completions = subcommand.GetCompletions(); @@ -118,7 +118,7 @@ public void Command_GetCompletions_returns_available_subcommands_and_option_alia var command = new Command("command") { new Command("subcommand", "subcommand"), - new Option("--option", "option"), + new Option("--option", "option"), new Argument { Arity = ArgumentArity.OneOrMore, @@ -426,7 +426,7 @@ public void An_option_can_be_hidden_from_completions_by_setting_IsHidden_to_true { IsHidden = true }, - new Option("-n", "Not hidden") + new Option("-n", "Not hidden") }; var completions = command.Parse("the-command ").GetCompletions(); diff --git a/src/System.CommandLine.Tests/GlobalOptionTests.cs b/src/System.CommandLine.Tests/GlobalOptionTests.cs index ff20a30e74..f6884378fb 100644 --- a/src/System.CommandLine.Tests/GlobalOptionTests.cs +++ b/src/System.CommandLine.Tests/GlobalOptionTests.cs @@ -1,10 +1,6 @@ // 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. -// 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.Threading.Tasks; using FluentAssertions; using Xunit; @@ -12,20 +8,6 @@ namespace System.CommandLine.Tests { public class GlobalOptionTests { - [Fact] - public void Global_options_may_be_added_with_aliases_that_conflict_with_local_options() - { - var command = new Command("the-command") - { - new Option("--same") - }; - - command - .Invoking(c => c.AddGlobalOption(new Option("--same"))) - .Should() - .NotThrow(); - } - [Fact] public void Global_options_appear_in_options_list() { diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs index d4e74bcc06..9853a1096d 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs @@ -208,7 +208,7 @@ public void Usage_section_for_subcommand_shows_names_of_parent_commands() outer.AddCommand(inner); var innerEr = new Command("inner-er", "the inner-er command"); inner.AddCommand(innerEr); - innerEr.AddOption(new Option("--some-option", "some option")); + innerEr.AddOption(new Option("--some-option", "some option")); var rootCommand = new RootCommand(); rootCommand.Add(outer); @@ -226,7 +226,7 @@ public void Usage_section_for_subcommand_shows_arguments_for_subcommand_and_pare { var inner = new Command("inner", "command help") { - new Option("-v", "Sets the verbosity"), + new Option("-v", "Sets the verbosity"), new Argument { Name = "inner-args" @@ -257,7 +257,7 @@ public void Usage_section_does_not_show_additional_arguments_when_TreatUnmatched "some-command", "Does something"); command.AddOption( - new Option("-x", "Indicates whether x")); + new Option("-x", "Indicates whether x")); _helpBuilder.Write(command, _console); @@ -270,7 +270,7 @@ public void Usage_section_does_not_show_additional_arguments_when_TreatUnmatched var command = new RootCommand(); var subcommand = new Command("some-command", "Does something"); command.AddCommand(subcommand); - subcommand.AddOption(new Option("-x", "Indicates whether x")); + subcommand.AddOption(new Option("-x", "Indicates whether x")); subcommand.TreatUnmatchedTokensAsErrors = true; _helpBuilder.Write(subcommand, _console); @@ -284,7 +284,7 @@ public void Usage_section_shows_additional_arguments_when_TreatUnmatchedTokensAs var command = new RootCommand(); var subcommand = new Command("some-command", "Does something"); command.AddCommand(subcommand); - subcommand.AddOption(new Option("-x", "Indicates whether x")); + subcommand.AddOption(new Option("-x", "Indicates whether x")); subcommand.TreatUnmatchedTokensAsErrors = false; _helpBuilder.Write(subcommand, _console); @@ -449,7 +449,7 @@ public void Arguments_section_is_not_included_if_there_are_only_options_with_arg { var command = new Command("command") { - new Option("-v", "Sets the verbosity.", arity: ArgumentArity.ExactlyOne) + new Option("-v", "Sets the verbosity.") { ArgumentHelpName = "argument for options" } @@ -755,17 +755,21 @@ public void Command_argument_usage_indicates_enums_values(Type type) } [Theory] - [InlineData(typeof(bool))] - [InlineData(typeof(bool?))] - public void Option_argument_usage_is_empty_for_boolean_values(Type type) + [InlineData(true)] + [InlineData(false)] + public void Option_argument_usage_is_empty_for_boolean_values(bool nullable) { var description = "This is the option description"; + Option option = nullable + ? new Option("--opt", description) + : new Option("--opt", description); + var command = new Command( "outer", "Help text for the outer command") - { - new Option("--opt", description, argumentType: type) - }; + { + option + }; HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth); @@ -796,17 +800,21 @@ public void Command_arguments_show_argument_name_in_first_column() } [Theory] - [InlineData(typeof(FileAccess))] - [InlineData(typeof(FileAccess?))] - public void Option_argument_first_column_indicates_enums_values(Type type) + [InlineData(true)] + [InlineData(false)] + public void Option_argument_first_column_indicates_enums_values(bool nullable) { var description = "This is the argument description"; + Option option = nullable + ? new Option("--opt", description) + : new Option("--opt", description); + var command = new Command( - "outer", "Help text for the outer command") - { - new Option("--opt", description, argumentType: type) - }; + "outer", "Help text for the outer command") + { + option + }; HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth); @@ -860,7 +868,7 @@ public void Help_does_not_show_default_value_for_argument_when_default_value_is_ [Fact] public void Help_does_not_show_default_value_for_option_when_default_value_is_empty() { - var option = new Option("-x", description: "The option description"); + var option = new Option("-x", description: "The option description"); option.SetDefaultValue(""); var command = new Command("the-command", "The command description") @@ -964,8 +972,8 @@ public void Options_section_includes_option_with_empty_description() { var command = new Command("the-command", "Does things.") { - new Option("-x"), - new Option("-n") + new Option("-x"), + new Option("-n") }; _helpBuilder.Write(command, _console); @@ -979,11 +987,11 @@ public void Options_section_includes_option_with_empty_description() public void Options_section_does_not_contain_option_with_HelpDefinition_that_IsHidden() { var command = new Command("the-command"); - command.AddOption(new Option("-x", "Is Hidden") + command.AddOption(new Option("-x", "Is Hidden") { IsHidden = true }); - command.AddOption(new Option("-n", "Not Hidden") + command.AddOption(new Option("-n", "Not Hidden") { IsHidden = false }); @@ -1089,9 +1097,9 @@ public void Options_section_properly_wraps_description() var command = new Command("test-command", "Help text for the command") { - new Option("-x", "Option with a short description"), + new Option("-x", "Option with a short description"), new Option(new[] { "-a", "--aaa" }, longOptionText), - new Option("-y", "Option with a short description"), + new Option("-y", "Option with a short description"), }; HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth); @@ -1114,9 +1122,9 @@ public void Options_section_properly_wraps_description_when_long_default_value_i var command = new Command("test-command", "Help text for the command") { - new Option("-x", "Option with a short description"), + new Option("-x", "Option with a short description"), new Option(new[] { "-a", "--aaa" }, longOptionText, getDefaultValue: () => "the quick brown fox jumps over the lazy dog"), - new Option("-y", "Option with a short description"), + new Option("-y", "Option with a short description"), }; HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth); @@ -1138,7 +1146,7 @@ public void Options_section_properly_wraps() var command = new RootCommand { - new Option(alias, description) + new Option(alias, description) }; HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth); @@ -1158,7 +1166,7 @@ public void Required_options_are_indicated() { var command = new RootCommand { - new Option("--required") + new Option("--required") { IsRequired = true } @@ -1346,7 +1354,7 @@ public void Subcommand_help_does_not_include_names_of_sibling_commands() new Command( "inner-er", "inner-er description") { - new Option("some-option", + new Option("some-option", "some-option description") } }; diff --git a/src/System.CommandLine.Tests/VersionOptionTests.cs b/src/System.CommandLine.Tests/VersionOptionTests.cs index d813c6144e..046b863876 100644 --- a/src/System.CommandLine.Tests/VersionOptionTests.cs +++ b/src/System.CommandLine.Tests/VersionOptionTests.cs @@ -75,7 +75,7 @@ public async Task When_the_version_option_is_specified_and_there_are_default_opt { var rootCommand = new RootCommand { - new Option("-x", getDefaultValue: () => true) + new Option("-x") }; rootCommand.SetHandler(() => { }); diff --git a/src/System.CommandLine/Argument.cs b/src/System.CommandLine/Argument.cs index 5d9c0d280d..7ac9603a2b 100644 --- a/src/System.CommandLine/Argument.cs +++ b/src/System.CommandLine/Argument.cs @@ -28,6 +28,7 @@ public class Argument : Symbol, IValueDescriptor /// public Argument() { + // FIX: (Argument) make this protected } /// diff --git a/src/System.CommandLine/Option.cs b/src/System.CommandLine/Option.cs index 508d8bfb4f..8d82d528b4 100644 --- a/src/System.CommandLine/Option.cs +++ b/src/System.CommandLine/Option.cs @@ -19,9 +19,19 @@ public class Option : IdentifierSymbol, IValueDescriptor private List>? _validators; private Argument? _argument; - public Option(string name) : this(name, description: null){} - - public Option(string name, string description) : this(name, description, argumentType: null){} + // FIX: (Option) delete temp constructors + + public Option(string name) : this(name, description: null) + { + } + + public Option(string name, ArgumentArity arity) : this(name, description: null, arity: arity) + { + } + + public Option(string name, Func? getDefaultValue) : this(name,null, getDefaultValue: getDefaultValue) + { + } /// /// Initializes a new instance of the class. From 9158be6085432121e2ae62a2be5466868596a2ec Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Mon, 14 Mar 2022 11:20:22 -0700 Subject: [PATCH 03/11] wip --- .../CommandLine/Perf_Parser_Options_Bare.cs | 5 +- .../Perf_Parser_Options_With_Arguments.cs | 3 +- .../CommandLine/Perf_Suggestions.cs | 4 +- .../ModelBindingCommandHandlerTests.cs | 18 --- .../ParameterBindingTests.cs | 2 +- .../Binding/TypeConversionTests.cs | 8 +- .../CommandLineConfigurationTests.cs | 4 +- src/System.CommandLine.Tests/CommandTests.cs | 4 +- .../CompletionTests.cs | 67 +++----- .../Invocation/TypoCorrectionTests.cs | 18 +-- src/System.CommandLine.Tests/OptionTests.cs | 67 +++----- .../ParseDiagramTests.cs | 13 +- src/System.CommandLine/IdentifierSymbol.cs | 2 +- .../Parsing/ParseResultExtensions.cs | 145 ++++++++++-------- 14 files changed, 155 insertions(+), 205 deletions(-) diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs index b4b69698db..ca6d9d3a70 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs @@ -22,8 +22,9 @@ public class Perf_Parser_Options_Bare private IEnumerable