From 1fd117972d5a785a6ef6798536f59c96265d6003 Mon Sep 17 00:00:00 2001 From: Kevin Bost Date: Sat, 29 Oct 2022 10:20:17 -0700 Subject: [PATCH] Rename members to GetValue Added GetValue methods to InvocationContext Renamed GetValueForOption and GetValueForArgument on ParseResult and SymbolResult classes Fixing compilation tests --- ...ommandLine_api_is_not_changed.approved.txt | 20 +-- .../Parameters/ArgumentParameter.cs | 2 +- .../Parameters/OptionParameter.cs | 2 +- .../ModelBinderTests.cs | 64 +++++++++ .../ParameterBindingTests.cs | 6 +- .../SuggestionDispatcher.cs | 8 +- src/System.CommandLine.Tests/ArgumentTests.cs | 28 ++-- .../Binding/SetHandlerTests.cs | 4 +- .../Binding/TypeConversionTests.cs | 134 +++++++++--------- .../CompletionTests.cs | 2 +- .../GlobalOptionTests.cs | 10 +- .../OptionTests.MultipleArgumentsPerToken.cs | 10 +- src/System.CommandLine.Tests/OptionTests.cs | 10 +- .../ParserTests.DoubleDash.cs | 6 +- .../ParserTests.MultipleArguments.cs | 34 ++--- src/System.CommandLine.Tests/ParserTests.cs | 44 +++--- .../ParsingValidationTests.cs | 8 +- .../ResponseFileTests.cs | 28 ++-- .../TestApps/NativeAOT/Program.cs | 4 +- .../TestApps/Trimming/Program.cs | 2 +- .../TokenReplacementTests.cs | 14 +- .../Invocation/InvocationContext.cs | 16 +++ src/System.CommandLine/ParseResult.cs | 24 ++-- .../Parsing/SymbolResult.cs | 16 +-- 24 files changed, 290 insertions(+), 206 deletions(-) diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index bd25b25078..bddfec9264 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -241,10 +241,10 @@ System.CommandLine public System.CommandLine.Parsing.SymbolResult FindResultFor(Symbol symbol) public System.CommandLine.Completions.CompletionContext GetCompletionContext() public System.Collections.Generic.IEnumerable GetCompletions(System.Nullable position = null) - public System.Object GetValueForArgument(Argument argument) - public T GetValueForArgument(Argument argument) - public System.Object GetValueForOption(Option option) - public T GetValueForOption(Option option) + public System.Object GetValue(Option option) + public System.Object GetValue(Argument argument) + public T GetValue(Argument argument) + public T GetValue(Option option) public System.String ToString() public class RootCommand : Command, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.CommandLine.Completions.ICompletionSource public static System.String ExecutableName { get; } @@ -366,6 +366,10 @@ System.CommandLine.Invocation public System.CommandLine.Parsing.Parser Parser { get; } public System.CommandLine.ParseResult ParseResult { get; set; } public System.Threading.CancellationToken GetCancellationToken() + public System.Object GetValue(System.CommandLine.Option option) + public T GetValue(Option option) + public System.Object GetValue(System.CommandLine.Argument argument) + public T GetValue(Argument argument) public System.Void LinkToken(System.Threading.CancellationToken token) public delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable .ctor(System.Object object, System.IntPtr method) @@ -466,10 +470,10 @@ System.CommandLine.Parsing public ArgumentResult FindResultFor(System.CommandLine.Argument argument) public CommandResult FindResultFor(System.CommandLine.Command command) public OptionResult FindResultFor(System.CommandLine.Option option) - public T GetValueForArgument(Argument argument) - public System.Object GetValueForArgument(System.CommandLine.Argument argument) - public T GetValueForOption(Option option) - public System.Object GetValueForOption(System.CommandLine.Option option) + public T GetValue(Argument argument) + public System.Object GetValue(System.CommandLine.Argument argument) + public T GetValue(Option option) + public System.Object GetValue(System.CommandLine.Option option) public System.String ToString() public class Token, System.IEquatable public static System.Boolean op_Equality(Token left, Token right) diff --git a/src/System.CommandLine.Generator/Parameters/ArgumentParameter.cs b/src/System.CommandLine.Generator/Parameters/ArgumentParameter.cs index cdc56a99b1..98172485a1 100644 --- a/src/System.CommandLine.Generator/Parameters/ArgumentParameter.cs +++ b/src/System.CommandLine.Generator/Parameters/ArgumentParameter.cs @@ -10,7 +10,7 @@ public ArgumentParameter(string localName, INamedTypeSymbol type, ITypeSymbol va } public override string GetValueFromContext() - => $"context.ParseResult.GetValueForArgument({LocalName})"; + => $"context.ParseResult.GetValue({LocalName})"; public override int GetHashCode() => base.GetHashCode(); diff --git a/src/System.CommandLine.Generator/Parameters/OptionParameter.cs b/src/System.CommandLine.Generator/Parameters/OptionParameter.cs index af14fb96b0..89667a38b1 100644 --- a/src/System.CommandLine.Generator/Parameters/OptionParameter.cs +++ b/src/System.CommandLine.Generator/Parameters/OptionParameter.cs @@ -11,7 +11,7 @@ public OptionParameter(string localName, INamedTypeSymbol type, ITypeSymbol valu } public override string GetValueFromContext() - => $"context.ParseResult.GetValueForOption({LocalName})"; + => $"context.ParseResult.GetValue({LocalName})"; public override int GetHashCode() => base.GetHashCode(); diff --git a/src/System.CommandLine.NamingConventionBinder.Tests/ModelBinderTests.cs b/src/System.CommandLine.NamingConventionBinder.Tests/ModelBinderTests.cs index 656b04cc10..a93a3ca6d8 100644 --- a/src/System.CommandLine.NamingConventionBinder.Tests/ModelBinderTests.cs +++ b/src/System.CommandLine.NamingConventionBinder.Tests/ModelBinderTests.cs @@ -774,6 +774,70 @@ public void Binder_does_not_match_by_substring() boundOptions.BundleId.Should().Be("value"); } + [Fact] + public void InvocationContext_GetValue_with_generic_option_returns_value() + { + Option option = new("--number"); + Command command = new("the-command") + { + option + }; + + InvocationContext invocationContext = new(command.Parse("the-command --number 42")); + + invocationContext.GetValue(option) + .Should() + .Be(42); + } + + [Fact] + public void InvocationContext_GetValue_with_non_generic_option_returns_value() + { + Option option = new Option("--number"); + Command command = new("the-command") + { + option + }; + + InvocationContext invocationContext = new(command.Parse("the-command --number 42")); + + invocationContext.GetValue(option) + .Should() + .Be(42); + } + + [Fact] + public void InvocationContext_GetValue_with_generic_argument_returns_value() + { + Argument option = new(); + Command command = new("the-command") + { + option + }; + + InvocationContext invocationContext = new(command.Parse("the-command 42")); + + invocationContext.GetValue(option) + .Should() + .Be(42); + } + + [Fact] + public void InvocationContext_GetValue_with_non_generic_argument_returns_value() + { + Argument option = new Argument(); + Command command = new("the-command") + { + option + }; + + InvocationContext invocationContext = new(command.Parse("the-command 42")); + + invocationContext.GetValue(option) + .Should() + .Be(42); + } + class DeployOptions { public string Bundle { get; set; } diff --git a/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs b/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs index 22bdc8d769..a92a0a84bd 100644 --- a/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs +++ b/src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs @@ -233,7 +233,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Pars await command.InvokeAsync("command -x 123", _console); - boundParseResult.GetValueForOption(option).Should().Be(123); + boundParseResult.GetValue(option).Should().Be(123); } [Fact] @@ -250,7 +250,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Bind await command.InvokeAsync("command -x 123", _console); - boundContext.ParseResult.GetValueForOption(option).Should().Be(123); + boundContext.ParseResult.GetValue(option).Should().Be(123); } [Fact] @@ -282,7 +282,7 @@ public async Task Method_parameters_of_type_InvocationContext_receive_the_curren await command.InvokeAsync("command -x 123", _console); - boundContext.ParseResult.GetValueForOption(option).Should().Be(123); + boundContext.ParseResult.GetValue(option).Should().Be(123); } private class ExecuteTestClass diff --git a/src/System.CommandLine.Suggest/SuggestionDispatcher.cs b/src/System.CommandLine.Suggest/SuggestionDispatcher.cs index 516142af96..ef916dbe0d 100644 --- a/src/System.CommandLine.Suggest/SuggestionDispatcher.cs +++ b/src/System.CommandLine.Suggest/SuggestionDispatcher.cs @@ -33,7 +33,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug }; CompleteScriptCommand.SetHandler(context => { - SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument)); + SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValue(shellTypeArgument)); }); ListCommand = new Command("list") @@ -63,7 +63,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug RegisterCommand.SetHandler(context => { - Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console); + Register(context.ParseResult.GetValue(commandPathOption), context.Console); return Task.FromResult(0); }); @@ -130,7 +130,7 @@ private void Register( private Task Get(InvocationContext context) { var parseResult = context.ParseResult; - var commandPath = parseResult.GetValueForOption(ExecutableOption); + var commandPath = parseResult.GetValue(ExecutableOption); Registration suggestionRegistration; if (commandPath.FullName == DotnetMuxer.Path.FullName) @@ -142,7 +142,7 @@ private Task Get(InvocationContext context) suggestionRegistration = _suggestionRegistration.FindRegistration(commandPath); } - var position = parseResult.GetValueForOption(PositionOption); + var position = parseResult.GetValue(PositionOption); if (suggestionRegistration is null) { diff --git a/src/System.CommandLine.Tests/ArgumentTests.cs b/src/System.CommandLine.Tests/ArgumentTests.cs index 19ffb3db3f..84abe6f2cd 100644 --- a/src/System.CommandLine.Tests/ArgumentTests.cs +++ b/src/System.CommandLine.Tests/ArgumentTests.cs @@ -183,7 +183,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_one_token() var argument = new Argument(result => int.Parse(result.Tokens.Single().Value)); argument.Parse("123") - .GetValueForArgument(argument) + .GetValue(argument) .Should() .Be(123); } @@ -194,7 +194,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_one_token() var argument = new Argument>(result => result.Tokens.Single().Value.Split(',').Select(int.Parse)); argument.Parse("1,2,3") - .GetValueForArgument(argument) + .GetValue(argument) .Should() .BeEquivalentTo(new[] { 1, 2, 3 }); } @@ -208,7 +208,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke }); argument.Parse("1 2 3") - .GetValueForArgument(argument) + .GetValue(argument) .Should() .BeEquivalentTo(new[] { 1, 2, 3 }); } @@ -222,7 +222,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens }; argument.Parse("1 2 3") - .GetValueForArgument(argument) + .GetValue(argument) .Should() .Be(6); } @@ -367,7 +367,7 @@ public void Default_value_and_custom_argument_parser_can_be_used_together() var result = argument.Parse(""); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .Be(123); } @@ -449,7 +449,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va var result = command.Parse("the-command -o not-an-int"); Action getValue = () => - result.GetValueForOption(option); + result.GetValue(option); getValue.Should() .Throw() @@ -511,7 +511,7 @@ public void Parse_delegate_is_called_when_Option_Arity_allows_zero_tokens(string opt }; - rootCommand.Parse(commandLine).GetValueForOption(opt).Should().Be(expectedValue); + rootCommand.Parse(commandLine).GetValue(opt).Should().Be(expectedValue); } [Theory] @@ -691,9 +691,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_one_multiple_arity_argument_to_ var result = command.Parse("1 2 3"); - result.GetValueForArgument(argument1).Should().BeEmpty(); + result.GetValue(argument1).Should().BeEmpty(); - result.GetValueForArgument(argument2).Should().BeEquivalentSequenceTo(1, 2, 3); + result.GetValue(argument2).Should().BeEquivalentSequenceTo(1, 2, 3); } [Fact] // https://github.com/dotnet/command-line-api/issues/1759 @@ -714,9 +714,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot var result = command.Parse("1 2 3"); - result.GetValueForArgument(scalar).Should().BeNull(); + result.GetValue(scalar).Should().BeNull(); - result.GetValueForArgument(multiple).Should().BeEquivalentSequenceTo(1, 2, 3); + result.GetValue(multiple).Should().BeEquivalentSequenceTo(1, 2, 3); } @@ -759,9 +759,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot var result = command.Parse("1 2 3"); - result.GetValueForArgument(first).Should().BeNull(); - result.GetValueForArgument(second).Should().BeEmpty(); - result.GetValueForArgument(third).Should().BeEquivalentSequenceTo("1", "2", "3"); + result.GetValue(first).Should().BeNull(); + result.GetValue(second).Should().BeEmpty(); + result.GetValue(third).Should().BeEquivalentSequenceTo("1", "2", "3"); } } diff --git a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs index a8c3848641..eb4d168259 100644 --- a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs +++ b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs @@ -63,8 +63,8 @@ protected override CustomType GetBoundValue(BindingContext bindingContext) return new CustomType { Console = bindingContext.Console, - IntValue = bindingContext.ParseResult.GetValueForOption(_intOption), - StringValue = bindingContext.ParseResult.GetValueForArgument(_stringArg), + IntValue = bindingContext.ParseResult.GetValue(_intOption), + StringValue = bindingContext.ParseResult.GetValue(_stringArg), }; } } diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 62552003ad..891b9a7121 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -22,7 +22,7 @@ public void Option_argument_of_FileInfo_can_be_bound_without_custom_conversion_l var file = new FileInfo(Path.Combine(new DirectoryInfo("temp").FullName, "the-file.txt")); var result = option.Parse($"--file {file.FullName}"); - result.GetValueForOption(option) + result.GetValue(option) .Name .Should() .Be("the-file.txt"); @@ -41,7 +41,7 @@ public void Command_argument_of_FileInfo_can_be_bound_without_custom_conversion_ var file = new FileInfo(Path.Combine(new DirectoryInfo("temp").FullName, "the-file.txt")); var result = command.Parse($"{file.FullName}"); - result.GetValueForArgument(argument) + result.GetValue(argument) .Name .Should() .Be("the-file.txt"); @@ -61,7 +61,7 @@ public void Command_argument_of_FileInfo_returns_null_when_argument_is_not_provi var result = command.Parse(""); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .BeNull(); } @@ -90,7 +90,7 @@ public void Argument_of_array_of_FileInfo_can_be_called_without_custom_conversio var file2 = new FileInfo(Path.Combine(new DirectoryInfo("temp").FullName, "file2.txt")); var result = option.Parse($"--file {file1.FullName} --file {file2.FullName}"); - result.GetValueForOption(option) + result.GetValue(option) .Select(fi => fi.Name) .Should() .BeEquivalentTo("file1.txt", "file2.txt"); @@ -146,7 +146,7 @@ public void Argument_parses_as_the_default_value_when_the_option_has_not_been_ap var result = command.Parse("something"); - result.GetValueForOption(option).Should().Be(123); + result.GetValue(option).Should().Be(123); } [Fact] @@ -161,7 +161,7 @@ public void Option_does_not_parse_as_the_default_value_when_the_option_has_been_ var result = command.Parse("something -x 456"); - result.GetValueForOption(option).Should().Be(456); + result.GetValue(option).Should().Be(456); } [Theory] @@ -180,7 +180,7 @@ public void Bool_parses_as_true_when_the_option_has_been_applied(string commandL command .Parse(commandLine) - .GetValueForOption(option) + .GetValue(option) .Should() .BeTrue(); } @@ -201,7 +201,7 @@ public void Nullable_bool_parses_as_true_when_the_option_has_been_applied(string command .Parse(commandLine) - .GetValueForOption(option) + .GetValue(option) .Should() .BeTrue(); } @@ -221,7 +221,7 @@ public void Nullable_bool_parses_as_false_when_the_option_has_been_applied(strin command .Parse(commandLine) - .GetValueForOption(option) + .GetValue(option) .Should() .BeFalse(); } @@ -233,7 +233,7 @@ public void Nullable_bool_parses_as_null_when_the_option_has_not_been_applied() option .Parse("") - .GetValueForOption(option) + .GetValue(option) .Should() .Be(null); } @@ -250,7 +250,7 @@ public void Generic_option_bool_parses_when_passed_to_non_generic_GetValueForOpt var parseResult = cmd.Parse("-b"); - parseResult.GetValueForOption((Option)option).Should().Be(true); + parseResult.GetValue((Option)option).Should().Be(true); } [Fact] @@ -265,7 +265,7 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get var result = command.Parse("the-command -x"); - Action getValue = () => result.GetValueForOption(option); + Action getValue = () => result.GetValue(option); getValue.Should() .Throw() @@ -294,7 +294,7 @@ public void When_command_argument_has_arity_greater_than_one_it_captures_argumen var result = command.Parse(commandLine); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .BeEquivalentTo(new[] { "c", "c", "c" }); } @@ -312,7 +312,7 @@ public void The_default_value_of_a_bool_option_with_no_arguments_is_true() var result = command.Parse("-x"); - result.GetValueForOption(option) + result.GetValue(option) .Should() .Be(true); } @@ -329,7 +329,7 @@ public void By_default_a_bool_option_without_arguments_parses_as_false_when_it_i var result = command.Parse("something"); - result.GetValueForOption(option) + result.GetValue(option) .Should() .Be(false); } @@ -346,7 +346,7 @@ public void An_option_with_a_default_value_parses_as_the_default_value_when_the_ var result = command.Parse("something"); - result.GetValueForOption(option) + result.GetValue(option) .Should() .Be("123"); } @@ -363,7 +363,7 @@ public void An_option_with_a_default_value_of_null_parses_as_null_when_the_optio var result = command.Parse("something"); - result.GetValueForOption(option) + result.GetValue(option) .Should() .Be(null); } @@ -379,7 +379,7 @@ public void A_default_value_of_a_non_string_type_can_be_specified() }; command.Parse("something") - .GetValueForOption(option) + .GetValue(option) .Should() .Be(123); } @@ -398,7 +398,7 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_an_op var result = command.Parse("something"); - result.GetValueForOption(option).Should().Be(directoryInfo); + result.GetValue(option).Should().Be(directoryInfo); } [Fact] @@ -417,7 +417,7 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com result.Errors.Should().BeEmpty(); - var value = result.GetValueForArgument(argument); + var value = result.GetValue(argument); value.Should().Be(directoryInfo); } @@ -434,7 +434,7 @@ public void Specifying_an_option_argument_overrides_the_default_value() var result = command.Parse("something -x 456"); - var value = result.GetValueForOption(option); + var value = result.GetValue(option); value.Should().Be(456); } @@ -446,7 +446,7 @@ public void Values_can_be_correctly_converted_to_DateTime_without_the_parser_spe var option = new Option("-x"); var dateString = "2022-02-06T01:46:03.0000000-08:00"; - var value = option.Parse($"-x {dateString}").GetValueForOption(option); + var value = option.Parse($"-x {dateString}").GetValue(option); value.Should().Be(DateTime.Parse(dateString)); } @@ -458,7 +458,7 @@ public void Values_can_be_correctly_converted_to_nullable_DateTime_without_the_p var option = new Option("-x"); var dateString = "2022-02-06T01:46:03.0000000-08:00"; - var value = option.Parse($"-x {dateString}").GetValueForOption(option); + var value = option.Parse($"-x {dateString}").GetValue(option); value.Should().Be(DateTime.Parse(dateString)); } @@ -469,7 +469,7 @@ public void Values_can_be_correctly_converted_to_DateTimeOffset_without_the_pars var option = new Option("-x"); var dateString = "2022-02-06T09:52:54.5275055-08:00"; - var value = option.Parse($"-x {dateString}").GetValueForOption(option); + var value = option.Parse($"-x {dateString}").GetValue(option); value.Should().Be(DateTime.Parse(dateString)); } @@ -480,7 +480,7 @@ public void Values_can_be_correctly_converted_to_nullable_DateTimeOffset_without var option = new Option("-x"); var dateString = "2022-02-06T09:52:54.5275055-08:00"; - var value = option.Parse($"-x {dateString}").GetValueForOption(option); + var value = option.Parse($"-x {dateString}").GetValue(option); value.Should().Be(DateTime.Parse(dateString)); } @@ -492,7 +492,7 @@ public void Values_can_be_correctly_converted_to_decimal_without_the_parser_spec var result = option.Parse("-x 123.456"); - var value = result.GetValueForOption(option); + var value = result.GetValue(option); value.Should().Be(123.456m); } @@ -502,7 +502,7 @@ public void Values_can_be_correctly_converted_to_nullable_decimal_without_the_pa { var option = new Option("-x"); - var value = option.Parse("-x 123.456").GetValueForOption(option); + var value = option.Parse("-x 123.456").GetValue(option); value.Should().Be(123.456m); } @@ -512,7 +512,7 @@ public void Values_can_be_correctly_converted_to_double_without_the_parser_speci { var option = new Option("-x"); - var value = option.Parse("-x 123.456").GetValueForOption(option); + var value = option.Parse("-x 123.456").GetValue(option); value.Should().Be(123.456d); } @@ -522,7 +522,7 @@ public void Values_can_be_correctly_converted_to_nullable_double_without_the_par { var option = new Option("-x"); - var value = option.Parse("-x 123.456").GetValueForOption(option); + var value = option.Parse("-x 123.456").GetValue(option); value.Should().Be(123.456d); } @@ -532,7 +532,7 @@ public void Values_can_be_correctly_converted_to_float_without_the_parser_specif { var option = new Option("-x"); - var value = option.Parse("-x 123.456").GetValueForOption(option); + var value = option.Parse("-x 123.456").GetValue(option); value.Should().Be(123.456f); } @@ -542,7 +542,7 @@ public void Values_can_be_correctly_converted_to_nullable_float_without_the_pars { var option = new Option("-x"); - var value = option.Parse("-x 123.456").GetValueForOption(option); + var value = option.Parse("-x 123.456").GetValue(option); value.Should().Be(123.456f); } @@ -553,7 +553,7 @@ public void Values_can_be_correctly_converted_to_Guid_without_the_parser_specify var guidString = "75517282-018F-46BB-B15F-1D8DBFE23F6E"; var option = new Option("-x"); - var value = option.Parse($"-x {guidString}").GetValueForOption(option); + var value = option.Parse($"-x {guidString}").GetValue(option); value.Should().Be(Guid.Parse(guidString)); } @@ -564,7 +564,7 @@ public void Values_can_be_correctly_converted_to_nullable_Guid_without_the_parse var guidString = "75517282-018F-46BB-B15F-1D8DBFE23F6E"; var option = new Option("-x"); - var value = option.Parse($"-x {guidString}").GetValueForOption(option); + var value = option.Parse($"-x {guidString}").GetValue(option); value.Should().Be(Guid.Parse(guidString)); } @@ -575,7 +575,7 @@ public void Values_can_be_correctly_converted_to_TimeSpan_without_the_parser_spe var timeSpanString = "30"; var option = new Option("-x"); - var value = option.Parse($"-x {timeSpanString}").GetValueForOption(option); + var value = option.Parse($"-x {timeSpanString}").GetValue(option); value.Should().Be(TimeSpan.Parse(timeSpanString)); } @@ -586,7 +586,7 @@ public void Values_can_be_correctly_converted_to_nullable_TimeSpan_without_the_p var timeSpanString = "30"; var option = new Option("-x"); - var value = option.Parse($"-x {timeSpanString}").GetValueForOption(option); + var value = option.Parse($"-x {timeSpanString}").GetValue(option); value.Should().Be(TimeSpan.Parse(timeSpanString)); } @@ -596,7 +596,7 @@ public void Values_can_be_correctly_converted_to_Uri_without_the_parser_specifyi { var option = new Option("-x"); - var value = option.Parse("-x http://example.com").GetValueForOption(option); + var value = option.Parse("-x http://example.com").GetValue(option); value.Should().BeEquivalentTo(new Uri("http://example.com")); } @@ -606,8 +606,8 @@ public void Options_with_arguments_specified_can_be_correctly_converted_to_bool_ { var option = new Option("-x"); - option.Parse("-x false").GetValueForOption(option).Should().BeFalse(); - option.Parse("-x true").GetValueForOption(option).Should().BeTrue(); + option.Parse("-x false").GetValue(option).Should().BeFalse(); + option.Parse("-x true").GetValue(option).Should().BeTrue(); } [Fact] @@ -615,7 +615,7 @@ public void Values_can_be_correctly_converted_to_long_without_the_parser_specify { var option = new Option("-x"); - var value = option.Parse("-x 123456790").GetValueForOption(option); + var value = option.Parse("-x 123456790").GetValue(option); value.Should().Be(123456790L); } @@ -625,7 +625,7 @@ public void Values_can_be_correctly_converted_to_nullable_long_without_the_parse { var option = new Option("-x"); - var value = option.Parse("-x 1234567890").GetValueForOption(option); + var value = option.Parse("-x 1234567890").GetValue(option); value.Should().Be(1234567890L); } @@ -635,7 +635,7 @@ public void Values_can_be_correctly_converted_to_short_without_the_parser_specif { var option = new Option("-s"); - var value = option.Parse("-s 1234").GetValueForOption(option); + var value = option.Parse("-s 1234").GetValue(option); value.Should().Be(1234); } @@ -645,7 +645,7 @@ public void Values_can_be_correctly_converted_to_nullable_short_without_the_pars { var option = new Option("-s"); - var value = option.Parse("-s 1234").GetValueForOption(option); + var value = option.Parse("-s 1234").GetValue(option); value.Should().Be(1234); } @@ -655,7 +655,7 @@ public void Values_can_be_correctly_converted_to_ulong_without_the_parser_specif { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 1234").GetValue(option); value.Should().Be(1234); } @@ -665,7 +665,7 @@ public void Values_can_be_correctly_converted_to_nullable_ulong_without_the_pars { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 1234").GetValue(option); value.Should().Be(1234); } @@ -675,7 +675,7 @@ public void Values_can_be_correctly_converted_to_ushort_without_the_parser_speci { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 1234").GetValue(option); value.Should().Be(1234); } @@ -685,7 +685,7 @@ public void Values_can_be_correctly_converted_to_nullable_ushort_without_the_par { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 1234").GetValue(option); value.Should().Be(1234); } @@ -695,7 +695,7 @@ public void Values_can_be_correctly_converted_to_sbyte_without_the_parser_specif { var option = new Option("-us"); - var value = option.Parse("-us 123").GetValueForOption(option); + var value = option.Parse("-us 123").GetValue(option); value.Should().Be(123); } @@ -705,7 +705,7 @@ public void Values_can_be_correctly_converted_to_nullable_sbyte_without_the_pars { var option = new Option("-x"); - var value = option.Parse("-x 123").GetValueForOption(option); + var value = option.Parse("-x 123").GetValue(option); value.Should().Be(123); } @@ -715,7 +715,7 @@ public void Values_can_be_correctly_converted_to_ipaddress_without_the_parser_sp { var option = new Option("-us"); - var value = option.Parse("-us 1.2.3.4").GetValueForOption(option); + var value = option.Parse("-us 1.2.3.4").GetValue(option); value.Should().Be(IPAddress.Parse("1.2.3.4")); } @@ -726,7 +726,7 @@ public void Values_can_be_correctly_converted_to_ipendpoint_without_the_parser_s { var option = new Option("-us"); - var value = option.Parse("-us 1.2.3.4:56").GetValueForOption(option); + var value = option.Parse("-us 1.2.3.4:56").GetValue(option); value.Should().Be(IPEndPoint.Parse("1.2.3.4:56")); } @@ -738,7 +738,7 @@ public void Values_can_be_correctly_converted_to_dateonly_without_the_parser_spe { var option = new Option("-us"); - var value = option.Parse("-us 2022-03-02").GetValueForOption(option); + var value = option.Parse("-us 2022-03-02").GetValue(option); value.Should().Be(DateOnly.Parse("2022-03-02")); } @@ -748,7 +748,7 @@ public void Values_can_be_correctly_converted_to_nullable_dateonly_without_the_p { var option = new Option("-x"); - var value = option.Parse("-x 2022-03-02").GetValueForOption(option); + var value = option.Parse("-x 2022-03-02").GetValue(option); value.Should().Be(DateOnly.Parse("2022-03-02")); } @@ -758,7 +758,7 @@ public void Values_can_be_correctly_converted_to_timeonly_without_the_parser_spe { var option = new Option("-us"); - var value = option.Parse("-us 12:34:56").GetValueForOption(option); + var value = option.Parse("-us 12:34:56").GetValue(option); value.Should().Be(TimeOnly.Parse("12:34:56")); } @@ -768,7 +768,7 @@ public void Values_can_be_correctly_converted_to_nullable_timeonly_without_the_p { var option = new Option("-x"); - var value = option.Parse("-x 12:34:56").GetValueForOption(option); + var value = option.Parse("-x 12:34:56").GetValue(option); value.Should().Be(TimeOnly.Parse("12:34:56")); } @@ -779,7 +779,7 @@ public void Values_can_be_correctly_converted_to_byte_without_the_parser_specify { var option = new Option("-us"); - var value = option.Parse("-us 123").GetValueForOption(option); + var value = option.Parse("-us 123").GetValue(option); value.Should().Be(123); } @@ -789,7 +789,7 @@ public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parse { var option = new Option("-x"); - var value = option.Parse("-x 123").GetValueForOption(option); + var value = option.Parse("-x 123").GetValue(option); value.Should().Be(123); } @@ -799,7 +799,7 @@ public void Values_can_be_correctly_converted_to_uint_without_the_parser_specify { var option = new Option("-us"); - var value = option.Parse("-us 1234").GetValueForOption(option); + var value = option.Parse("-us 1234").GetValue(option); value.Should().Be(1234); } @@ -809,7 +809,7 @@ public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parse { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 1234").GetValue(option); value.Should().Be(1234); } @@ -819,7 +819,7 @@ public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser { var option = new Option("-x"); - var value = option.Parse("-x 1 -x 2 -x 3").GetValueForOption(option); + var value = option.Parse("-x 1 -x 2 -x 3").GetValue(option); value.Should().BeEquivalentTo(1, 2, 3); } @@ -870,7 +870,7 @@ public void Values_can_be_correctly_converted_to_List_of_int_without_the_parser_ { var option = new Option>("-x"); - var value = option.Parse("-x 1 -x 2 -x 3").GetValueForOption(option); + var value = option.Parse("-x 1 -x 2 -x 3").GetValue(option); value.Should().BeEquivalentTo(1, 2, 3); } @@ -880,7 +880,7 @@ public void Values_can_be_correctly_converted_to_IEnumerable_of_int_without_the_ { var option = new Option>("-x"); - var value = option.Parse("-x 1 -x 2 -x 3").GetValueForOption(option); + var value = option.Parse("-x 1 -x 2 -x 3").GetValue(option); value.Should().BeEquivalentTo(1, 2, 3); } @@ -892,7 +892,7 @@ public void Enum_values_can_be_correctly_converted_based_on_enum_value_name_with var parseResult = option.Parse("-x Monday"); - var value = parseResult.GetValueForOption(option); + var value = parseResult.GetValue(option); value.Should().Be(DayOfWeek.Monday); } @@ -904,7 +904,7 @@ public void Nullable_enum_values_can_be_correctly_converted_based_on_enum_value_ var parseResult = option.Parse("-x Monday"); - var value = parseResult.GetValueForOption(option); + var value = parseResult.GetValue(option); value.Should().Be(DayOfWeek.Monday); } @@ -932,7 +932,7 @@ public void When_getting_a_single_value_and_specifying_a_conversion_type_that_is var result = option.Parse("-x not-an-int"); - Action getValue = () => result.GetValueForOption(option); + Action getValue = () => result.GetValue(option); getValue.Should() .Throw() @@ -949,7 +949,7 @@ public void When_getting_an_array_of_values_and_specifying_a_conversion_type_tha var result = option.Parse("-x not-an-int -x 2"); - Action getValue = () => result.GetValueForOption(option); + Action getValue = () => result.GetValue(option); getValue.Should() .Throw() @@ -969,7 +969,7 @@ public void String_defaults_to_null_when_not_specified() }; var result = command.Parse("mycommand"); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .BeNull(); } @@ -1004,7 +1004,7 @@ private void AssertParsedValueIsEmpty(Argument argument) where T : IEnumer { var result = argument.Parse(""); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .BeEmpty(); } diff --git a/src/System.CommandLine.Tests/CompletionTests.cs b/src/System.CommandLine.Tests/CompletionTests.cs index 3de7a6ea28..ddb31b8c33 100644 --- a/src/System.CommandLine.Tests/CompletionTests.cs +++ b/src/System.CommandLine.Tests/CompletionTests.cs @@ -218,7 +218,7 @@ public void Command_GetCompletions_can_access_ParseResult() new Option("--clone") .AddCompletions(ctx => { - var opt1Value = ctx.ParseResult.GetValueForOption(originOption); + var opt1Value = ctx.ParseResult.GetValue(originOption); return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty(); }) }); diff --git a/src/System.CommandLine.Tests/GlobalOptionTests.cs b/src/System.CommandLine.Tests/GlobalOptionTests.cs index 8a44bf30c7..f02a15b214 100644 --- a/src/System.CommandLine.Tests/GlobalOptionTests.cs +++ b/src/System.CommandLine.Tests/GlobalOptionTests.cs @@ -92,9 +92,9 @@ public void Subcommands_added_after_a_global_option_is_added_to_parent_will_reco root.AddCommand(child); - root.Parse("child --global 123").GetValueForOption(option).Should().Be(123); + root.Parse("child --global 123").GetValue(option).Should().Be(123); - child.Parse("--global 123").GetValueForOption(option).Should().Be(123); + child.Parse("--global 123").GetValue(option).Should().Be(123); } [Fact] @@ -114,11 +114,11 @@ public void Subcommands_with_global_option_should_propagate_option_to_children() firstChild.AddCommand(secondChild); - root.Parse("first second --global 123").GetValueForOption(option).Should().Be(123); + root.Parse("first second --global 123").GetValue(option).Should().Be(123); - firstChild.Parse("second --global 123").GetValueForOption(option).Should().Be(123); + firstChild.Parse("second --global 123").GetValue(option).Should().Be(123); - secondChild.Parse("--global 123").GetValueForOption(option).Should().Be(123); + secondChild.Parse("--global 123").GetValue(option).Should().Be(123); } } } \ No newline at end of file diff --git a/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs b/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs index 1eb8242b63..881975de3f 100644 --- a/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs +++ b/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs @@ -114,7 +114,7 @@ public void When_max_arity_is_1_then_subsequent_option_args_overwrite_previous_o var result = command.Parse(commandLine); - var value = result.GetValueForOption(option); + var value = result.GetValue(option); value.Should().Be("2"); } @@ -162,8 +162,8 @@ public void Multiple_option_arguments_that_match_single_arity_option_aliases_are _output.WriteLine(result.Diagram()); result.Errors.Should().BeEmpty(); - result.GetValueForOption(optionY).Should().Be("-x"); - result.GetValueForOption(optionX).Should().Be("-y"); + result.GetValue(optionY).Should().Be("-x"); + result.GetValue(optionX).Should().Be("-y"); } } @@ -177,7 +177,7 @@ public void Single_option_arg_is_matched() var result = command.Parse("--option 1 2"); - var value = result.GetValueForOption(option); + var value = result.GetValue(option); value.Should().BeEquivalentTo(new[] { "1" }); } @@ -202,7 +202,7 @@ public void When_max_arity_is_greater_than_1_then_multiple_option_args_are_match var result = command.Parse("--option 1 --option 2"); - var value = result.GetValueForOption(option); + var value = result.GetValue(option); value.Should().BeEquivalentTo(new[] { "1", "2" }); result.Errors.Should().BeEmpty(); diff --git a/src/System.CommandLine.Tests/OptionTests.cs b/src/System.CommandLine.Tests/OptionTests.cs index dc3ac47b23..1812bffe71 100644 --- a/src/System.CommandLine.Tests/OptionTests.cs +++ b/src/System.CommandLine.Tests/OptionTests.cs @@ -218,9 +218,9 @@ public void When_options_use_different_prefixes_they_still_work(string prefix) var result = rootCommand.Parse(prefix + "c value-for-c " + prefix + "a value-for-a"); - result.GetValueForOption(optionA).Should().Be("value-for-a"); + result.GetValue(optionA).Should().Be("value-for-a"); result.HasOption(optionB).Should().BeFalse(); - result.GetValueForOption(optionC).Should().Be("value-for-c"); + result.GetValue(optionC).Should().Be("value-for-c"); } [Fact] @@ -326,7 +326,7 @@ public void Option_of_string_defaults_to_null_when_not_specified() result.HasOption(option) .Should() .BeFalse(); - result.GetValueForOption(option) + result.GetValue(option) .Should() .BeNull(); } @@ -353,7 +353,7 @@ public void When_aliases_overlap_the_longer_alias_is_chosen(string parseInput) var parseResult = option.Parse(parseInput); - parseResult.GetValueForOption(option).Should().Be("value"); + parseResult.GetValue(option).Should().Be("value"); } [Fact] @@ -366,7 +366,7 @@ public void Option_of_boolean_defaults_to_false_when_not_specified() result.HasOption(option) .Should() .BeFalse(); - result.GetValueForOption(option) + result.GetValue(option) .Should() .BeFalse(); } diff --git a/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs b/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs index d52bfef122..4ac3c6b415 100644 --- a/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs +++ b/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs @@ -29,9 +29,9 @@ public void Subsequent_tokens_are_parsed_as_arguments_even_if_they_match_option_ result.HasOption(option).Should().BeTrue(); - result.GetValueForOption(option).Should().BeEquivalentTo("some stuff"); + result.GetValue(option).Should().BeEquivalentTo("some stuff"); - result.GetValueForArgument(argument).Should().BeEquivalentSequenceTo("-o", "--one", "-x", "-y", "-z", "-o:foo"); + result.GetValue(argument).Should().BeEquivalentSequenceTo("-o", "--one", "-x", "-y", "-z", "-o:foo"); result.UnmatchedTokens.Should().BeEmpty(); } @@ -85,7 +85,7 @@ public void A_second_double_dash_is_parsed_as_an_argument() .Build() .Parse("a b c -- -- d"); - var strings = result.GetValueForArgument(argument); + var strings = result.GetValue(argument); strings.Should().BeEquivalentSequenceTo("a", "b", "c", "--", "d"); } diff --git a/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs b/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs index 261e2d1d26..db83bb6ffd 100644 --- a/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs +++ b/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs @@ -38,10 +38,10 @@ public void Multiple_arguments_can_differ_by_arity() var result = command.Parse("1 2 3 4"); - result.GetValueForArgument(multipleArityArg) + result.GetValue(multipleArityArg) .Should() .BeEquivalentSequenceTo("1", "2", "3"); - result.GetValueForArgument(singleArityArg) + result.GetValue(singleArityArg) .Should() .BeEquivalentSequenceTo("4"); } @@ -66,8 +66,8 @@ public void Multiple_arguments_can_differ_by_type() var result = command.Parse("1 2"); - result.GetValueForArgument(stringArg).Should().Be("1"); - result.GetValueForArgument(intArg).Should().Be(2); + result.GetValue(stringArg).Should().Be("1"); + result.GetValue(intArg).Should().Be(2); } [Theory] @@ -101,22 +101,22 @@ public void When_multiple_arguments_are_present_then_their_order_relative_to_sib var parseResult = command.Parse(commandLine); parseResult - .GetValueForArgument(first) + .GetValue(first) .Should() .Be("one"); parseResult - .GetValueForArgument(second) + .GetValue(second) .Should() .Be("two"); parseResult - .GetValueForArgument(third) + .GetValue(third) .Should() .BeEquivalentSequenceTo("three", "four", "five"); parseResult - .GetValueForOption(verbose) + .GetValue(verbose) .Should() .BeTrue(); } @@ -159,7 +159,7 @@ public void When_multiple_arguments_are_defined_but_not_provided_then_option_par var result = command.Parse("-e foo"); - var optionResult = result.GetValueForOption(option); + var optionResult = result.GetValue(option); optionResult.Should().Be("foo"); } @@ -180,12 +180,12 @@ public void Tokens_that_cannot_be_converted_by_multiple_arity_argument_flow_to_n var _ = new AssertionScope(); - result.GetValueForArgument(ints) + result.GetValue(ints) .Should() .BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering()); - result.GetValueForArgument(strings) + result.GetValue(strings) .Should() .BeEquivalentTo(new[] { "one", "two" }, options => options.WithStrictOrdering()); @@ -207,12 +207,12 @@ public void Tokens_that_cannot_be_converted_by_multiple_arity_argument_flow_to_n var _ = new AssertionScope(); - result.GetValueForArgument(ints) + result.GetValue(ints) .Should() .BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering()); - result.GetValueForArgument(strings) + result.GetValue(strings) .Should() .Be("four"); @@ -244,8 +244,8 @@ public void Unsatisfied_subsequent_argument_with_min_arity_0_parses_as_default_v var result = rootCommand.Parse("value-1"); - result.GetValueForArgument(arg1).Should().Be("value-1"); - result.GetValueForArgument(arg2).Should().Be("the-default"); + result.GetValue(arg1).Should().Be("value-1"); + result.GetValue(arg2).Should().Be("the-default"); } [Fact] // https://github.com/dotnet/command-line-api/issues/1403 @@ -263,7 +263,7 @@ public void Unsatisfied_subsequent_argument_with_min_arity_1_parses_as_default_v var result = rootCommand.Parse(""); result.FindResultFor(arg1).Should().BeNull(); - result.GetValueForArgument(arg2).Should().Be("the-default"); + result.GetValue(arg2).Should().Be("the-default"); } [Fact] // https://github.com/dotnet/command-line-api/issues/1395 @@ -283,7 +283,7 @@ public void When_subsequent_argument_with_ZeroOrOne_arity_is_not_provided_then_p result.Errors.Should().BeEmpty(); - result.GetValueForArgument(argument1).Should().Be("one"); + result.GetValue(argument1).Should().Be("one"); } [Theory] // https://github.com/dotnet/command-line-api/issues/1711 diff --git a/src/System.CommandLine.Tests/ParserTests.cs b/src/System.CommandLine.Tests/ParserTests.cs index 51a955364e..d91bf3ab97 100644 --- a/src/System.CommandLine.Tests/ParserTests.cs +++ b/src/System.CommandLine.Tests/ParserTests.cs @@ -824,7 +824,7 @@ public void Commands_can_have_default_argument_values() ParseResult result = command.Parse("command"); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .Be("default"); } @@ -839,7 +839,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_c ParseResult result = command.Parse("command"); result.HasOption(option).Should().BeTrue(); - result.GetValueForOption(option).Should().Be("the-default"); + result.GetValue(option).Should().Be("the-default"); } [Fact] @@ -914,7 +914,7 @@ public void Command_default_argument_value_does_not_override_parsed_value() var result = command.Parse("the-directory"); - result.GetValueForArgument(argument) + result.GetValue(argument) ?.Name .Should() .Be("the-directory"); @@ -1092,7 +1092,7 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_op var result = command.Parse(input); - var valueForOption = result.GetValueForOption(optionX); + var valueForOption = result.GetValue(optionX); valueForOption.Should().Be("-y"); } @@ -1113,9 +1113,9 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_bu var result = command.Parse("-a -bc"); - result.GetValueForOption(optionA).Should().Be("-bc"); - result.GetValueForOption(optionB).Should().BeFalse(); - result.GetValueForOption(optionC).Should().BeFalse(); + result.GetValue(optionA).Should().Be("-bc"); + result.GetValue(optionB).Should().BeFalse(); + result.GetValue(optionC).Should().BeFalse(); } [Fact] @@ -1132,7 +1132,7 @@ public void Option_arguments_can_match_subcommands() _output.WriteLine(result.ToString()); - result.GetValueForOption(optionA).Should().Be("subcommand"); + result.GetValue(optionA).Should().Be("subcommand"); result.CommandResult.Command.Should().BeSameAs(root); } @@ -1153,7 +1153,7 @@ public void Arguments_can_match_subcommands() result.CommandResult.Command.Should().BeSameAs(subcommand); - result.GetValueForArgument(argument) + result.GetValue(argument) .Should() .BeEquivalentSequenceTo("one", "two", "three", "subcommand", "four"); } @@ -1173,7 +1173,7 @@ public void Option_arguments_can_match_the_aliases_of_sibling_options_when_non_s var result = command.Parse(input); - var valueForOption = result.GetValueForOption(optionX); + var valueForOption = result.GetValue(optionX); result.Errors.Should().BeEmpty(); valueForOption.Should().Be("-y"); @@ -1191,7 +1191,7 @@ public void Single_option_arguments_that_match_option_aliases_are_parsed_correct var result = command.Parse("-x -x"); - result.GetValueForOption(optionX).Should().Be("-x"); + result.GetValue(optionX).Should().Be("-x"); } [Theory] @@ -1218,8 +1218,8 @@ public void Boolean_options_are_not_greedy(string commandLine) result.Errors.Should().BeEmpty(); - result.GetValueForOption(optX).Should().BeTrue(); - result.GetValueForOption(optY).Should().BeTrue(); + result.GetValue(optX).Should().BeTrue(); + result.GetValue(optY).Should().BeTrue(); } [Fact] @@ -1238,8 +1238,8 @@ public void Multiple_option_arguments_that_match_multiple_arity_option_aliases_a _output.WriteLine(result.Diagram()); - result.GetValueForOption(optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" }); - result.GetValueForOption(optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" }); + result.GetValue(optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" }); + result.GetValue(optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" }); } [Fact] @@ -1256,7 +1256,7 @@ public void Bundled_option_arguments_that_match_option_aliases_are_parsed_correc var result = command.Parse("-yxx"); - result.GetValueForOption(optionX).Should().Be("x"); + result.GetValue(optionX).Should().Be("x"); } [Fact] @@ -1273,8 +1273,8 @@ public void Argument_name_is_not_matched_as_a_token() var result = command.Parse("name one two three"); - result.GetValueForArgument(nameArg).Should().Be("name"); - result.GetValueForArgument(columnsArg).Should().BeEquivalentTo("one", "two", "three"); + result.GetValue(nameArg).Should().Be("name"); + result.GetValue(columnsArg).Should().BeEquivalentTo("one", "two", "three"); } [Fact] @@ -1299,7 +1299,7 @@ public void Boolean_options_with_no_argument_specified_do_not_match_subsequent_a var result = command.Parse("-v an-argument"); - result.GetValueForOption(option).Should().BeTrue(); + result.GetValue(option).Should().BeTrue(); } [Fact] @@ -1316,8 +1316,8 @@ public void When_a_command_line_has_unmatched_tokens_they_are_not_applied_to_sub var result = command.Parse("-x 23 unmatched-token -y 42"); - result.GetValueForOption(optionX).Should().Be("23"); - result.GetValueForOption(optionY).Should().Be("42"); + result.GetValue(optionX).Should().Be("23"); + result.GetValue(optionY).Should().Be("42"); result.UnmatchedTokens.Should().BeEquivalentTo("unmatched-token"); } @@ -1564,7 +1564,7 @@ public void Parsed_value_of_empty_string_arg_is_an_empty_string(string arg1, str var result = rootCommand.Parse(new[] { arg1, arg2 }); - result.GetValueForOption(option).Should().BeEmpty(); + result.GetValue(option).Should().BeEmpty(); } } } diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs index e252bb5b3b..1416cb282e 100644 --- a/src/System.CommandLine.Tests/ParsingValidationTests.cs +++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs @@ -483,7 +483,7 @@ public void The_parsed_value_of_an_argument_is_available_within_a_validator() var errorMessage = "The value of option '-x' must be between 1 and 100."; argument.AddValidator(result => { - var value = result.GetValueForArgument(argument); + var value = result.GetValue(argument); if (value < 0 || value > 100) { @@ -507,7 +507,7 @@ public void The_parsed_value_of_an_option_is_available_within_a_validator() var errorMessage = "The value of option '-x' must be between 1 and 100."; option.AddValidator(result => { - var value = result.GetValueForOption(option); + var value = result.GetValue(option); if (value < 0 || value > 100) { @@ -1094,8 +1094,8 @@ public void When_an_option_has_a_default_value_then_the_default_should_apply_if_ var result = parser.Parse(""); result.Errors.Should().BeEmpty(); - result.GetValueForOption(optionX).Should().Be(123); - result.GetValueForOption(optionY).Should().Be(456); + result.GetValue(optionX).Should().Be(123); + result.GetValue(optionY).Should().Be(456); } [Fact] // https://github.com/dotnet/command-line-api/issues/1505 diff --git a/src/System.CommandLine.Tests/ResponseFileTests.cs b/src/System.CommandLine.Tests/ResponseFileTests.cs index 40b1197385..d50eda4c20 100644 --- a/src/System.CommandLine.Tests/ResponseFileTests.cs +++ b/src/System.CommandLine.Tests/ResponseFileTests.cs @@ -69,7 +69,7 @@ public void When_response_file_is_specified_it_loads_options_with_arguments_from .Parse($"@{responseFile}"); result.HasOption(optionOne).Should().BeTrue(); - result.GetValueForOption(optionTwo).Should().Be(123); + result.GetValue(optionTwo).Should().Be(123); result.Errors.Should().BeEmpty(); } @@ -179,7 +179,7 @@ public void Response_file_can_contain_blank_lines() } .Parse($"@{responseFile}"); - result.GetValueForOption(option).Should().Be(123); + result.GetValue(option).Should().Be(123); result.Errors.Should().BeEmpty(); } @@ -292,8 +292,8 @@ public void When_response_file_parse_as_space_separated_returns_expected_values( var result = parser.Parse($"@{responseFile}"); - result.GetValueForOption(optionOne).Should().Be("first value"); - result.GetValueForOption(optionTwo).Should().Be(123); + result.GetValue(optionOne).Should().Be("first value"); + result.GetValue(optionTwo).Should().Be(123); } [Fact] @@ -338,10 +338,10 @@ public void Response_files_can_refer_to_other_response_files() var result = command.Parse($"@{file1}"); - result.GetValueForOption(option1).Should().Be(1); - result.GetValueForOption(option1).Should().Be(1); - result.GetValueForOption(option2).Should().Be(2); - result.GetValueForOption(option3).Should().Be(3); + result.GetValue(option1).Should().Be(1); + result.GetValue(option1).Should().Be(1); + result.GetValue(option2).Should().Be(2); + result.GetValue(option3).Should().Be(3); result.Errors.Should().BeEmpty(); } @@ -354,8 +354,8 @@ public void When_response_file_options_or_arguments_contain_trailing_spaces_they var option2 = new Option("--option2"); var result = new RootCommand { option1, option2 }.Parse($"@{responseFile}"); - result.GetValueForOption(option1).Should().Be("value1"); - result.GetValueForOption(option2).Should().Be(2); + result.GetValue(option1).Should().Be("value1"); + result.GetValue(option2).Should().Be(2); } [Fact] @@ -367,8 +367,8 @@ public void When_response_file_options_or_arguments_contain_leading_spaces_they_ var option2 = new Option("--option2"); var result = new RootCommand { option1, option2 }.Parse($"@{responseFile}"); - result.GetValueForOption(option1).Should().Be("value1"); - result.GetValueForOption(option2).Should().Be(2); + result.GetValue(option1).Should().Be("value1"); + result.GetValue(option2).Should().Be(2); result.Errors.Should().BeEmpty(); } @@ -381,8 +381,8 @@ public void When_response_file_options_or_arguments_contain_trailing_and_leading var option2 = new Option("--option2"); var result = new RootCommand { option1, option2 }.Parse($"@{responseFile}"); - result.GetValueForOption(option1).Should().Be("value1"); - result.GetValueForOption(option2).Should().Be(2); + result.GetValue(option1).Should().Be("value1"); + result.GetValue(option2).Should().Be(2); result.Errors.Should().BeEmpty(); } } diff --git a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs index 90dd41b694..c7de987072 100644 --- a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs @@ -22,8 +22,8 @@ private static int Main(string[] args) void Run(InvocationContext context) { - context.Console.WriteLine($"Bool option: {context.ParseResult.GetValueForOption(boolOption)}"); - context.Console.WriteLine($"String option: {context.ParseResult.GetValueForOption(stringOption)}"); + context.Console.WriteLine($"Bool option: {context.ParseResult.GetValue(boolOption)}"); + context.Console.WriteLine($"String option: {context.ParseResult.GetValue(stringOption)}"); } } } \ No newline at end of file diff --git a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs index f81e9d0ec7..40f6f6e3b3 100644 --- a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs @@ -10,7 +10,7 @@ command.SetHandler(context => { - context.Console.Write($"The file you chose was: {context.ParseResult.GetValueForArgument(fileArgument)}"); + context.Console.Write($"The file you chose was: {context.ParseResult.GetValue(fileArgument)}"); }); command.Invoke(args); diff --git a/src/System.CommandLine.Tests/TokenReplacementTests.cs b/src/System.CommandLine.Tests/TokenReplacementTests.cs index d768754317..d615b70761 100644 --- a/src/System.CommandLine.Tests/TokenReplacementTests.cs +++ b/src/System.CommandLine.Tests/TokenReplacementTests.cs @@ -54,7 +54,7 @@ public void Token_replacer_can_expand_argument_values() result.Errors.Should().BeEmpty(); - result.GetValueForArgument(argument).Should().Be(123); + result.GetValue(argument).Should().Be(123); } [Fact] @@ -77,7 +77,7 @@ public void Custom_token_replacer_can_expand_option_argument_values() result.Errors.Should().BeEmpty(); - result.GetValueForOption(option).Should().Be(123); + result.GetValue(option).Should().Be(123); } [Fact] @@ -100,7 +100,7 @@ public void Custom_token_replacer_can_expand_subcommands_and_options_and_argumen result.Errors.Should().BeEmpty(); - result.GetValueForOption(option).Should().Be(123); + result.GetValue(option).Should().Be(123); } [Fact] @@ -121,7 +121,7 @@ public void Expanded_tokens_containing_whitespace_are_parsed_as_single_tokens() var result = parser.Parse("@replace-me"); - result.GetValueForArgument(argument).Should().Be("one two three"); + result.GetValue(argument).Should().Be("one two three"); } [Fact] @@ -167,7 +167,7 @@ public void When_token_replacer_returns_false_without_setting_an_error_message_t result.Errors.Should().BeEmpty(); - result.GetValueForArgument(argument).Should().Be("@replace-me"); + result.GetValue(argument).Should().Be("@replace-me"); } [Fact] @@ -190,7 +190,7 @@ public void Token_replacer_will_delete_token_when_delegate_returns_true_and_sets result.Errors.Should().BeEmpty(); - result.GetValueForArgument(argument).Should().BeEmpty(); + result.GetValue(argument).Should().BeEmpty(); } [Fact] @@ -213,6 +213,6 @@ public void Token_replacer_will_delete_token_when_delegate_returns_true_and_sets result.Errors.Should().BeEmpty(); - result.GetValueForArgument(argument).Should().BeEmpty(); + result.GetValue(argument).Should().BeEmpty(); } } \ No newline at end of file diff --git a/src/System.CommandLine/Invocation/InvocationContext.cs b/src/System.CommandLine/Invocation/InvocationContext.cs index 07e6e024f3..daad518e18 100644 --- a/src/System.CommandLine/Invocation/InvocationContext.cs +++ b/src/System.CommandLine/Invocation/InvocationContext.cs @@ -123,6 +123,22 @@ public void LinkToken(CancellationToken token) _registrations.AddLast(token.Register(Cancel)); } + /// + public object? GetValue(Option option) => + ParseResult.GetValue(option); + + /// + public T? GetValue(Option option) + => ParseResult.GetValue(option); + + /// + public object? GetValue(Argument argument) => + ParseResult.GetValue(argument); + + /// + public T GetValue(Argument argument) + => ParseResult.GetValue(argument); + /// void IDisposable.Dispose() { diff --git a/src/System.CommandLine/ParseResult.cs b/src/System.CommandLine/ParseResult.cs index b84acded9f..1a9eba1aef 100644 --- a/src/System.CommandLine/ParseResult.cs +++ b/src/System.CommandLine/ParseResult.cs @@ -129,8 +129,8 @@ CommandLineText is null internal T? GetValueFor(IValueDescriptor symbol) => symbol switch { - Argument argument => GetValueForArgument(argument), - Option option => GetValueForOption(option), + Argument argument => GetValue(argument), + Option option => GetValue(option), _ => throw new ArgumentOutOfRangeException() }; @@ -139,24 +139,24 @@ CommandLineText is null /// /// The option for which to get a value. /// The parsed value or a configured default. - public object? GetValueForOption(Option option) => - RootCommandResult.GetValueForOption(option); + public object? GetValue(Option option) => + RootCommandResult.GetValue(option); /// /// Gets the parsed or default value for the specified argument. /// /// The argument for which to get a value. /// The parsed value or a configured default. - public object? GetValueForArgument(Argument argument) => - RootCommandResult.GetValueForArgument(argument); + public object? GetValue(Argument argument) => + RootCommandResult.GetValue(argument); - /// - public T GetValueForArgument(Argument argument) - => RootCommandResult.GetValueForArgument(argument); + /// + public T GetValue(Argument argument) + => RootCommandResult.GetValue(argument); - /// - public T? GetValueForOption(Option option) - => RootCommandResult.GetValueForOption(option); + /// + public T? GetValue(Option option) + => RootCommandResult.GetValue(option); /// public override string ToString() => $"{nameof(ParseResult)}: {this.Diagram()}"; diff --git a/src/System.CommandLine/Parsing/SymbolResult.cs b/src/System.CommandLine/Parsing/SymbolResult.cs index 8cc1f0df01..ff72eaf48b 100644 --- a/src/System.CommandLine/Parsing/SymbolResult.cs +++ b/src/System.CommandLine/Parsing/SymbolResult.cs @@ -128,8 +128,8 @@ public LocalizationResources LocalizationResources public virtual OptionResult? FindResultFor(Option option) => Root?.FindResultFor(option); - /// - public T GetValueForArgument(Argument argument) + /// + public T GetValue(Argument argument) { if (FindResultFor(argument) is { } result && result.GetValueOrDefault() is { } t) @@ -140,8 +140,8 @@ public T GetValueForArgument(Argument argument) return (T)ArgumentConverter.GetDefaultValue(argument.ValueType)!; } - /// - public object? GetValueForArgument(Argument argument) + /// + public object? GetValue(Argument argument) { if (FindResultFor(argument) is { } result && result.GetValueOrDefault() is { } t) @@ -152,8 +152,8 @@ public T GetValueForArgument(Argument argument) return ArgumentConverter.GetDefaultValue(argument.ValueType); } - /// - public T? GetValueForOption(Option option) + /// + public T? GetValue(Option option) { if (FindResultFor(option) is { } result && result.GetValueOrDefault() is { } t) @@ -164,8 +164,8 @@ public T GetValueForArgument(Argument argument) return (T)ArgumentConverter.GetDefaultValue(option.Argument.ValueType)!; } - /// - public object? GetValueForOption(Option option) + /// + public object? GetValue(Option option) { if (FindResultFor(option) is { } result && result.GetValueOrDefault() is { } t)