Skip to content

Commit

Permalink
API review updates (#2167)
Browse files Browse the repository at this point in the history
* rename `FindResultFor` to `GetResult`

This addresses feedback from API review: dotnet/runtime#84177 (comment)

* change type of `UnmatchedTokens`

* rename `Token` and `TokenType` to `CliToken` and `CliTokenType`

* API baseline updates
  • Loading branch information
jonsequitur authored Apr 13, 2023
1 parent 7c049d9 commit c75eab4
Show file tree
Hide file tree
Showing 38 changed files with 266 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ System.CommandLine
public CliConfiguration Configuration { get; }
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.ParseError> Errors { get; }
public System.CommandLine.Parsing.CommandResult RootCommandResult { get; }
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.Token> 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<System.CommandLine.Parsing.CliToken> Tokens { get; }
public System.Collections.Generic.IReadOnlyList<System.String> UnmatchedTokens { get; }
public System.CommandLine.Completions.CompletionContext GetCompletionContext()
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.Nullable<System.Int32> 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<T>(CliArgument<T> argument)
public T GetValue<T>(CliOption<T> option)
public T GetValue<T>(System.String name)
Expand Down Expand Up @@ -236,17 +236,33 @@ System.CommandLine.Parsing
public static System.CommandLine.ParseResult Parse(System.CommandLine.CliCommand command, System.Collections.Generic.IReadOnlyList<System.String> 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<System.String> SplitCommandLine(System.String commandLine)
public class CliToken, System.IEquatable<CliToken>
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<SymbolResult> 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<System.String> 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; }
Expand All @@ -259,30 +275,14 @@ System.CommandLine.Parsing
public abstract class SymbolResult
public System.Collections.Generic.IEnumerable<ParseError> Errors { get; }
public SymbolResult Parent { get; }
public System.Collections.Generic.IReadOnlyList<Token> Tokens { get; }
public System.Collections.Generic.IReadOnlyList<CliToken> 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<T>(CliArgument<T> argument)
public T GetValue<T>(CliOption<T> option)
public class Token, System.IEquatable<Token>
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<System.String> replacementTokens, ref System.String& errorMessage, System.AsyncCallback callback, System.Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Hosting/HostingAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override BindingContext GetBindingContext(ParseResult parseResult)
public async override Task<int> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(valueDescriptor.ValueName), parseResult.RootCommandResult.SymbolResultTree, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>();
}
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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<object>();
return true;
}
break;
case CliArgument argument:
var argumentResult = bindingContext?.ParseResult.FindResultFor(argument);
var argumentResult = bindingContext?.ParseResult.GetResult(argument);
if (argumentResult is not null)
{
boundValue = argumentResult.GetValueOrDefault<object>();
Expand Down
12 changes: 6 additions & 6 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<int[]>()
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 },
options => options.WithStrictOrdering());

parseResult.FindResultFor(argument2)
parseResult.GetResult(argument2)
.GetValueOrDefault<int[]>()
.Should()
.BeEquivalentTo(new[] { 4, 5, 6, 7, 8 },
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>().Should().BeAssignableTo(argumentType);
result.GetResult(option).GetValueOrDefault<object>().Should().BeAssignableTo(argumentType);
}

[Fact]
Expand Down
16 changes: 8 additions & 8 deletions src/System.CommandLine.Tests/DirectiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Raw_tokens_still_hold_directives()

ParseResult result = Parse(new CliOption<bool>("-y"), directive, "[parse] -y");

result.FindResultFor(directive).Should().NotBeNull();
result.GetResult(directive).Should().NotBeNull();
result.Tokens.Should().Contain(t => t.Value == "[parse]");
}

Expand All @@ -40,7 +40,7 @@ public void Directives_must_precede_other_symbols()

ParseResult result = Parse(new CliOption<bool>("-y"), directive, "-y [parse]");

result.FindResultFor(directive).Should().BeNull();
result.GetResult(directive).Should().BeNull();
}

[Fact]
Expand All @@ -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]
Expand Down Expand Up @@ -179,7 +179,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo

ParseResult result = Parse(new CliOption<bool>("-y"), directive, $"{wholeText} -y");

result.FindResultFor(directive).Values.Single().Should().Be(expectedValue);
result.GetResult(directive).Values.Single().Should().Be(expectedValue);
}

[Fact]
Expand All @@ -189,7 +189,7 @@ public void Directives_without_a_value_specified_have_no_values()

ParseResult result = Parse(new CliOption<bool>("-y"), directive, "[parse] -y");

result.FindResultFor(directive).Values.Should().BeEmpty();
result.GetResult(directive).Values.Should().BeEmpty();
}

[Theory]
Expand All @@ -216,7 +216,7 @@ public void Directives_cannot_contain_spaces(string value, string firstUnmatched

CliDirective directive = new("parse");
ParseResult result = Parse(new CliOption<bool>("-y"), directive, $"{value} -y");
result.FindResultFor(directive).Should().BeNull();
result.GetResult(directive).Should().BeNull();

result.UnmatchedTokens.Should().BeEquivalentTo(firstUnmatchedToken, secondUnmatchedToken);
}
Expand All @@ -228,7 +228,7 @@ public void When_a_directive_is_specified_more_than_once_then_its_values_are_agg

ParseResult result = Parse(new CliOption<bool>("-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)
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Tests/GetValueByNameParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void When_required_argument_value_is_not_parsed_then_an_exception_is_thro
getRequired
.Should()
.Throw<InvalidOperationException>()
.Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.FindResultFor(command.Arguments[0])));
.Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.GetResult(command.Arguments[0])));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit c75eab4

Please sign in to comment.