Skip to content

Commit b8ddb00

Browse files
Keboojonsequitur
authored andcommitted
Rename members to GetValue
Added GetValue methods to InvocationContext Renamed GetValueForOption and GetValueForArgument on ParseResult and SymbolResult classes Fixing compilation tests
1 parent ff9e8f7 commit b8ddb00

File tree

24 files changed

+290
-206
lines changed

24 files changed

+290
-206
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ System.CommandLine
241241
public System.CommandLine.Parsing.SymbolResult FindResultFor(Symbol symbol)
242242
public System.CommandLine.Completions.CompletionContext GetCompletionContext()
243243
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.Nullable<System.Int32> position = null)
244-
public System.Object GetValueForArgument(Argument argument)
245-
public T GetValueForArgument<T>(Argument<T> argument)
246-
public System.Object GetValueForOption(Option option)
247-
public T GetValueForOption<T>(Option<T> option)
244+
public System.Object GetValue(Option option)
245+
public System.Object GetValue(Argument argument)
246+
public T GetValue<T>(Argument<T> argument)
247+
public T GetValue<T>(Option<T> option)
248248
public System.String ToString()
249249
public class RootCommand : Command, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable, System.CommandLine.Completions.ICompletionSource
250250
public static System.String ExecutableName { get; }
@@ -366,6 +366,10 @@ System.CommandLine.Invocation
366366
public System.CommandLine.Parsing.Parser Parser { get; }
367367
public System.CommandLine.ParseResult ParseResult { get; set; }
368368
public System.Threading.CancellationToken GetCancellationToken()
369+
public System.Object GetValue(System.CommandLine.Option option)
370+
public T GetValue<T>(Option<T> option)
371+
public System.Object GetValue(System.CommandLine.Argument argument)
372+
public T GetValue<T>(Argument<T> argument)
369373
public System.Void LinkToken(System.Threading.CancellationToken token)
370374
public delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
371375
.ctor(System.Object object, System.IntPtr method)
@@ -466,10 +470,10 @@ System.CommandLine.Parsing
466470
public ArgumentResult FindResultFor(System.CommandLine.Argument argument)
467471
public CommandResult FindResultFor(System.CommandLine.Command command)
468472
public OptionResult FindResultFor(System.CommandLine.Option option)
469-
public T GetValueForArgument<T>(Argument<T> argument)
470-
public System.Object GetValueForArgument(System.CommandLine.Argument argument)
471-
public T GetValueForOption<T>(Option<T> option)
472-
public System.Object GetValueForOption(System.CommandLine.Option option)
473+
public T GetValue<T>(Argument<T> argument)
474+
public System.Object GetValue(System.CommandLine.Argument argument)
475+
public T GetValue<T>(Option<T> option)
476+
public System.Object GetValue(System.CommandLine.Option option)
473477
public System.String ToString()
474478
public class Token, System.IEquatable<Token>
475479
public static System.Boolean op_Equality(Token left, Token right)

src/System.CommandLine.Generator/Parameters/ArgumentParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public ArgumentParameter(string localName, INamedTypeSymbol type, ITypeSymbol va
1010
}
1111

1212
public override string GetValueFromContext()
13-
=> $"context.ParseResult.GetValueForArgument({LocalName})";
13+
=> $"context.ParseResult.GetValue({LocalName})";
1414

1515
public override int GetHashCode()
1616
=> base.GetHashCode();

src/System.CommandLine.Generator/Parameters/OptionParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public OptionParameter(string localName, INamedTypeSymbol type, ITypeSymbol valu
1111
}
1212

1313
public override string GetValueFromContext()
14-
=> $"context.ParseResult.GetValueForOption({LocalName})";
14+
=> $"context.ParseResult.GetValue({LocalName})";
1515

1616
public override int GetHashCode()
1717
=> base.GetHashCode();

src/System.CommandLine.NamingConventionBinder.Tests/ModelBinderTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,70 @@ public void Binder_does_not_match_by_substring()
774774
boundOptions.BundleId.Should().Be("value");
775775
}
776776

777+
[Fact]
778+
public void InvocationContext_GetValue_with_generic_option_returns_value()
779+
{
780+
Option<int> option = new("--number");
781+
Command command = new("the-command")
782+
{
783+
option
784+
};
785+
786+
InvocationContext invocationContext = new(command.Parse("the-command --number 42"));
787+
788+
invocationContext.GetValue(option)
789+
.Should()
790+
.Be(42);
791+
}
792+
793+
[Fact]
794+
public void InvocationContext_GetValue_with_non_generic_option_returns_value()
795+
{
796+
Option option = new Option<int>("--number");
797+
Command command = new("the-command")
798+
{
799+
option
800+
};
801+
802+
InvocationContext invocationContext = new(command.Parse("the-command --number 42"));
803+
804+
invocationContext.GetValue(option)
805+
.Should()
806+
.Be(42);
807+
}
808+
809+
[Fact]
810+
public void InvocationContext_GetValue_with_generic_argument_returns_value()
811+
{
812+
Argument<int> option = new();
813+
Command command = new("the-command")
814+
{
815+
option
816+
};
817+
818+
InvocationContext invocationContext = new(command.Parse("the-command 42"));
819+
820+
invocationContext.GetValue(option)
821+
.Should()
822+
.Be(42);
823+
}
824+
825+
[Fact]
826+
public void InvocationContext_GetValue_with_non_generic_argument_returns_value()
827+
{
828+
Argument option = new Argument<int>();
829+
Command command = new("the-command")
830+
{
831+
option
832+
};
833+
834+
InvocationContext invocationContext = new(command.Parse("the-command 42"));
835+
836+
invocationContext.GetValue(option)
837+
.Should()
838+
.Be(42);
839+
}
840+
777841
class DeployOptions
778842
{
779843
public string Bundle { get; set; }

src/System.CommandLine.NamingConventionBinder.Tests/ParameterBindingTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Pars
233233

234234
await command.InvokeAsync("command -x 123", _console);
235235

236-
boundParseResult.GetValueForOption(option).Should().Be(123);
236+
boundParseResult.GetValue(option).Should().Be(123);
237237
}
238238

239239
[Fact]
@@ -250,7 +250,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Bind
250250

251251
await command.InvokeAsync("command -x 123", _console);
252252

253-
boundContext.ParseResult.GetValueForOption(option).Should().Be(123);
253+
boundContext.ParseResult.GetValue(option).Should().Be(123);
254254
}
255255

256256
[Fact]
@@ -282,7 +282,7 @@ public async Task Method_parameters_of_type_InvocationContext_receive_the_curren
282282

283283
await command.InvokeAsync("command -x 123", _console);
284284

285-
boundContext.ParseResult.GetValueForOption(option).Should().Be(123);
285+
boundContext.ParseResult.GetValue(option).Should().Be(123);
286286
}
287287

288288
private class ExecuteTestClass

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
3333
};
3434
CompleteScriptCommand.SetHandler(context =>
3535
{
36-
SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument));
36+
SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValue(shellTypeArgument));
3737
});
3838

3939
ListCommand = new Command("list")
@@ -63,7 +63,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
6363

6464
RegisterCommand.SetHandler(context =>
6565
{
66-
Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console);
66+
Register(context.ParseResult.GetValue(commandPathOption), context.Console);
6767
return Task.FromResult(0);
6868
});
6969

@@ -130,7 +130,7 @@ private void Register(
130130
private Task<int> Get(InvocationContext context)
131131
{
132132
var parseResult = context.ParseResult;
133-
var commandPath = parseResult.GetValueForOption(ExecutableOption);
133+
var commandPath = parseResult.GetValue(ExecutableOption);
134134

135135
Registration suggestionRegistration;
136136
if (commandPath.FullName == DotnetMuxer.Path.FullName)
@@ -142,7 +142,7 @@ private Task<int> Get(InvocationContext context)
142142
suggestionRegistration = _suggestionRegistration.FindRegistration(commandPath);
143143
}
144144

145-
var position = parseResult.GetValueForOption(PositionOption);
145+
var position = parseResult.GetValue(PositionOption);
146146

147147
if (suggestionRegistration is null)
148148
{

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_one_token()
183183
var argument = new Argument<int>(result => int.Parse(result.Tokens.Single().Value));
184184

185185
argument.Parse("123")
186-
.GetValueForArgument(argument)
186+
.GetValue(argument)
187187
.Should()
188188
.Be(123);
189189
}
@@ -194,7 +194,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_one_token()
194194
var argument = new Argument<IEnumerable<int>>(result => result.Tokens.Single().Value.Split(',').Select(int.Parse));
195195

196196
argument.Parse("1,2,3")
197-
.GetValueForArgument(argument)
197+
.GetValue(argument)
198198
.Should()
199199
.BeEquivalentTo(new[] { 1, 2, 3 });
200200
}
@@ -208,7 +208,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke
208208
});
209209

210210
argument.Parse("1 2 3")
211-
.GetValueForArgument(argument)
211+
.GetValue(argument)
212212
.Should()
213213
.BeEquivalentTo(new[] { 1, 2, 3 });
214214
}
@@ -222,7 +222,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens
222222
};
223223

224224
argument.Parse("1 2 3")
225-
.GetValueForArgument(argument)
225+
.GetValue(argument)
226226
.Should()
227227
.Be(6);
228228
}
@@ -367,7 +367,7 @@ public void Default_value_and_custom_argument_parser_can_be_used_together()
367367

368368
var result = argument.Parse("");
369369

370-
result.GetValueForArgument(argument)
370+
result.GetValue(argument)
371371
.Should()
372372
.Be(123);
373373
}
@@ -449,7 +449,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
449449
var result = command.Parse("the-command -o not-an-int");
450450

451451
Action getValue = () =>
452-
result.GetValueForOption(option);
452+
result.GetValue(option);
453453

454454
getValue.Should()
455455
.Throw<InvalidOperationException>()
@@ -511,7 +511,7 @@ public void Parse_delegate_is_called_when_Option_Arity_allows_zero_tokens(string
511511
opt
512512
};
513513

514-
rootCommand.Parse(commandLine).GetValueForOption(opt).Should().Be(expectedValue);
514+
rootCommand.Parse(commandLine).GetValue(opt).Should().Be(expectedValue);
515515
}
516516

517517
[Theory]
@@ -691,9 +691,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_one_multiple_arity_argument_to_
691691

692692
var result = command.Parse("1 2 3");
693693

694-
result.GetValueForArgument(argument1).Should().BeEmpty();
694+
result.GetValue(argument1).Should().BeEmpty();
695695

696-
result.GetValueForArgument(argument2).Should().BeEquivalentSequenceTo(1, 2, 3);
696+
result.GetValue(argument2).Should().BeEquivalentSequenceTo(1, 2, 3);
697697
}
698698

699699
[Fact] // https://github.com/dotnet/command-line-api/issues/1759
@@ -714,9 +714,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
714714

715715
var result = command.Parse("1 2 3");
716716

717-
result.GetValueForArgument(scalar).Should().BeNull();
717+
result.GetValue(scalar).Should().BeNull();
718718

719-
result.GetValueForArgument(multiple).Should().BeEquivalentSequenceTo(1, 2, 3);
719+
result.GetValue(multiple).Should().BeEquivalentSequenceTo(1, 2, 3);
720720
}
721721

722722

@@ -759,9 +759,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
759759

760760
var result = command.Parse("1 2 3");
761761

762-
result.GetValueForArgument(first).Should().BeNull();
763-
result.GetValueForArgument(second).Should().BeEmpty();
764-
result.GetValueForArgument(third).Should().BeEquivalentSequenceTo("1", "2", "3");
762+
result.GetValue(first).Should().BeNull();
763+
result.GetValue(second).Should().BeEmpty();
764+
result.GetValue(third).Should().BeEquivalentSequenceTo("1", "2", "3");
765765
}
766766
}
767767

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ protected override CustomType GetBoundValue(BindingContext bindingContext)
6363
return new CustomType
6464
{
6565
Console = bindingContext.Console,
66-
IntValue = bindingContext.ParseResult.GetValueForOption(_intOption),
67-
StringValue = bindingContext.ParseResult.GetValueForArgument(_stringArg),
66+
IntValue = bindingContext.ParseResult.GetValue(_intOption),
67+
StringValue = bindingContext.ParseResult.GetValue(_stringArg),
6868
};
6969
}
7070
}

0 commit comments

Comments
 (0)