Skip to content

Commit c75eab4

Browse files
authored
API review updates (#2167)
* 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
1 parent 7c049d9 commit c75eab4

38 files changed

+266
-266
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ System.CommandLine
136136
public CliConfiguration Configuration { get; }
137137
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.ParseError> Errors { get; }
138138
public System.CommandLine.Parsing.CommandResult RootCommandResult { get; }
139-
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.Token> Tokens { get; }
140-
public System.String[] UnmatchedTokens { get; }
141-
public System.CommandLine.Parsing.ArgumentResult FindResultFor(CliArgument argument)
142-
public System.CommandLine.Parsing.CommandResult FindResultFor(CliCommand command)
143-
public System.CommandLine.Parsing.OptionResult FindResultFor(CliOption option)
144-
public System.CommandLine.Parsing.DirectiveResult FindResultFor(CliDirective directive)
145-
public System.CommandLine.Parsing.SymbolResult FindResultFor(CliSymbol symbol)
139+
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.CliToken> Tokens { get; }
140+
public System.Collections.Generic.IReadOnlyList<System.String> UnmatchedTokens { get; }
146141
public System.CommandLine.Completions.CompletionContext GetCompletionContext()
147142
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.Nullable<System.Int32> position = null)
143+
public System.CommandLine.Parsing.ArgumentResult GetResult(CliArgument argument)
144+
public System.CommandLine.Parsing.CommandResult GetResult(CliCommand command)
145+
public System.CommandLine.Parsing.OptionResult GetResult(CliOption option)
146+
public System.CommandLine.Parsing.DirectiveResult GetResult(CliDirective directive)
147+
public System.CommandLine.Parsing.SymbolResult GetResult(CliSymbol symbol)
148148
public T GetValue<T>(CliArgument<T> argument)
149149
public T GetValue<T>(CliOption<T> option)
150150
public T GetValue<T>(System.String name)
@@ -236,17 +236,33 @@ System.CommandLine.Parsing
236236
public static System.CommandLine.ParseResult Parse(System.CommandLine.CliCommand command, System.Collections.Generic.IReadOnlyList<System.String> args, System.CommandLine.CliConfiguration configuration = null)
237237
public static System.CommandLine.ParseResult Parse(System.CommandLine.CliCommand command, System.String commandLine, System.CommandLine.CliConfiguration configuration = null)
238238
public static System.Collections.Generic.IEnumerable<System.String> SplitCommandLine(System.String commandLine)
239+
public class CliToken, System.IEquatable<CliToken>
240+
public static System.Boolean op_Equality(CliToken left, CliToken right)
241+
public static System.Boolean op_Inequality(CliToken left, CliToken right)
242+
.ctor(System.String value, CliTokenType type, System.CommandLine.CliSymbol symbol)
243+
public CliTokenType Type { get; }
244+
public System.String Value { get; }
245+
public System.Boolean Equals(System.Object obj)
246+
public System.Boolean Equals(CliToken other)
247+
public System.Int32 GetHashCode()
248+
public System.String ToString()
249+
public enum CliTokenType : System.Enum, System.IComparable, System.IConvertible, System.IFormattable
250+
Argument=0
251+
Command=1
252+
Option=2
253+
DoubleDash=3
254+
Directive=4
239255
public class CommandResult : SymbolResult
240256
public System.Collections.Generic.IEnumerable<SymbolResult> Children { get; }
241257
public System.CommandLine.CliCommand Command { get; }
242-
public Token IdentifierToken { get; }
258+
public CliToken IdentifierToken { get; }
243259
public System.String ToString()
244260
public class DirectiveResult : SymbolResult
245261
public System.CommandLine.CliDirective Directive { get; }
246-
public Token Token { get; }
262+
public CliToken Token { get; }
247263
public System.Collections.Generic.IReadOnlyList<System.String> Values { get; }
248264
public class OptionResult : SymbolResult
249-
public Token IdentifierToken { get; }
265+
public CliToken IdentifierToken { get; }
250266
public System.Int32 IdentifierTokenCount { get; }
251267
public System.Boolean Implicit { get; }
252268
public System.CommandLine.CliOption Option { get; }
@@ -259,30 +275,14 @@ System.CommandLine.Parsing
259275
public abstract class SymbolResult
260276
public System.Collections.Generic.IEnumerable<ParseError> Errors { get; }
261277
public SymbolResult Parent { get; }
262-
public System.Collections.Generic.IReadOnlyList<Token> Tokens { get; }
278+
public System.Collections.Generic.IReadOnlyList<CliToken> Tokens { get; }
263279
public System.Void AddError(System.String errorMessage)
264-
public ArgumentResult FindResultFor(System.CommandLine.CliArgument argument)
265-
public CommandResult FindResultFor(System.CommandLine.CliCommand command)
266-
public OptionResult FindResultFor(System.CommandLine.CliOption option)
267-
public DirectiveResult FindResultFor(System.CommandLine.CliDirective directive)
280+
public ArgumentResult GetResult(System.CommandLine.CliArgument argument)
281+
public CommandResult GetResult(System.CommandLine.CliCommand command)
282+
public OptionResult GetResult(System.CommandLine.CliOption option)
283+
public DirectiveResult GetResult(System.CommandLine.CliDirective directive)
268284
public T GetValue<T>(CliArgument<T> argument)
269285
public T GetValue<T>(CliOption<T> option)
270-
public class Token, System.IEquatable<Token>
271-
public static System.Boolean op_Equality(Token left, Token right)
272-
public static System.Boolean op_Inequality(Token left, Token right)
273-
.ctor(System.String value, TokenType type, System.CommandLine.CliSymbol symbol)
274-
public TokenType Type { get; }
275-
public System.String Value { get; }
276-
public System.Boolean Equals(System.Object obj)
277-
public System.Boolean Equals(Token other)
278-
public System.Int32 GetHashCode()
279-
public System.String ToString()
280-
public enum TokenType : System.Enum, System.IComparable, System.IConvertible, System.IFormattable
281-
Argument=0
282-
Command=1
283-
Option=2
284-
DoubleDash=3
285-
Directive=4
286286
public delegate TryReplaceToken : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
287287
.ctor(System.Object object, System.IntPtr method)
288288
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)

src/System.CommandLine.Hosting/DirectiveConfigurationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static IConfigurationBuilder AddCommandLineDirectives(
1717
if (directive is null)
1818
throw new ArgumentNullException(nameof(directive));
1919

20-
if (commandline.FindResultFor(directive) is not DirectiveResult result
20+
if (commandline.GetResult(directive) is not DirectiveResult result
2121
|| result.Values.Count == 0)
2222
{
2323
return config;

src/System.CommandLine.Hosting/HostingAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override BindingContext GetBindingContext(ParseResult parseResult)
4141
public async override Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken = default)
4242
{
4343
var argsRemaining = parseResult.UnmatchedTokens;
44-
var hostBuilder = _hostBuilderFactory?.Invoke(argsRemaining)
44+
var hostBuilder = _hostBuilderFactory?.Invoke(argsRemaining.ToArray())
4545
?? new HostBuilder();
4646
hostBuilder.Properties[typeof(ParseResult)] = parseResult;
4747

src/System.CommandLine.NamingConventionBinder/BindingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal bool TryBindToScalarValue(
8989
else
9090
{
9191
ArgumentResult argumentResult = valueDescriptor is CliArgument argument
92-
? parseResult.FindResultFor(argument) is ArgumentResult found
92+
? parseResult.GetResult(argument) is ArgumentResult found
9393
? found
9494
: new ArgumentResult(argument, parseResult.RootCommandResult.SymbolResultTree, null)
9595
: new ArgumentResult(new CliArgument<string>(valueDescriptor.ValueName), parseResult.RootCommandResult.SymbolResultTree, null);

src/System.CommandLine.NamingConventionBinder/CommandResultExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static bool TryGetValueForArgument(
2020

2121
if (valueDescriptor.ValueName.IsMatch(RemovePrefix(argument.Name)))
2222
{
23-
if (commandResult.FindResultFor(argument) is { } argumentResult)
23+
if (commandResult.GetResult(argument) is { } argumentResult)
2424
{
2525
value = argumentResult.GetValueOrDefault<object>();
2626
}
@@ -53,7 +53,7 @@ internal static bool TryGetValueForOption(
5353

5454
if (hasMatchingAlias)
5555
{
56-
var optionResult = commandResult.FindResultFor(option);
56+
var optionResult = commandResult.GetResult(option);
5757

5858
if (optionResult is not null)
5959
{

src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public bool TryGetValue(IValueDescriptor valueDescriptor,
2121
switch (_symbol)
2222
{
2323
case CliOption option:
24-
var optionResult = bindingContext?.ParseResult.FindResultFor(option);
24+
var optionResult = bindingContext?.ParseResult.GetResult(option);
2525
if (optionResult is not null)
2626
{
2727
boundValue = optionResult.GetValueOrDefault<object>();
2828
return true;
2929
}
3030
break;
3131
case CliArgument argument:
32-
var argumentResult = bindingContext?.ParseResult.FindResultFor(argument);
32+
var argumentResult = bindingContext?.ParseResult.GetResult(argument);
3333
if (argumentResult is not null)
3434
{
3535
boundValue = argumentResult.GetValueOrDefault<object>();

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void Symbol_can_be_found_without_explicitly_traversing_result_tree(string
312312
{
313313
CustomParser = argResult =>
314314
{
315-
resultForOptionX = argResult.FindResultFor(optionX);
315+
resultForOptionX = argResult.GetResult(optionX);
316316
return string.Empty;
317317
}
318318
};
@@ -461,7 +461,7 @@ public void Custom_parser_can_check_another_option_result_for_custom_errors(stri
461461
{
462462
CustomParser = result =>
463463
{
464-
if (result.FindResultFor(optionWithError) is { } optionWithErrorResult)
464+
if (result.GetResult(optionWithError) is { } optionWithErrorResult)
465465
{
466466
var otherOptionError = optionWithErrorResult.Errors.SingleOrDefault()?.Message;
467467

@@ -654,13 +654,13 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
654654

655655
var parseResult = command.Parse(commandLine);
656656

657-
parseResult.FindResultFor(argument1)
657+
parseResult.GetResult(argument1)
658658
.GetValueOrDefault<int[]>()
659659
.Should()
660660
.BeEquivalentTo(new[] { 1, 2, 3 },
661661
options => options.WithStrictOrdering());
662662

663-
parseResult.FindResultFor(argument2)
663+
parseResult.GetResult(argument2)
664664
.GetValueOrDefault<int[]>()
665665
.Should()
666666
.BeEquivalentTo(new[] { 4, 5, 6, 7, 8 },
@@ -751,14 +751,14 @@ public void When_custom_parser_passes_on_tokens_the_argument_result_tokens_refle
751751

752752
var parseResult = command.Parse("1 2 3 4 5 6 7 8");
753753

754-
parseResult.FindResultFor(argument1)
754+
parseResult.GetResult(argument1)
755755
.Tokens
756756
.Select(t => t.Value)
757757
.Should()
758758
.BeEquivalentTo(new[] { "1", "2", "3" },
759759
options => options.WithStrictOrdering());
760760

761-
parseResult.FindResultFor(argument2)
761+
parseResult.GetResult(argument2)
762762
.Tokens
763763
.Select(t => t.Value)
764764
.Should()

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types(
688688
var result = command.Parse("--items one --items two --items three");
689689

690690
result.Errors.Should().BeEmpty();
691-
result.FindResultFor(option).GetValueOrDefault<object>().Should().BeAssignableTo(argumentType);
691+
result.GetResult(option).GetValueOrDefault<object>().Should().BeAssignableTo(argumentType);
692692
}
693693

694694
[Fact]

src/System.CommandLine.Tests/DirectiveTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Raw_tokens_still_hold_directives()
2929

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

32-
result.FindResultFor(directive).Should().NotBeNull();
32+
result.GetResult(directive).Should().NotBeNull();
3333
result.Tokens.Should().Contain(t => t.Value == "[parse]");
3434
}
3535

@@ -40,7 +40,7 @@ public void Directives_must_precede_other_symbols()
4040

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

43-
result.FindResultFor(directive).Should().BeNull();
43+
result.GetResult(directive).Should().BeNull();
4444
}
4545

4646
[Fact]
@@ -55,8 +55,8 @@ public void Multiple_directives_are_allowed()
5555

5656
var result = root.Parse("[parse] [suggest] -y", config);
5757

58-
result.FindResultFor(parseDirective).Should().NotBeNull();
59-
result.FindResultFor(suggestDirective).Should().NotBeNull();
58+
result.GetResult(parseDirective).Should().NotBeNull();
59+
result.GetResult(suggestDirective).Should().NotBeNull();
6060
}
6161

6262
[Theory]
@@ -179,7 +179,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo
179179

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

182-
result.FindResultFor(directive).Values.Single().Should().Be(expectedValue);
182+
result.GetResult(directive).Values.Single().Should().Be(expectedValue);
183183
}
184184

185185
[Fact]
@@ -189,7 +189,7 @@ public void Directives_without_a_value_specified_have_no_values()
189189

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

192-
result.FindResultFor(directive).Values.Should().BeEmpty();
192+
result.GetResult(directive).Values.Should().BeEmpty();
193193
}
194194

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

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

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

229229
ParseResult result = Parse(new CliOption<bool>("-a"), directive, "[directive:one] [directive:two] -a");
230230

231-
result.FindResultFor(directive).Values.Should().BeEquivalentTo("one", "two");
231+
result.GetResult(directive).Values.Should().BeEquivalentTo("one", "two");
232232
}
233233

234234
private static ParseResult Parse(CliOption option, CliDirective directive, string commandLine)

src/System.CommandLine.Tests/GetValueByNameParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void When_required_argument_value_is_not_parsed_then_an_exception_is_thro
130130
getRequired
131131
.Should()
132132
.Throw<InvalidOperationException>()
133-
.Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.FindResultFor(command.Arguments[0])));
133+
.Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.GetResult(command.Arguments[0])));
134134
}
135135

136136
[Fact]

0 commit comments

Comments
 (0)