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 1c02e0e378..1305e4a18f 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 @@ -136,15 +136,15 @@ System.CommandLine public CliConfiguration Configuration { get; } public System.Collections.Generic.IReadOnlyList Errors { get; } public System.CommandLine.Parsing.CommandResult RootCommandResult { get; } - public System.Collections.Generic.IReadOnlyList Tokens { get; } - public System.String[] UnmatchedTokens { get; } - public System.CommandLine.Parsing.ArgumentResult FindResultFor(CliArgument argument) - public System.CommandLine.Parsing.CommandResult FindResultFor(CliCommand command) - public System.CommandLine.Parsing.OptionResult FindResultFor(CliOption option) - public System.CommandLine.Parsing.DirectiveResult FindResultFor(CliDirective directive) - public System.CommandLine.Parsing.SymbolResult FindResultFor(CliSymbol symbol) + public System.Collections.Generic.IReadOnlyList Tokens { get; } + public System.Collections.Generic.IReadOnlyList UnmatchedTokens { get; } public System.CommandLine.Completions.CompletionContext GetCompletionContext() public System.Collections.Generic.IEnumerable GetCompletions(System.Nullable position = null) + public System.CommandLine.Parsing.ArgumentResult GetResult(CliArgument argument) + public System.CommandLine.Parsing.CommandResult GetResult(CliCommand command) + public System.CommandLine.Parsing.OptionResult GetResult(CliOption option) + public System.CommandLine.Parsing.DirectiveResult GetResult(CliDirective directive) + public System.CommandLine.Parsing.SymbolResult GetResult(CliSymbol symbol) public T GetValue(CliArgument argument) public T GetValue(CliOption option) public T GetValue(System.String name) @@ -236,17 +236,33 @@ System.CommandLine.Parsing public static System.CommandLine.ParseResult Parse(System.CommandLine.CliCommand command, System.Collections.Generic.IReadOnlyList args, System.CommandLine.CliConfiguration configuration = null) public static System.CommandLine.ParseResult Parse(System.CommandLine.CliCommand command, System.String commandLine, System.CommandLine.CliConfiguration configuration = null) public static System.Collections.Generic.IEnumerable SplitCommandLine(System.String commandLine) + public class CliToken, System.IEquatable + public static System.Boolean op_Equality(CliToken left, CliToken right) + public static System.Boolean op_Inequality(CliToken left, CliToken right) + .ctor(System.String value, CliTokenType type, System.CommandLine.CliSymbol symbol) + public CliTokenType Type { get; } + public System.String Value { get; } + public System.Boolean Equals(System.Object obj) + public System.Boolean Equals(CliToken other) + public System.Int32 GetHashCode() + public System.String ToString() + public enum CliTokenType : System.Enum, System.IComparable, System.IConvertible, System.IFormattable + Argument=0 + Command=1 + Option=2 + DoubleDash=3 + Directive=4 public class CommandResult : SymbolResult public System.Collections.Generic.IEnumerable Children { get; } public System.CommandLine.CliCommand Command { get; } - public Token IdentifierToken { get; } + public CliToken IdentifierToken { get; } public System.String ToString() public class DirectiveResult : SymbolResult public System.CommandLine.CliDirective Directive { get; } - public Token Token { get; } + public CliToken Token { get; } public System.Collections.Generic.IReadOnlyList Values { get; } public class OptionResult : SymbolResult - public Token IdentifierToken { get; } + public CliToken IdentifierToken { get; } public System.Int32 IdentifierTokenCount { get; } public System.Boolean Implicit { get; } public System.CommandLine.CliOption Option { get; } @@ -259,30 +275,14 @@ System.CommandLine.Parsing public abstract class SymbolResult public System.Collections.Generic.IEnumerable Errors { get; } public SymbolResult Parent { get; } - public System.Collections.Generic.IReadOnlyList Tokens { get; } + public System.Collections.Generic.IReadOnlyList Tokens { get; } public System.Void AddError(System.String errorMessage) - public ArgumentResult FindResultFor(System.CommandLine.CliArgument argument) - public CommandResult FindResultFor(System.CommandLine.CliCommand command) - public OptionResult FindResultFor(System.CommandLine.CliOption option) - public DirectiveResult FindResultFor(System.CommandLine.CliDirective directive) + public ArgumentResult GetResult(System.CommandLine.CliArgument argument) + public CommandResult GetResult(System.CommandLine.CliCommand command) + public OptionResult GetResult(System.CommandLine.CliOption option) + public DirectiveResult GetResult(System.CommandLine.CliDirective directive) public T GetValue(CliArgument argument) public T GetValue(CliOption option) - public class Token, System.IEquatable - public static System.Boolean op_Equality(Token left, Token right) - public static System.Boolean op_Inequality(Token left, Token right) - .ctor(System.String value, TokenType type, System.CommandLine.CliSymbol symbol) - public TokenType Type { get; } - public System.String Value { get; } - public System.Boolean Equals(System.Object obj) - public System.Boolean Equals(Token other) - public System.Int32 GetHashCode() - public System.String ToString() - public enum TokenType : System.Enum, System.IComparable, System.IConvertible, System.IFormattable - Argument=0 - Command=1 - Option=2 - DoubleDash=3 - Directive=4 public delegate TryReplaceToken : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable .ctor(System.Object object, System.IntPtr method) public System.IAsyncResult BeginInvoke(System.String tokenToReplace, ref System.Collections.Generic.IReadOnlyList replacementTokens, ref System.String& errorMessage, System.AsyncCallback callback, System.Object object) diff --git a/src/System.CommandLine.Hosting/DirectiveConfigurationExtensions.cs b/src/System.CommandLine.Hosting/DirectiveConfigurationExtensions.cs index eb9f717420..b953f4f6e8 100644 --- a/src/System.CommandLine.Hosting/DirectiveConfigurationExtensions.cs +++ b/src/System.CommandLine.Hosting/DirectiveConfigurationExtensions.cs @@ -17,7 +17,7 @@ public static IConfigurationBuilder AddCommandLineDirectives( if (directive is null) throw new ArgumentNullException(nameof(directive)); - if (commandline.FindResultFor(directive) is not DirectiveResult result + if (commandline.GetResult(directive) is not DirectiveResult result || result.Values.Count == 0) { return config; diff --git a/src/System.CommandLine.Hosting/HostingAction.cs b/src/System.CommandLine.Hosting/HostingAction.cs index 489cf8c3b4..8f582336e6 100644 --- a/src/System.CommandLine.Hosting/HostingAction.cs +++ b/src/System.CommandLine.Hosting/HostingAction.cs @@ -41,7 +41,7 @@ public override BindingContext GetBindingContext(ParseResult parseResult) public async override Task InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken = default) { var argsRemaining = parseResult.UnmatchedTokens; - var hostBuilder = _hostBuilderFactory?.Invoke(argsRemaining) + var hostBuilder = _hostBuilderFactory?.Invoke(argsRemaining.ToArray()) ?? new HostBuilder(); hostBuilder.Properties[typeof(ParseResult)] = parseResult; diff --git a/src/System.CommandLine.NamingConventionBinder/BindingContext.cs b/src/System.CommandLine.NamingConventionBinder/BindingContext.cs index 660e6b9fec..b7ee661fb4 100644 --- a/src/System.CommandLine.NamingConventionBinder/BindingContext.cs +++ b/src/System.CommandLine.NamingConventionBinder/BindingContext.cs @@ -89,7 +89,7 @@ internal bool TryBindToScalarValue( else { ArgumentResult argumentResult = valueDescriptor is CliArgument argument - ? parseResult.FindResultFor(argument) is ArgumentResult found + ? parseResult.GetResult(argument) is ArgumentResult found ? found : new ArgumentResult(argument, parseResult.RootCommandResult.SymbolResultTree, null) : new ArgumentResult(new CliArgument(valueDescriptor.ValueName), parseResult.RootCommandResult.SymbolResultTree, null); diff --git a/src/System.CommandLine.NamingConventionBinder/CommandResultExtensions.cs b/src/System.CommandLine.NamingConventionBinder/CommandResultExtensions.cs index 08fe59741f..f20269245e 100644 --- a/src/System.CommandLine.NamingConventionBinder/CommandResultExtensions.cs +++ b/src/System.CommandLine.NamingConventionBinder/CommandResultExtensions.cs @@ -20,7 +20,7 @@ internal static bool TryGetValueForArgument( if (valueDescriptor.ValueName.IsMatch(RemovePrefix(argument.Name))) { - if (commandResult.FindResultFor(argument) is { } argumentResult) + if (commandResult.GetResult(argument) is { } argumentResult) { value = argumentResult.GetValueOrDefault(); } @@ -53,7 +53,7 @@ internal static bool TryGetValueForOption( if (hasMatchingAlias) { - var optionResult = commandResult.FindResultFor(option); + var optionResult = commandResult.GetResult(option); if (optionResult is not null) { diff --git a/src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs b/src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs index 11c12c296e..c038c07087 100644 --- a/src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs +++ b/src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs @@ -21,7 +21,7 @@ public bool TryGetValue(IValueDescriptor valueDescriptor, switch (_symbol) { case CliOption option: - var optionResult = bindingContext?.ParseResult.FindResultFor(option); + var optionResult = bindingContext?.ParseResult.GetResult(option); if (optionResult is not null) { boundValue = optionResult.GetValueOrDefault(); @@ -29,7 +29,7 @@ public bool TryGetValue(IValueDescriptor valueDescriptor, } break; case CliArgument argument: - var argumentResult = bindingContext?.ParseResult.FindResultFor(argument); + var argumentResult = bindingContext?.ParseResult.GetResult(argument); if (argumentResult is not null) { boundValue = argumentResult.GetValueOrDefault(); diff --git a/src/System.CommandLine.Tests/ArgumentTests.cs b/src/System.CommandLine.Tests/ArgumentTests.cs index e71c68d737..666b36ae01 100644 --- a/src/System.CommandLine.Tests/ArgumentTests.cs +++ b/src/System.CommandLine.Tests/ArgumentTests.cs @@ -312,7 +312,7 @@ public void Symbol_can_be_found_without_explicitly_traversing_result_tree(string { CustomParser = argResult => { - resultForOptionX = argResult.FindResultFor(optionX); + resultForOptionX = argResult.GetResult(optionX); return string.Empty; } }; @@ -461,7 +461,7 @@ public void Custom_parser_can_check_another_option_result_for_custom_errors(stri { CustomParser = result => { - if (result.FindResultFor(optionWithError) is { } optionWithErrorResult) + if (result.GetResult(optionWithError) is { } optionWithErrorResult) { var otherOptionError = optionWithErrorResult.Errors.SingleOrDefault()?.Message; @@ -654,13 +654,13 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine) var parseResult = command.Parse(commandLine); - parseResult.FindResultFor(argument1) + parseResult.GetResult(argument1) .GetValueOrDefault() .Should() .BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering()); - parseResult.FindResultFor(argument2) + parseResult.GetResult(argument2) .GetValueOrDefault() .Should() .BeEquivalentTo(new[] { 4, 5, 6, 7, 8 }, @@ -751,14 +751,14 @@ public void When_custom_parser_passes_on_tokens_the_argument_result_tokens_refle var parseResult = command.Parse("1 2 3 4 5 6 7 8"); - parseResult.FindResultFor(argument1) + parseResult.GetResult(argument1) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo(new[] { "1", "2", "3" }, options => options.WithStrictOrdering()); - parseResult.FindResultFor(argument2) + parseResult.GetResult(argument2) .Tokens .Select(t => t.Value) .Should() diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index e606dcfc71..b06f2ce33a 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -688,7 +688,7 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types( var result = command.Parse("--items one --items two --items three"); result.Errors.Should().BeEmpty(); - result.FindResultFor(option).GetValueOrDefault().Should().BeAssignableTo(argumentType); + result.GetResult(option).GetValueOrDefault().Should().BeAssignableTo(argumentType); } [Fact] diff --git a/src/System.CommandLine.Tests/DirectiveTests.cs b/src/System.CommandLine.Tests/DirectiveTests.cs index 58398be4a7..ed25bd359e 100644 --- a/src/System.CommandLine.Tests/DirectiveTests.cs +++ b/src/System.CommandLine.Tests/DirectiveTests.cs @@ -29,7 +29,7 @@ public void Raw_tokens_still_hold_directives() ParseResult result = Parse(new CliOption("-y"), directive, "[parse] -y"); - result.FindResultFor(directive).Should().NotBeNull(); + result.GetResult(directive).Should().NotBeNull(); result.Tokens.Should().Contain(t => t.Value == "[parse]"); } @@ -40,7 +40,7 @@ public void Directives_must_precede_other_symbols() ParseResult result = Parse(new CliOption("-y"), directive, "-y [parse]"); - result.FindResultFor(directive).Should().BeNull(); + result.GetResult(directive).Should().BeNull(); } [Fact] @@ -55,8 +55,8 @@ public void Multiple_directives_are_allowed() var result = root.Parse("[parse] [suggest] -y", config); - result.FindResultFor(parseDirective).Should().NotBeNull(); - result.FindResultFor(suggestDirective).Should().NotBeNull(); + result.GetResult(parseDirective).Should().NotBeNull(); + result.GetResult(suggestDirective).Should().NotBeNull(); } [Theory] @@ -179,7 +179,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo ParseResult result = Parse(new CliOption("-y"), directive, $"{wholeText} -y"); - result.FindResultFor(directive).Values.Single().Should().Be(expectedValue); + result.GetResult(directive).Values.Single().Should().Be(expectedValue); } [Fact] @@ -189,7 +189,7 @@ public void Directives_without_a_value_specified_have_no_values() ParseResult result = Parse(new CliOption("-y"), directive, "[parse] -y"); - result.FindResultFor(directive).Values.Should().BeEmpty(); + result.GetResult(directive).Values.Should().BeEmpty(); } [Theory] @@ -216,7 +216,7 @@ public void Directives_cannot_contain_spaces(string value, string firstUnmatched CliDirective directive = new("parse"); ParseResult result = Parse(new CliOption("-y"), directive, $"{value} -y"); - result.FindResultFor(directive).Should().BeNull(); + result.GetResult(directive).Should().BeNull(); result.UnmatchedTokens.Should().BeEquivalentTo(firstUnmatchedToken, secondUnmatchedToken); } @@ -228,7 +228,7 @@ public void When_a_directive_is_specified_more_than_once_then_its_values_are_agg ParseResult result = Parse(new CliOption("-a"), directive, "[directive:one] [directive:two] -a"); - result.FindResultFor(directive).Values.Should().BeEquivalentTo("one", "two"); + result.GetResult(directive).Values.Should().BeEquivalentTo("one", "two"); } private static ParseResult Parse(CliOption option, CliDirective directive, string commandLine) diff --git a/src/System.CommandLine.Tests/GetValueByNameParserTests.cs b/src/System.CommandLine.Tests/GetValueByNameParserTests.cs index 944a7fd310..9293fe6aa1 100644 --- a/src/System.CommandLine.Tests/GetValueByNameParserTests.cs +++ b/src/System.CommandLine.Tests/GetValueByNameParserTests.cs @@ -130,7 +130,7 @@ public void When_required_argument_value_is_not_parsed_then_an_exception_is_thro getRequired .Should() .Throw() - .Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.FindResultFor(command.Arguments[0]))); + .Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.GetResult(command.Arguments[0]))); } [Fact] diff --git a/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs b/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs index 30b6a1b341..347c24898f 100644 --- a/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs +++ b/src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs @@ -41,14 +41,14 @@ public void When_option_is_not_respecified_but_limit_is_not_reached_then_the_fol var result = command.Parse("-a cat dog -v carrot"); result - .FindResultFor(animalsOption) + .GetResult(animalsOption) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo(new[] { "cat", "dog" }); result - .FindResultFor(vegetablesOption) + .GetResult(vegetablesOption) .Tokens .Select(t => t.Value) .Should() @@ -77,13 +77,13 @@ public void When_option_is_not_respecified_and_limit_is_reached_then_the_followi var result = command.Parse("-a cat some-arg -v carrot"); - result.FindResultFor(animalsOption) + result.GetResult(animalsOption) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo("cat"); - result.FindResultFor(vegetablesOption) + result.GetResult(vegetablesOption) .Tokens .Select(t => t.Value) .Should() @@ -131,7 +131,7 @@ public void All_consumed_tokens_are_present_in_option_result() _output.WriteLine(result.Diagram()); - var optionResult = result.FindResultFor(option); + var optionResult = result.GetResult(option); optionResult .Tokens diff --git a/src/System.CommandLine.Tests/OptionTests.cs b/src/System.CommandLine.Tests/OptionTests.cs index b555ab89a6..14f0a5bb22 100644 --- a/src/System.CommandLine.Tests/OptionTests.cs +++ b/src/System.CommandLine.Tests/OptionTests.cs @@ -199,7 +199,7 @@ 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.GetValue(optionA).Should().Be("value-for-a"); - result.FindResultFor(optionB).Should().BeNull(); + result.GetResult(optionB).Should().BeNull(); result.GetValue(optionC).Should().Be("value-for-c"); } @@ -213,7 +213,7 @@ public void Option_T_default_value_can_be_set_via_the_constructor() new CliRootCommand { option } .Parse("") - .FindResultFor(option) + .GetResult(option) .GetValueOrDefault() .Should() .Be(123); @@ -229,7 +229,7 @@ public void Option_T_default_value_can_be_set_after_instantiation() new CliRootCommand { option } .Parse("") - .FindResultFor(option) + .GetResult(option) .GetValueOrDefault() .Should() .Be(123); @@ -244,7 +244,7 @@ public void Option_T_default_value_factory_can_be_set_after_instantiation() new CliRootCommand { option } .Parse("") - .FindResultFor(option) + .GetResult(option) .GetValueOrDefault() .Should() .Be(123); @@ -275,7 +275,7 @@ public void Option_of_string_defaults_to_null_when_not_specified() var option = new CliOption("-x"); var result = new CliRootCommand { option }.Parse(""); - result.FindResultFor(option) + result.GetResult(option) .Should() .BeNull(); result.GetValue(option) @@ -290,7 +290,7 @@ public void Option_of_boolean_defaults_to_false_when_not_specified() var result = new CliRootCommand { option }.Parse(""); - result.FindResultFor(option) + result.GetResult(option) .Should() .BeNull(); result.GetValue(option) @@ -322,7 +322,7 @@ public void Option_result_provides_identifier_token_if_name_was_provided() var result = new CliRootCommand { option }.Parse("--name 123"); - result.FindResultFor(option).IdentifierToken.Value.Should().Be("--name"); + result.GetResult(option).IdentifierToken.Value.Should().Be("--name"); } [Fact] @@ -335,7 +335,7 @@ public void Option_result_provides_identifier_token_if_alias_was_provided() var result = new CliRootCommand { option }.Parse("-n 123"); - result.FindResultFor(option).IdentifierToken.Value.Should().Be("-n"); + result.GetResult(option).IdentifierToken.Value.Should().Be("-n"); } [Theory] @@ -356,7 +356,7 @@ public void Number_of_occurrences_of_identifier_token_is_exposed_by_option_resul new CliOption("-x") }; - var optionResult = root.Parse(commandLine).FindResultFor(option); + var optionResult = root.Parse(commandLine).GetResult(option); optionResult.IdentifierTokenCount.Should().Be(expectedCount); } diff --git a/src/System.CommandLine.Tests/ParseResultTests.cs b/src/System.CommandLine.Tests/ParseResultTests.cs index bac682ba67..240bf521d0 100644 --- a/src/System.CommandLine.Tests/ParseResultTests.cs +++ b/src/System.CommandLine.Tests/ParseResultTests.cs @@ -19,7 +19,7 @@ public void An_option_with_a_default_value_and_no_explicitly_provided_argument_h { option }.Parse("-x") - .FindResultFor(option); + .GetResult(option); result.Tokens.Should().BeEmpty(); } @@ -36,11 +36,11 @@ public void FindResult_can_be_used_to_check_the_presence_of_an_option() var result = command.Parse("the-command -h"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); } [Fact] - public void FindResultFor_can_be_used_to_check_the_presence_of_an_implicit_option() + public void GetResult_can_be_used_to_check_the_presence_of_an_implicit_option() { var option = new CliOption("-c", "--count") { DefaultValueFactory = (_) => 5 }; var command = new CliCommand("the-command") @@ -50,7 +50,7 @@ public void FindResultFor_can_be_used_to_check_the_presence_of_an_implicit_optio var result = command.Parse("the-command"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); } [Fact] diff --git a/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs b/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs index a31aec8935..c2740ec620 100644 --- a/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs +++ b/src/System.CommandLine.Tests/ParserTests.DoubleDash.cs @@ -24,7 +24,7 @@ public void Subsequent_tokens_are_parsed_as_arguments_even_if_they_match_option_ var result = rootCommand.Parse("-o \"some stuff\" -- -o --one -x -y -z -o:foo"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); result.GetValue(option).Should().BeEquivalentTo("some stuff"); diff --git a/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs b/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs index 4273be1d32..f79a09de15 100644 --- a/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs +++ b/src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs @@ -126,12 +126,12 @@ public void Multiple_arguments_of_unspecified_type_are_parsed_correctly() var result = root.Parse("src.txt dest.txt"); - result.FindResultFor(sourceArg) + result.GetResult(sourceArg) .GetValueOrDefault() .Should() .Be("src.txt"); - result.FindResultFor(destinationArg) + result.GetResult(destinationArg) .GetValueOrDefault() .Should() .Be("dest.txt"); @@ -257,7 +257,7 @@ public void Unsatisfied_subsequent_argument_with_min_arity_1_parses_as_default_v var result = rootCommand.Parse(""); - result.FindResultFor(arg1).Should().NotBeNull(); + result.GetResult(arg1).Should().NotBeNull(); result.GetValue(arg2).Should().Be("the-default"); } diff --git a/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs b/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs index 30bd45a8c1..7c74880137 100644 --- a/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs +++ b/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs @@ -30,7 +30,7 @@ public void An_argument_can_be_specified_in_more_than_one_position(string comman var parseResult = command.Parse(commandLine); - var argumentResult = parseResult.FindResultFor(argument); + var argumentResult = parseResult.GetResult(argument); argumentResult.Should().NotBeNull(); @@ -79,7 +79,7 @@ public void An_option_can_be_specified_in_more_than_one_position(string commandL var parseResult = command.Parse(commandLine); - var optionResult = parseResult.FindResultFor(option); + var optionResult = parseResult.GetResult(option); optionResult.Should().NotBeNull(); diff --git a/src/System.CommandLine.Tests/ParserTests.cs b/src/System.CommandLine.Tests/ParserTests.cs index b6adccc328..af8e89266c 100644 --- a/src/System.CommandLine.Tests/ParserTests.cs +++ b/src/System.CommandLine.Tests/ParserTests.cs @@ -37,8 +37,8 @@ public void An_option_can_be_checked_by_object_instance() var result = new CliRootCommand { option, option2 } .Parse("--flag"); - result.FindResultFor(option).Should().NotBeNull(); - result.FindResultFor(option2).Should().BeNull(); + result.GetResult(option).Should().NotBeNull(); + result.GetResult(option2).Should().BeNull(); } [Fact] @@ -50,8 +50,8 @@ public void Two_options_are_parsed_correctly() var result = new CliRootCommand { optionOne, optionTwo }.Parse("-o -t"); - result.FindResultFor(optionOne).Should().NotBeNull(); - result.FindResultFor(optionTwo).Should().NotBeNull(); + result.GetResult(optionOne).Should().NotBeNull(); + result.GetResult(optionTwo).Should().NotBeNull(); } [Theory] @@ -76,7 +76,7 @@ public void Short_form_options_can_be_specified_using_equals_delimiter() result.Errors.Should().BeEmpty(); - result.FindResultFor(option).Tokens.Should().ContainSingle(a => a.Value == "some-value"); + result.GetResult(option).Tokens.Should().ContainSingle(a => a.Value == "some-value"); } [Fact] @@ -88,7 +88,7 @@ public void Long_form_options_can_be_specified_using_equals_delimiter() result.Errors.Should().BeEmpty(); - result.FindResultFor(option).Tokens.Should().ContainSingle(a => a.Value == "there"); + result.GetResult(option).Tokens.Should().ContainSingle(a => a.Value == "there"); } [Fact] @@ -100,7 +100,7 @@ public void Short_form_options_can_be_specified_using_colon_delimiter() result.Errors.Should().BeEmpty(); - result.FindResultFor(option).Tokens.Should().ContainSingle(a => a.Value == "some-value"); + result.GetResult(option).Tokens.Should().ContainSingle(a => a.Value == "some-value"); } [Fact] @@ -112,7 +112,7 @@ public void Long_form_options_can_be_specified_using_colon_delimiter() result.Errors.Should().BeEmpty(); - result.FindResultFor(option).Tokens.Should().ContainSingle(a => a.Value == "there"); + result.GetResult(option).Tokens.Should().ContainSingle(a => a.Value == "there"); } [Fact] @@ -216,7 +216,7 @@ public void Required_option_arguments_are_not_unbundled() var result = command.Parse("-a -bc"); - result.FindResultFor(optionA) + result.GetResult(optionA) .Tokens .Should() .ContainSingle(t => t.Value == "-bc"); @@ -237,10 +237,10 @@ public void Last_bundled_option_can_accept_argument_with_no_separator() }; var result = command.Parse("-abcvalue"); - result.FindResultFor(optionA).Should().NotBeNull(); - result.FindResultFor(optionB).Should().NotBeNull(); + result.GetResult(optionA).Should().NotBeNull(); + result.GetResult(optionB).Should().NotBeNull(); - result.FindResultFor(optionC) + result.GetResult(optionC) .Tokens .Should() .ContainSingle(t => t.Value == "value"); @@ -261,10 +261,10 @@ public void Last_bundled_option_can_accept_argument_with_equals_separator() }; var result = command.Parse("-abc=value"); - result.FindResultFor(optionA).Should().NotBeNull(); - result.FindResultFor(optionB).Should().NotBeNull(); + result.GetResult(optionA).Should().NotBeNull(); + result.GetResult(optionB).Should().NotBeNull(); - result.FindResultFor(optionC) + result.GetResult(optionC) .Tokens .Should() .ContainSingle(t => t.Value == "value"); @@ -285,10 +285,10 @@ public void Last_bundled_option_can_accept_argument_with_colon_separator() }; var result = command.Parse("-abc:value"); - result.FindResultFor(optionA).Should().NotBeNull(); - result.FindResultFor(optionB).Should().NotBeNull(); + result.GetResult(optionA).Should().NotBeNull(); + result.GetResult(optionB).Should().NotBeNull(); - result.FindResultFor(optionC) + result.GetResult(optionC) .Tokens .Should() .ContainSingle(t => t.Value == "value"); @@ -309,15 +309,15 @@ public void Invalid_char_in_bundle_causes_rest_to_be_interpreted_as_value() }; var result = command.Parse("-abvcalue"); - result.FindResultFor(optionA).Should().NotBeNull(); - result.FindResultFor(optionB).Should().NotBeNull(); + result.GetResult(optionA).Should().NotBeNull(); + result.GetResult(optionB).Should().NotBeNull(); - result.FindResultFor(optionB) + result.GetResult(optionB) .Tokens .Should() .ContainSingle(t => t.Value == "vcalue"); - result.FindResultFor(optionC).Should().BeNull(); + result.GetResult(optionC).Should().BeNull(); } [Fact] @@ -333,13 +333,13 @@ public void Parser_root_Options_can_be_specified_multiple_times_and_their_argume var result = parser.Parse("-a cat -v carrot -a dog"); - result.FindResultFor(animalsOption) + result.GetResult(animalsOption) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo("cat", "dog"); - result.FindResultFor(vegetablesOption) + result.GetResult(vegetablesOption) .Tokens .Select(t => t.Value) .Should() @@ -360,13 +360,13 @@ public void Options_can_be_specified_multiple_times_and_their_arguments_are_coll var result = command.Parse("the-command -a cat -v carrot -a dog"); - result.FindResultFor(animalsOption) + result.GetResult(animalsOption) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo("cat", "dog"); - result.FindResultFor(vegetablesOption) + result.GetResult(vegetablesOption) .Tokens .Select(t => t.Value) .Should() @@ -390,13 +390,13 @@ public void When_an_option_is_not_respecified_but_limit_is_reached_then_the_foll var result = command.Parse("the-command -a cat some-arg -v carrot"); - result.FindResultFor(animalsOption) + result.GetResult(animalsOption) .Tokens .Select(t => t.Value) .Should() .BeEquivalentTo("cat"); - result.FindResultFor(vegetablesOption) + result.GetResult(vegetablesOption) .Tokens .Select(t => t.Value) .Should() @@ -593,7 +593,7 @@ public void When_child_option_will_not_accept_arg_then_parent_can() var result = command.Parse("the-command -x the-argument"); - var optionResult = result.FindResultFor(option); + var optionResult = result.GetResult(option); optionResult.Tokens.Should().BeEmpty(); result.CommandResult.Tokens.Select(t => t.Value).Should().BeEquivalentTo("the-argument"); } @@ -609,7 +609,7 @@ public void When_parent_option_will_not_accept_arg_then_child_can() var result = command.Parse("the-command -x the-argument"); - result.FindResultFor(option).Tokens.Select(t => t.Value).Should().BeEquivalentTo("the-argument"); + result.GetResult(option).Tokens.Select(t => t.Value).Should().BeEquivalentTo("the-argument"); result.CommandResult.Tokens.Should().BeEmpty(); } @@ -748,7 +748,7 @@ public void Options_only_apply_to_the_nearest_command() var result = outer.Parse("outer inner -x one -x two"); result.RootCommandResult - .FindResultFor(outerOption) + .GetResult(outerOption) .Should() .BeNull(); } @@ -847,7 +847,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_c ParseResult result = command.Parse("command"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); GetValue(result, option).Should().Be("the-default"); } @@ -866,7 +866,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_r var result = command.Parse("command"); - result.FindResultFor(option) + result.GetResult(option) .Implicit .Should() .BeTrue(); @@ -887,10 +887,10 @@ public void When_an_option_with_a_default_value_is_not_matched_then_there_are_no var result = command.Parse("command"); - result.FindResultFor(option) + result.GetResult(option) .IdentifierToken .Should() - .BeEquivalentTo(default(Token)); + .BeEquivalentTo(default(CliToken)); } [Fact] @@ -907,7 +907,7 @@ public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_ }; var result = command.Parse("command"); - result.FindResultFor(argument) + result.GetResult(argument) .Tokens .Should() .BeEmpty(); @@ -1061,7 +1061,7 @@ public void When_an_option_argument_is_enclosed_in_double_quotes_its_value_retai var parseResult = new CliRootCommand { option }.Parse(new[] { arg1, arg2 }); parseResult - .FindResultFor(option) + .GetResult(option) .Tokens .Select(t => t.Value) .Should() @@ -1295,7 +1295,7 @@ public void Option_aliases_do_not_need_to_be_prefixed() var result = new CliRootCommand { option }.Parse("noprefix"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); } [Fact] @@ -1357,9 +1357,9 @@ public void Command_argument_arity_can_be_a_fixed_value_greater_than_1() .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, argument), - new Token("2", TokenType.Argument, argument), - new Token("3", TokenType.Argument, argument)); + new CliToken("1", CliTokenType.Argument, argument), + new CliToken("2", CliTokenType.Argument, argument), + new CliToken("3", CliTokenType.Argument, argument)); } [Fact] @@ -1379,19 +1379,19 @@ public void Command_argument_arity_can_be_a_range_with_a_lower_bound_greater_tha .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, argument), - new Token("2", TokenType.Argument, argument), - new Token("3", TokenType.Argument, argument)); + new CliToken("1", CliTokenType.Argument, argument), + new CliToken("2", CliTokenType.Argument, argument), + new CliToken("3", CliTokenType.Argument, argument)); command.Parse("1 2 3 4 5") .CommandResult .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, argument), - new Token("2", TokenType.Argument, argument), - new Token("3", TokenType.Argument, argument), - new Token("4", TokenType.Argument, argument), - new Token("5", TokenType.Argument, argument)); + new CliToken("1", CliTokenType.Argument, argument), + new CliToken("2", CliTokenType.Argument, argument), + new CliToken("3", CliTokenType.Argument, argument), + new CliToken("4", CliTokenType.Argument, argument), + new CliToken("5", CliTokenType.Argument, argument)); } [Fact] @@ -1410,7 +1410,7 @@ public void When_command_arguments_are_fewer_than_minimum_arity_then_an_error_is result.Errors .Select(e => e.Message) .Should() - .Contain(LocalizationResources.RequiredArgumentMissing(result.FindResultFor(command.Arguments[0]))); + .Contain(LocalizationResources.RequiredArgumentMissing(result.GetResult(command.Arguments[0]))); } [Fact] @@ -1444,13 +1444,13 @@ public void Option_argument_arity_can_be_a_fixed_value_greater_than_1() }; command.Parse("-x 1 -x 2 -x 3") - .FindResultFor(option) + .GetResult(option) .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, default), - new Token("2", TokenType.Argument, default), - new Token("3", TokenType.Argument, default)); + new CliToken("1", CliTokenType.Argument, default), + new CliToken("2", CliTokenType.Argument, default), + new CliToken("3", CliTokenType.Argument, default)); } [Fact] @@ -1464,23 +1464,23 @@ public void Option_argument_arity_can_be_a_range_with_a_lower_bound_greater_than }; command.Parse("-x 1 -x 2 -x 3") - .FindResultFor(option) + .GetResult(option) .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, default), - new Token("2", TokenType.Argument, default), - new Token("3", TokenType.Argument, default)); + new CliToken("1", CliTokenType.Argument, default), + new CliToken("2", CliTokenType.Argument, default), + new CliToken("3", CliTokenType.Argument, default)); command.Parse("-x 1 -x 2 -x 3 -x 4 -x 5") - .FindResultFor(option) + .GetResult(option) .Tokens .Should() .BeEquivalentTo( - new Token("1", TokenType.Argument, default), - new Token("2", TokenType.Argument, default), - new Token("3", TokenType.Argument, default), - new Token("4", TokenType.Argument, default), - new Token("5", TokenType.Argument, default)); + new CliToken("1", CliTokenType.Argument, default), + new CliToken("2", CliTokenType.Argument, default), + new CliToken("3", CliTokenType.Argument, default), + new CliToken("4", CliTokenType.Argument, default), + new CliToken("5", CliTokenType.Argument, default)); } [Fact] @@ -1501,7 +1501,7 @@ public void When_option_arguments_are_fewer_than_minimum_arity_then_an_error_is_ result.Errors .Select(e => e.Message) .Should() - .Contain(LocalizationResources.RequiredArgumentMissing(result.FindResultFor(option))); + .Contain(LocalizationResources.RequiredArgumentMissing(result.GetResult(option))); } [Fact] diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs index 7827b9ed16..bd9687b2d2 100644 --- a/src/System.CommandLine.Tests/ParsingValidationTests.cs +++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs @@ -1297,7 +1297,7 @@ internal void When_there_is_an_arity_error_then_further_errors_are_not_reported( .Should() .Be("Required argument missing for option: '-o'."); } - + private CliArgument CreateArgumentWithAcceptOnlyFromAmong(string name, params string[] values) { CliArgument argument = new(name); diff --git a/src/System.CommandLine.Tests/ResponseFileTests.cs b/src/System.CommandLine.Tests/ResponseFileTests.cs index 05a8ef9f02..886116ea98 100644 --- a/src/System.CommandLine.Tests/ResponseFileTests.cs +++ b/src/System.CommandLine.Tests/ResponseFileTests.cs @@ -47,7 +47,7 @@ public void When_response_file_specified_it_loads_options_from_response_file() var result = new CliRootCommand { option }.Parse($"@{CreateResponseFile("--flag")}"); - result.FindResultFor(option).Should().NotBeNull(); + result.GetResult(option).Should().NotBeNull(); } [Fact] @@ -68,7 +68,7 @@ public void When_response_file_is_specified_it_loads_options_with_arguments_from } .Parse($"@{responseFile}"); - result.FindResultFor(optionOne).Should().NotBeNull(); + result.GetResult(optionOne).Should().NotBeNull(); result.GetValue(optionTwo).Should().Be(123); result.Errors.Should().BeEmpty(); } @@ -203,8 +203,8 @@ public void Response_file_can_contain_comments_which_are_ignored_when_loaded() optionTwo }.Parse($"@{responseFile}"); - result.FindResultFor(optionOne).Should().NotBeNull(); - result.FindResultFor(optionTwo).Should().NotBeNull(); + result.GetResult(optionOne).Should().NotBeNull(); + result.GetResult(optionTwo).Should().NotBeNull(); result.Errors.Should().BeEmpty(); } @@ -220,8 +220,8 @@ public void When_response_file_does_not_exist_then_error_is_returned() optionTwo }.Parse("@nonexistent.rsp"); - result.FindResultFor(optionOne).Should().BeNull(); - result.FindResultFor(optionTwo).Should().BeNull(); + result.GetResult(optionOne).Should().BeNull(); + result.GetResult(optionTwo).Should().BeNull(); result.Errors.Should().HaveCount(1); result.Errors.Single().Message.Should().Be("Response file not found 'nonexistent.rsp'."); } @@ -239,8 +239,8 @@ public void When_response_filepath_is_not_specified_then_error_is_returned() } .Parse("@"); - result.FindResultFor(optionOne).Should().BeNull(); - result.FindResultFor(optionTwo).Should().BeNull(); + result.GetResult(optionOne).Should().BeNull(); + result.GetResult(optionTwo).Should().BeNull(); result.Errors.Should().HaveCount(1); result.Errors .Single() @@ -264,8 +264,8 @@ public void When_response_file_cannot_be_read_then_specified_error_is_returned() optionTwo }.Parse($"@{nonexistent}"); - result.FindResultFor(optionOne).Should().BeNull(); - result.FindResultFor(optionTwo).Should().BeNull(); + result.GetResult(optionOne).Should().BeNull(); + result.GetResult(optionTwo).Should().BeNull(); result.Errors.Should().HaveCount(1); result.Errors.Single().Message.Should().StartWith($"Error reading response file '{nonexistent}'"); } @@ -312,7 +312,7 @@ public void When_response_file_processing_is_disabled_then_it_returns_response_f result.Tokens .Should() .Contain(t => t.Value == "@file.rsp" && - t.Type == TokenType.Argument); + t.Type == CliTokenType.Argument); result.Errors.Should().HaveCount(0); } diff --git a/src/System.CommandLine/Binding/ArgumentConverter.cs b/src/System.CommandLine/Binding/ArgumentConverter.cs index 84adf4169c..a54a3b2e82 100644 --- a/src/System.CommandLine/Binding/ArgumentConverter.cs +++ b/src/System.CommandLine/Binding/ArgumentConverter.cs @@ -16,10 +16,10 @@ internal static ArgumentConversionResult ConvertObject( { switch (value) { - case Token singleValue: + case CliToken singleValue: return ConvertToken(argumentResult, type, singleValue); - case IReadOnlyList manyValues: + case IReadOnlyList manyValues: return ConvertTokens(argumentResult, type, manyValues); default: @@ -30,7 +30,7 @@ internal static ArgumentConversionResult ConvertObject( private static ArgumentConversionResult ConvertToken( ArgumentResult argumentResult, Type type, - Token token) + CliToken token) { var value = token.Value; @@ -75,7 +75,7 @@ private static ArgumentConversionResult ConvertToken( private static ArgumentConversionResult ConvertTokens( ArgumentResult argumentResult, Type type, - IReadOnlyList tokens) + IReadOnlyList tokens) { var itemType = type.GetElementTypeIfEnumerable() ?? typeof(string); var values = CreateEnumerable(type, itemType, tokens.Count); diff --git a/src/System.CommandLine/CliCommand.cs b/src/System.CommandLine/CliCommand.cs index 1172233d22..e5441433b4 100644 --- a/src/System.CommandLine/CliCommand.cs +++ b/src/System.CommandLine/CliCommand.cs @@ -266,7 +266,7 @@ public override IEnumerable GetCompletions(CompletionContext con { CliCommand parentCommand = (CliCommand)parent.Symbol; - if (context.IsEmpty || context.ParseResult.FindResultFor(parentCommand) is not null) + if (context.IsEmpty || context.ParseResult.GetResult(parentCommand) is not null) { if (parentCommand.HasOptions) { diff --git a/src/System.CommandLine/Completions/CompletionContext.cs b/src/System.CommandLine/Completions/CompletionContext.cs index 6fb201a0ef..0ce1544cae 100644 --- a/src/System.CommandLine/Completions/CompletionContext.cs +++ b/src/System.CommandLine/Completions/CompletionContext.cs @@ -47,7 +47,7 @@ protected static string GetWordToComplete( ParseResult parseResult, int? position = null) { - Token? lastToken = parseResult.Tokens.LastOrDefault(t => t.Type != TokenType.Directive); + CliToken? lastToken = parseResult.Tokens.LastOrDefault(t => t.Type != CliTokenType.Directive); string? textToMatch = null; string? rawInput = parseResult.CommandLineText; @@ -75,8 +75,8 @@ protected static string GetWordToComplete( if (string.IsNullOrWhiteSpace(rawInput)) { - if (parseResult.UnmatchedTokens.Length > 0 || - lastToken?.Type == TokenType.Argument) + if (parseResult.UnmatchedTokens.Count > 0 || + lastToken?.Type == CliTokenType.Argument) { return textToMatch ?? ""; } diff --git a/src/System.CommandLine/Completions/SuggestDirective.cs b/src/System.CommandLine/Completions/SuggestDirective.cs index 6a47097874..4853194705 100644 --- a/src/System.CommandLine/Completions/SuggestDirective.cs +++ b/src/System.CommandLine/Completions/SuggestDirective.cs @@ -32,12 +32,12 @@ private sealed class SuggestDirectiveAction : CliAction public override int Invoke(ParseResult parseResult) { - string? parsedValues = parseResult.FindResultFor(_directive)!.Values.SingleOrDefault(); + string? parsedValues = parseResult.GetResult(_directive)!.Values.SingleOrDefault(); string? rawInput = parseResult.CommandLineText; int position = !string.IsNullOrEmpty(parsedValues) ? int.Parse(parsedValues) : rawInput?.Length ?? 0; - var commandLineToComplete = parseResult.Tokens.LastOrDefault(t => t.Type != TokenType.Directive)?.Value ?? ""; + var commandLineToComplete = parseResult.Tokens.LastOrDefault(t => t.Type != CliTokenType.Directive)?.Value ?? ""; var completionParseResult = parseResult.RootCommandResult.Command.Parse(commandLineToComplete, parseResult.Configuration); diff --git a/src/System.CommandLine/EnvironmentVariablesDirective.cs b/src/System.CommandLine/EnvironmentVariablesDirective.cs index d65ae37fe5..b47aa33509 100644 --- a/src/System.CommandLine/EnvironmentVariablesDirective.cs +++ b/src/System.CommandLine/EnvironmentVariablesDirective.cs @@ -48,7 +48,7 @@ public override Task InvokeAsync(ParseResult parseResult, CancellationToken private void SetEnvVars(ParseResult parseResult) { - DirectiveResult directiveResult = parseResult.FindResultFor(_directive)!; + DirectiveResult directiveResult = parseResult.GetResult(_directive)!; for (int i = 0; i < directiveResult.Values.Count; i++) { diff --git a/src/System.CommandLine/Invocation/TypoCorrectionAction.cs b/src/System.CommandLine/Invocation/TypoCorrectionAction.cs index 8daead5693..c490f94695 100644 --- a/src/System.CommandLine/Invocation/TypoCorrectionAction.cs +++ b/src/System.CommandLine/Invocation/TypoCorrectionAction.cs @@ -23,7 +23,7 @@ public override Task InvokeAsync(ParseResult parseResult, CancellationToken private static int ProvideSuggestions(ParseResult result) { var unmatchedTokens = result.UnmatchedTokens; - for (var i = 0; i < unmatchedTokens.Length; i++) + for (var i = 0; i < unmatchedTokens.Count; i++) { var token = unmatchedTokens[i]; diff --git a/src/System.CommandLine/ParseResult.cs b/src/System.CommandLine/ParseResult.cs index a540cace89..4a1bd0fcc9 100644 --- a/src/System.CommandLine/ParseResult.cs +++ b/src/System.CommandLine/ParseResult.cs @@ -18,7 +18,7 @@ namespace System.CommandLine public sealed class ParseResult { private readonly CommandResult _rootCommandResult; - private readonly IReadOnlyList _unmatchedTokens; + private readonly IReadOnlyList _unmatchedTokens; private CompletionContext? _completionContext; private readonly CliAction? _action; private readonly List? _nonexclusiveActions; @@ -28,8 +28,8 @@ internal ParseResult( CliConfiguration configuration, CommandResult rootCommandResult, CommandResult commandResult, - List tokens, - List? unmatchedTokens, + List tokens, + List? unmatchedTokens, List? errors, string? commandLineText = null, CliAction? action = null, @@ -52,11 +52,11 @@ internal ParseResult( } else { - Tokens = Array.Empty(); + Tokens = Array.Empty(); } CommandLineText = commandLineText; - _unmatchedTokens = unmatchedTokens is null ? Array.Empty() : unmatchedTokens; + _unmatchedTokens = unmatchedTokens is null ? Array.Empty() : unmatchedTokens; Errors = errors is not null ? errors : Array.Empty(); } @@ -85,7 +85,7 @@ internal ParseResult( /// /// Gets the tokens identified while parsing command line input. /// - public IReadOnlyList Tokens { get; } + public IReadOnlyList Tokens { get; } /// /// Holds the value of a complete command line input prior to splitting and tokenization, when provided. @@ -96,7 +96,7 @@ internal ParseResult( /// /// Gets the list of tokens used on the command line that were not matched by the parser. /// - public string[] UnmatchedTokens + public IReadOnlyList UnmatchedTokens => _unmatchedTokens.Count == 0 ? Array.Empty() : _unmatchedTokens.Select(t => t.Value).ToArray(); /// @@ -203,38 +203,38 @@ void Populate(Dictionary cache, IList s /// /// The argument for which to find a result. /// A result for the specified argument, or if it was not provided and no default was configured. - public ArgumentResult? FindResultFor(CliArgument argument) => - _rootCommandResult.FindResultFor(argument); + public ArgumentResult? GetResult(CliArgument argument) => + _rootCommandResult.GetResult(argument); /// /// Gets the result, if any, for the specified command. /// /// The command for which to find a result. /// A result for the specified command, or if it was not provided. - public CommandResult? FindResultFor(CliCommand command) => - _rootCommandResult.FindResultFor(command); + public CommandResult? GetResult(CliCommand command) => + _rootCommandResult.GetResult(command); /// /// Gets the result, if any, for the specified option. /// /// The option for which to find a result. /// A result for the specified option, or if it was not provided and no default was configured. - public OptionResult? FindResultFor(CliOption option) => - _rootCommandResult.FindResultFor(option); + public OptionResult? GetResult(CliOption option) => + _rootCommandResult.GetResult(option); /// /// Gets the result, if any, for the specified directive. /// /// The directive for which to find a result. /// A result for the specified directive, or if it was not provided. - public DirectiveResult? FindResultFor(CliDirective directive) => _rootCommandResult.FindResultFor(directive); + public DirectiveResult? GetResult(CliDirective directive) => _rootCommandResult.GetResult(directive); /// /// Gets the result, if any, for the specified symbol. /// /// The symbol for which to find a result. /// A result for the specified symbol, or if it was not provided and no default was configured. - public SymbolResult? FindResultFor(CliSymbol symbol) + public SymbolResult? GetResult(CliSymbol symbol) => _rootCommandResult.SymbolResultTree.TryGetValue(symbol, out SymbolResult? result) ? result : null; /// diff --git a/src/System.CommandLine/Parsing/ArgumentResult.cs b/src/System.CommandLine/Parsing/ArgumentResult.cs index 01fe38a4c7..b222b33732 100644 --- a/src/System.CommandLine/Parsing/ArgumentResult.cs +++ b/src/System.CommandLine/Parsing/ArgumentResult.cs @@ -98,7 +98,7 @@ public void OnlyTake(int numberOfTokens) while (!nextArgumentResult.ArgumentLimitReached && tokensToPass > 0) { - Token toPass = _tokens[numberOfTokens]; + CliToken toPass = _tokens[numberOfTokens]; _tokens.RemoveAt(numberOfTokens); nextArgumentResult.AddToken(toPass); --tokensToPass; @@ -110,7 +110,7 @@ public void OnlyTake(int numberOfTokens) // When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_they_become_unmatched_tokens while (tokensToPass > 0) { - Token unmatched = _tokens[numberOfTokens]; + CliToken unmatched = _tokens[numberOfTokens]; _tokens.RemoveAt(numberOfTokens); SymbolResultTree.AddUnmatchedToken(unmatched, parent.Command.TreatUnmatchedTokensAsErrors ? parent : null); --tokensToPass; diff --git a/src/System.CommandLine/Parsing/CliParser.cs b/src/System.CommandLine/Parsing/CliParser.cs index 6aff55b75a..d05b9f0552 100644 --- a/src/System.CommandLine/Parsing/CliParser.cs +++ b/src/System.CommandLine/Parsing/CliParser.cs @@ -151,7 +151,7 @@ private static ParseResult Parse( arguments.Tokenize( configuration, inferRootCommand: rawInput is not null, - out List tokens, + out List tokens, out List? tokenizationErrors); var operation = new ParseOperation( diff --git a/src/System.CommandLine/Parsing/Token.cs b/src/System.CommandLine/Parsing/CliToken.cs similarity index 62% rename from src/System.CommandLine/Parsing/Token.cs rename to src/System.CommandLine/Parsing/CliToken.cs index ef5eeba58e..e268459468 100644 --- a/src/System.CommandLine/Parsing/Token.cs +++ b/src/System.CommandLine/Parsing/CliToken.cs @@ -6,14 +6,14 @@ namespace System.CommandLine.Parsing /// /// A unit of significant text on the command line. /// - public sealed class Token : IEquatable + public sealed class CliToken : IEquatable { internal const int ImplicitPosition = -1; /// The string value of the token. /// The type of the token. /// The symbol represented by the token - public Token(string? value, TokenType type, CliSymbol symbol) + public CliToken(string? value, CliTokenType type, CliSymbol symbol) { Value = value ?? ""; Type = type; @@ -21,7 +21,7 @@ public Token(string? value, TokenType type, CliSymbol symbol) Position = ImplicitPosition; } - internal Token(string? value, TokenType type, CliSymbol? symbol, int position) + internal CliToken(string? value, CliTokenType type, CliSymbol? symbol, int position) { Value = value ?? ""; Type = type; @@ -41,7 +41,7 @@ internal Token(string? value, TokenType type, CliSymbol? symbol, int position) /// /// The type of the token. /// - public TokenType Type { get; } + public CliTokenType Type { get; } /// /// The Symbol represented by the token (if any). @@ -49,10 +49,10 @@ internal Token(string? value, TokenType type, CliSymbol? symbol, int position) internal CliSymbol? Symbol { get; set; } /// - public override bool Equals(object? obj) => Equals(obj as Token); + public override bool Equals(object? obj) => Equals(obj as CliToken); /// - public bool Equals(Token? other) => other is not null && Value == other.Value && Type == other.Type && ReferenceEquals(Symbol, other.Symbol); + public bool Equals(CliToken? other) => other is not null && Value == other.Value && Type == other.Type && ReferenceEquals(Symbol, other.Symbol); /// public override int GetHashCode() => Value.GetHashCode() ^ (int)Type; @@ -61,19 +61,19 @@ internal Token(string? value, TokenType type, CliSymbol? symbol, int position) public override string ToString() => Value; /// - /// Checks if two specified instances have the same value. + /// Checks if two specified instances have the same value. /// - /// The first . - /// The second . + /// The first . + /// The second . /// if the objects are equal. - public static bool operator ==(Token? left, Token? right) => left is null ? right is null : left.Equals(right); + public static bool operator ==(CliToken? left, CliToken? right) => left is null ? right is null : left.Equals(right); /// - /// Checks if two specified instances have different values. + /// Checks if two specified instances have different values. /// - /// The first . - /// The second . + /// The first . + /// The second . /// if the objects are not equal. - public static bool operator !=(Token? left, Token? right) => left is null ? right is not null : !left.Equals(right); + public static bool operator !=(CliToken? left, CliToken? right) => left is null ? right is not null : !left.Equals(right); } } diff --git a/src/System.CommandLine/Parsing/TokenType.cs b/src/System.CommandLine/Parsing/CliTokenType.cs similarity index 91% rename from src/System.CommandLine/Parsing/TokenType.cs rename to src/System.CommandLine/Parsing/CliTokenType.cs index b0ccc047b3..4ce8eecc01 100644 --- a/src/System.CommandLine/Parsing/TokenType.cs +++ b/src/System.CommandLine/Parsing/CliTokenType.cs @@ -4,9 +4,9 @@ namespace System.CommandLine.Parsing { /// - /// Identifies the type of a . + /// Identifies the type of a . /// - public enum TokenType + public enum CliTokenType { /// /// An argument token. diff --git a/src/System.CommandLine/Parsing/CommandResult.cs b/src/System.CommandLine/Parsing/CommandResult.cs index 4316d2904e..be9b16ecbd 100644 --- a/src/System.CommandLine/Parsing/CommandResult.cs +++ b/src/System.CommandLine/Parsing/CommandResult.cs @@ -13,7 +13,7 @@ public sealed class CommandResult : SymbolResult { internal CommandResult( CliCommand command, - Token token, + CliToken token, SymbolResultTree symbolResultTree, CommandResult? parent = null) : base(symbolResultTree, parent) @@ -30,7 +30,7 @@ internal CommandResult( /// /// The token that was parsed to specify the command. /// - public Token IdentifierToken { get; } + public CliToken IdentifierToken { get; } /// /// Child symbol results in the parse tree. diff --git a/src/System.CommandLine/Parsing/DirectiveResult.cs b/src/System.CommandLine/Parsing/DirectiveResult.cs index 3db0cd769b..a1d08544a7 100644 --- a/src/System.CommandLine/Parsing/DirectiveResult.cs +++ b/src/System.CommandLine/Parsing/DirectiveResult.cs @@ -9,7 +9,7 @@ public sealed class DirectiveResult : SymbolResult { private List? _values; - internal DirectiveResult(CliDirective directive, Token token, SymbolResultTree symbolResultTree) + internal DirectiveResult(CliDirective directive, CliToken token, SymbolResultTree symbolResultTree) : base(symbolResultTree, null) // directives don't belong to any command { Directive = directive; @@ -30,7 +30,7 @@ internal DirectiveResult(CliDirective directive, Token token, SymbolResultTree s /// /// The token that was parsed to specify the directive. /// - public Token Token { get; } + public CliToken Token { get; } internal void AddValue(string value) => (_values ??= new()).Add(value); } diff --git a/src/System.CommandLine/Parsing/OptionResult.cs b/src/System.CommandLine/Parsing/OptionResult.cs index 1e28632d3a..e8bd70d329 100644 --- a/src/System.CommandLine/Parsing/OptionResult.cs +++ b/src/System.CommandLine/Parsing/OptionResult.cs @@ -17,7 +17,7 @@ public sealed class OptionResult : SymbolResult internal OptionResult( CliOption option, SymbolResultTree symbolResultTree, - Token? token = null, + CliToken? token = null, CommandResult? parent = null) : base(symbolResultTree, parent) { @@ -40,7 +40,7 @@ internal OptionResult( /// The token that was parsed to specify the option. /// /// An identifier token is a token that matches either the option's name or one of its aliases. - public Token? IdentifierToken { get; } + public CliToken? IdentifierToken { get; } /// /// The number of occurrences of an identifier token matching the option. @@ -63,7 +63,7 @@ internal bool IsArgumentLimitReached => Option.Argument.Arity.MaximumNumberOfValues == (Implicit ? Tokens.Count - 1 : Tokens.Count); internal ArgumentConversionResult ArgumentConversionResult - => _argumentConversionResult ??= FindResultFor(Option.Argument)!.GetArgumentConversionResult(); + => _argumentConversionResult ??= GetResult(Option.Argument)!.GetArgumentConversionResult(); internal override bool UseDefaultValueFor(ArgumentResult argument) => Implicit; } diff --git a/src/System.CommandLine/Parsing/ParseDiagramAction.cs b/src/System.CommandLine/Parsing/ParseDiagramAction.cs index d9932b2f42..ccdf486562 100644 --- a/src/System.CommandLine/Parsing/ParseDiagramAction.cs +++ b/src/System.CommandLine/Parsing/ParseDiagramAction.cs @@ -52,11 +52,11 @@ internal static StringBuilder Diagram(ParseResult parseResult) Diagram(builder, parseResult.RootCommandResult, parseResult); var unmatchedTokens = parseResult.UnmatchedTokens; - if (unmatchedTokens.Length > 0) + if (unmatchedTokens.Count > 0) { builder.Append(" ???-->"); - for (var i = 0; i < unmatchedTokens.Length; i++) + for (var i = 0; i < unmatchedTokens.Count; i++) { var error = unmatchedTokens[i]; builder.Append(" "); diff --git a/src/System.CommandLine/Parsing/ParseOperation.cs b/src/System.CommandLine/Parsing/ParseOperation.cs index 2b2512ef8b..3385b3c897 100644 --- a/src/System.CommandLine/Parsing/ParseOperation.cs +++ b/src/System.CommandLine/Parsing/ParseOperation.cs @@ -9,7 +9,7 @@ namespace System.CommandLine.Parsing { internal sealed class ParseOperation { - private readonly List _tokens; + private readonly List _tokens; private readonly CliConfiguration _configuration; private readonly string? _rawInput; private readonly SymbolResultTree _symbolResultTree; @@ -23,7 +23,7 @@ internal sealed class ParseOperation private List? _nonexclusiveActions; public ParseOperation( - List tokens, + List tokens, CliConfiguration configuration, List? tokenizeErrors, string? rawInput) @@ -40,14 +40,14 @@ public ParseOperation( Advance(); } - private Token CurrentToken => _tokens[_index]; + private CliToken CurrentToken => _tokens[_index]; private void Advance() => _index++; - private bool More(out TokenType currentTokenType) + private bool More(out CliTokenType currentTokenType) { bool result = _index < _tokens.Count; - currentTokenType = result ? _tokens[_index].Type : (TokenType)(-1); + currentTokenType = result ? _tokens[_index].Type : (CliTokenType)(-1); return result; } @@ -109,17 +109,17 @@ private void ParseCommandChildren() int currentArgumentCount = 0; int currentArgumentIndex = 0; - while (More(out TokenType currentTokenType)) + while (More(out CliTokenType currentTokenType)) { - if (currentTokenType == TokenType.Command) + if (currentTokenType == CliTokenType.Command) { ParseSubcommand(); } - else if (currentTokenType == TokenType.Option) + else if (currentTokenType == CliTokenType.Option) { ParseOption(); } - else if (currentTokenType == TokenType.Argument) + else if (currentTokenType == CliTokenType.Argument) { ParseCommandArguments(ref currentArgumentCount, ref currentArgumentIndex); } @@ -133,7 +133,7 @@ private void ParseCommandChildren() private void ParseCommandArguments(ref int currentArgumentCount, ref int currentArgumentIndex) { - while (More(out TokenType currentTokenType) && currentTokenType == TokenType.Argument) + while (More(out CliTokenType currentTokenType) && currentTokenType == CliTokenType.Argument) { while (_innermostCommandResult.Command.HasArguments && currentArgumentIndex < _innermostCommandResult.Command.Arguments.Count) { @@ -231,7 +231,7 @@ private void ParseOptionArguments(OptionResult optionResult) var contiguousTokens = 0; int argumentCount = 0; - while (More(out TokenType currentTokenType) && currentTokenType == TokenType.Argument) + while (More(out CliTokenType currentTokenType) && currentTokenType == CliTokenType.Argument) { if (argumentCount >= argument.Arity.MaximumNumberOfValues) { @@ -289,7 +289,7 @@ private void ParseOptionArguments(OptionResult optionResult) private void ParseDirectives() { - while (More(out TokenType currentTokenType) && currentTokenType == TokenType.Directive) + while (More(out CliTokenType currentTokenType) && currentTokenType == CliTokenType.Directive) { if (_configuration.Directives.Count > 0) { @@ -354,7 +354,7 @@ void ParseDirective() private void AddCurrentTokenToUnmatched() { - if (CurrentToken.Type == TokenType.DoubleDash) + if (CurrentToken.Type == CliTokenType.DoubleDash) { return; } diff --git a/src/System.CommandLine/Parsing/StringExtensions.cs b/src/System.CommandLine/Parsing/StringExtensions.cs index bcdaacf971..839d9a4d1b 100644 --- a/src/System.CommandLine/Parsing/StringExtensions.cs +++ b/src/System.CommandLine/Parsing/StringExtensions.cs @@ -48,7 +48,7 @@ internal static void Tokenize( this IReadOnlyList args, CliConfiguration configuration, bool inferRootCommand, - out List tokens, + out List tokens, out List? errors) { const int FirstArgIsNotRootCommand = -1; @@ -59,7 +59,7 @@ internal static void Tokenize( var foundDoubleDash = false; var foundEndOfDirectives = false; - var tokenList = new List(args.Count); + var tokenList = new List(args.Count); var knownTokens = configuration.RootCommand.ValidTokens(configuration.Directives); @@ -148,11 +148,11 @@ internal static void Tokenize( { switch (token.Type) { - case TokenType.Option: + case CliTokenType.Option: tokenList.Add(Option(arg, (CliOption)token.Symbol!)); break; - case TokenType.Command: + case CliTokenType.Command: CliCommand cmd = (CliCommand)token.Symbol!; if (cmd != currentCommand) { @@ -175,7 +175,7 @@ internal static void Tokenize( } else if (arg.TrySplitIntoSubtokens(out var first, out var rest) && knownTokens.TryGetValue(first, out var subtoken) && - subtoken.Type == TokenType.Option) + subtoken.Type == CliTokenType.Option) { tokenList.Add(Option(first, (CliOption)subtoken.Symbol!)); @@ -191,19 +191,19 @@ internal static void Tokenize( tokenList.Add(Argument(arg)); } - Token Argument(string value) => new(value, TokenType.Argument, default, i); + CliToken Argument(string value) => new(value, CliTokenType.Argument, default, i); - Token CommandArgument(string value, CliCommand command) => new(value, TokenType.Argument, command, i); + CliToken CommandArgument(string value, CliCommand command) => new(value, CliTokenType.Argument, command, i); - Token OptionArgument(string value, CliOption option) => new(value, TokenType.Argument, option, i); + CliToken OptionArgument(string value, CliOption option) => new(value, CliTokenType.Argument, option, i); - Token Command(string value, CliCommand cmd) => new(value, TokenType.Command, cmd, i); + CliToken Command(string value, CliCommand cmd) => new(value, CliTokenType.Command, cmd, i); - Token Option(string value, CliOption option) => new(value, TokenType.Option, option, i); + CliToken Option(string value, CliOption option) => new(value, CliTokenType.Option, option, i); - Token DoubleDash() => new("--", TokenType.DoubleDash, default, i); + CliToken DoubleDash() => new("--", CliTokenType.DoubleDash, default, i); - Token Directive(string value, CliDirective? directive) => new(value, TokenType.Directive, directive, i); + CliToken Directive(string value, CliDirective? directive) => new(value, CliTokenType.Directive, directive, i); } tokens = tokenList; @@ -229,24 +229,24 @@ bool TryUnbundle(ReadOnlySpan alias, int argumentIndex) { if (alias[i] == ':' || alias[i] == '=') { - tokenList.Add(new Token(alias.Slice(i + 1).ToString(), TokenType.Argument, default, argumentIndex)); + tokenList.Add(new CliToken(alias.Slice(i + 1).ToString(), CliTokenType.Argument, default, argumentIndex)); return true; } pCandidate[1] = alias[i]; - if (!knownTokens.TryGetValue(candidate, out Token? found)) + if (!knownTokens.TryGetValue(candidate, out CliToken? found)) { - if (tokensBefore != tokenList.Count && tokenList[tokenList.Count - 1].Type == TokenType.Option) + if (tokensBefore != tokenList.Count && tokenList[tokenList.Count - 1].Type == CliTokenType.Option) { // Invalid_char_in_bundle_causes_rest_to_be_interpreted_as_value - tokenList.Add(new Token(alias.Slice(i).ToString(), TokenType.Argument, default, argumentIndex)); + tokenList.Add(new CliToken(alias.Slice(i).ToString(), CliTokenType.Argument, default, argumentIndex)); return true; } return false; } - tokenList.Add(new Token(found.Value, found.Type, found.Symbol, argumentIndex)); + tokenList.Add(new CliToken(found.Value, found.Type, found.Symbol, argumentIndex)); if (i != alias.Length - 1 && ((CliOption)found.Symbol!).Greedy) { int index = i + 1; @@ -254,7 +254,7 @@ bool TryUnbundle(ReadOnlySpan alias, int argumentIndex) { index++; // Last_bundled_option_can_accept_argument_with_colon_separator } - tokenList.Add(new Token(alias.Slice(index).ToString(), TokenType.Argument, default, argumentIndex)); + tokenList.Add(new CliToken(alias.Slice(index).ToString(), CliTokenType.Argument, default, argumentIndex)); return true; } } @@ -270,7 +270,7 @@ bool PreviousTokenIsAnOptionExpectingAnArgument(out CliOption? option) { var token = tokenList[tokenList.Count - 1]; - if (token.Type == TokenType.Option) + if (token.Type == CliTokenType.Option) { if (token.Symbol is CliOption { Greedy: true } opt) { @@ -405,16 +405,16 @@ static IEnumerable SplitLine(string line) } } - private static Dictionary ValidTokens(this CliCommand command, IReadOnlyList? directives) + private static Dictionary ValidTokens(this CliCommand command, IReadOnlyList? directives) { - Dictionary tokens = new(StringComparer.Ordinal); + Dictionary tokens = new(StringComparer.Ordinal); if (directives is not null) { for (int directiveIndex = 0; directiveIndex < directives.Count; directiveIndex++) { CliDirective directive = directives[directiveIndex]; - tokens[directive.Name] = new Token(directive.Name, TokenType.Directive, directive, Token.ImplicitPosition); + tokens[directive.Name] = new CliToken(directive.Name, CliTokenType.Directive, directive, CliToken.ImplicitPosition); } } @@ -468,24 +468,24 @@ private static Dictionary ValidTokens(this CliCommand command, IR return tokens; - static void AddCommandTokens(Dictionary tokens, CliCommand cmd) + static void AddCommandTokens(Dictionary tokens, CliCommand cmd) { - tokens.Add(cmd.Name, new Token(cmd.Name, TokenType.Command, cmd, Token.ImplicitPosition)); + tokens.Add(cmd.Name, new CliToken(cmd.Name, CliTokenType.Command, cmd, CliToken.ImplicitPosition)); if (cmd._aliases is not null) { foreach (string childAlias in cmd._aliases) { - tokens.Add(childAlias, new Token(childAlias, TokenType.Command, cmd, Token.ImplicitPosition)); + tokens.Add(childAlias, new CliToken(childAlias, CliTokenType.Command, cmd, CliToken.ImplicitPosition)); } } } - static void AddOptionTokens(Dictionary tokens, CliOption option) + static void AddOptionTokens(Dictionary tokens, CliOption option) { if (!tokens.ContainsKey(option.Name)) { - tokens.Add(option.Name, new Token(option.Name, TokenType.Option, option, Token.ImplicitPosition)); + tokens.Add(option.Name, new CliToken(option.Name, CliTokenType.Option, option, CliToken.ImplicitPosition)); } if (option._aliases is not null) @@ -494,7 +494,7 @@ static void AddOptionTokens(Dictionary tokens, CliOption option) { if (!tokens.ContainsKey(childAlias)) { - tokens.Add(childAlias, new Token(childAlias, TokenType.Option, option, Token.ImplicitPosition)); + tokens.Add(childAlias, new CliToken(childAlias, CliTokenType.Option, option, CliToken.ImplicitPosition)); } } } diff --git a/src/System.CommandLine/Parsing/SymbolResult.cs b/src/System.CommandLine/Parsing/SymbolResult.cs index 75da9cf598..cff413b4f9 100644 --- a/src/System.CommandLine/Parsing/SymbolResult.cs +++ b/src/System.CommandLine/Parsing/SymbolResult.cs @@ -12,7 +12,7 @@ namespace System.CommandLine.Parsing public abstract class SymbolResult { internal readonly SymbolResultTree SymbolResultTree; - private protected List? _tokens; + private protected List? _tokens; private protected SymbolResult(SymbolResultTree symbolResultTree, SymbolResult? parent) { @@ -53,9 +53,9 @@ public IEnumerable Errors /// /// The list of tokens associated with this symbol result during parsing. /// - public IReadOnlyList Tokens => _tokens is not null ? _tokens : Array.Empty(); + public IReadOnlyList Tokens => _tokens is not null ? _tokens : Array.Empty(); - internal void AddToken(Token token) => (_tokens ??= new()).Add(token); + internal void AddToken(CliToken token) => (_tokens ??= new()).Add(token); /// /// Adds an error message for this symbol result to it's parse tree. @@ -68,33 +68,33 @@ public IEnumerable Errors /// /// The argument for which to find a result. /// An argument result if the argument was matched by the parser or has a default value; otherwise, null. - public ArgumentResult? FindResultFor(CliArgument argument) => SymbolResultTree.FindResultFor(argument); + public ArgumentResult? GetResult(CliArgument argument) => SymbolResultTree.GetResult(argument); /// /// Finds a result for the specific command anywhere in the parse tree, including parent and child symbol results. /// /// The command for which to find a result. /// An command result if the command was matched by the parser; otherwise, null. - public CommandResult? FindResultFor(CliCommand command) => SymbolResultTree.FindResultFor(command); + public CommandResult? GetResult(CliCommand command) => SymbolResultTree.GetResult(command); /// /// Finds a result for the specific option anywhere in the parse tree, including parent and child symbol results. /// /// The option for which to find a result. /// An option result if the option was matched by the parser or has a default value; otherwise, null. - public OptionResult? FindResultFor(CliOption option) => SymbolResultTree.FindResultFor(option); + public OptionResult? GetResult(CliOption option) => SymbolResultTree.GetResult(option); /// /// Finds a result for the specific directive anywhere in the parse tree. /// /// The directive for which to find a result. /// A directive result if the directive was matched by the parser, null otherwise. - public DirectiveResult? FindResultFor(CliDirective directive) => SymbolResultTree.FindResultFor(directive); + public DirectiveResult? GetResult(CliDirective directive) => SymbolResultTree.GetResult(directive); /// public T? GetValue(CliArgument argument) { - if (FindResultFor(argument) is { } result && + if (GetResult(argument) is { } result && result.GetValueOrDefault() is { } t) { return t; @@ -106,7 +106,7 @@ public IEnumerable Errors /// public T? GetValue(CliOption option) { - if (FindResultFor(option) is { } result && + if (GetResult(option) is { } result && result.GetValueOrDefault() is { } t) { return t; diff --git a/src/System.CommandLine/Parsing/SymbolResultTree.cs b/src/System.CommandLine/Parsing/SymbolResultTree.cs index 00c3603884..4c73177f6f 100644 --- a/src/System.CommandLine/Parsing/SymbolResultTree.cs +++ b/src/System.CommandLine/Parsing/SymbolResultTree.cs @@ -8,7 +8,7 @@ namespace System.CommandLine.Parsing internal sealed class SymbolResultTree : Dictionary { internal List? Errors; - internal List? UnmatchedTokens; + internal List? UnmatchedTokens; internal SymbolResultTree(List? tokenizeErrors) { @@ -25,16 +25,16 @@ internal SymbolResultTree(List? tokenizeErrors) internal int ErrorCount => Errors?.Count ?? 0; - internal ArgumentResult? FindResultFor(CliArgument argument) + internal ArgumentResult? GetResult(CliArgument argument) => TryGetValue(argument, out SymbolResult? result) ? (ArgumentResult)result : default; - internal CommandResult? FindResultFor(CliCommand command) + internal CommandResult? GetResult(CliCommand command) => TryGetValue(command, out SymbolResult? result) ? (CommandResult)result : default; - internal OptionResult? FindResultFor(CliOption option) + internal OptionResult? GetResult(CliOption option) => TryGetValue(option, out SymbolResult? result) ? (OptionResult)result : default; - internal DirectiveResult? FindResultFor(CliDirective directive) + internal DirectiveResult? GetResult(CliDirective directive) => TryGetValue(directive, out SymbolResult? result) ? (DirectiveResult)result : default; internal IEnumerable GetChildren(SymbolResult parent) @@ -55,7 +55,7 @@ internal IEnumerable GetChildren(SymbolResult parent) internal void InsertFirstError(ParseError parseError) => (Errors ??= new()).Insert(0, parseError); - internal void AddUnmatchedToken(Token token, CommandResult? commandResult) + internal void AddUnmatchedToken(CliToken token, CommandResult? commandResult) { (UnmatchedTokens ??= new()).Add(token);