Skip to content

Commit

Permalink
Rename members to GetValue
Browse files Browse the repository at this point in the history
Added GetValue methods to InvocationContext
Renamed GetValueForOption and GetValueForArgument on ParseResult and SymbolResult classes
Fixing compilation tests
  • Loading branch information
Keboo committed Oct 29, 2022
1 parent ff9e8f7 commit 1fd1179
Show file tree
Hide file tree
Showing 24 changed files with 290 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ System.CommandLine
public System.CommandLine.Parsing.SymbolResult FindResultFor(Symbol symbol)
public System.CommandLine.Completions.CompletionContext GetCompletionContext()
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.Nullable<System.Int32> position = null)
public System.Object GetValueForArgument(Argument argument)
public T GetValueForArgument<T>(Argument<T> argument)
public System.Object GetValueForOption(Option option)
public T GetValueForOption<T>(Option<T> option)
public System.Object GetValue(Option option)
public System.Object GetValue(Argument argument)
public T GetValue<T>(Argument<T> argument)
public T GetValue<T>(Option<T> option)
public System.String ToString()
public class RootCommand : Command, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable, System.CommandLine.Completions.ICompletionSource
public static System.String ExecutableName { get; }
Expand Down Expand Up @@ -366,6 +366,10 @@ System.CommandLine.Invocation
public System.CommandLine.Parsing.Parser Parser { get; }
public System.CommandLine.ParseResult ParseResult { get; set; }
public System.Threading.CancellationToken GetCancellationToken()
public System.Object GetValue(System.CommandLine.Option option)
public T GetValue<T>(Option<T> option)
public System.Object GetValue(System.CommandLine.Argument argument)
public T GetValue<T>(Argument<T> argument)
public System.Void LinkToken(System.Threading.CancellationToken token)
public delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
.ctor(System.Object object, System.IntPtr method)
Expand Down Expand Up @@ -466,10 +470,10 @@ System.CommandLine.Parsing
public ArgumentResult FindResultFor(System.CommandLine.Argument argument)
public CommandResult FindResultFor(System.CommandLine.Command command)
public OptionResult FindResultFor(System.CommandLine.Option option)
public T GetValueForArgument<T>(Argument<T> argument)
public System.Object GetValueForArgument(System.CommandLine.Argument argument)
public T GetValueForOption<T>(Option<T> option)
public System.Object GetValueForOption(System.CommandLine.Option option)
public T GetValue<T>(Argument<T> argument)
public System.Object GetValue(System.CommandLine.Argument argument)
public T GetValue<T>(Option<T> option)
public System.Object GetValue(System.CommandLine.Option option)
public System.String ToString()
public class Token, System.IEquatable<Token>
public static System.Boolean op_Equality(Token left, Token right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ArgumentParameter(string localName, INamedTypeSymbol type, ITypeSymbol va
}

public override string GetValueFromContext()
=> $"context.ParseResult.GetValueForArgument({LocalName})";
=> $"context.ParseResult.GetValue({LocalName})";

public override int GetHashCode()
=> base.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public OptionParameter(string localName, INamedTypeSymbol type, ITypeSymbol valu
}

public override string GetValueFromContext()
=> $"context.ParseResult.GetValueForOption({LocalName})";
=> $"context.ParseResult.GetValue({LocalName})";

public override int GetHashCode()
=> base.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,70 @@ public void Binder_does_not_match_by_substring()
boundOptions.BundleId.Should().Be("value");
}

[Fact]
public void InvocationContext_GetValue_with_generic_option_returns_value()
{
Option<int> option = new("--number");
Command command = new("the-command")
{
option
};

InvocationContext invocationContext = new(command.Parse("the-command --number 42"));

invocationContext.GetValue(option)
.Should()
.Be(42);
}

[Fact]
public void InvocationContext_GetValue_with_non_generic_option_returns_value()
{
Option option = new Option<int>("--number");
Command command = new("the-command")
{
option
};

InvocationContext invocationContext = new(command.Parse("the-command --number 42"));

invocationContext.GetValue(option)
.Should()
.Be(42);
}

[Fact]
public void InvocationContext_GetValue_with_generic_argument_returns_value()
{
Argument<int> option = new();
Command command = new("the-command")
{
option
};

InvocationContext invocationContext = new(command.Parse("the-command 42"));

invocationContext.GetValue(option)
.Should()
.Be(42);
}

[Fact]
public void InvocationContext_GetValue_with_non_generic_argument_returns_value()
{
Argument option = new Argument<int>();
Command command = new("the-command")
{
option
};

InvocationContext invocationContext = new(command.Parse("the-command 42"));

invocationContext.GetValue(option)
.Should()
.Be(42);
}

class DeployOptions
{
public string Bundle { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Pars

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

boundParseResult.GetValueForOption(option).Should().Be(123);
boundParseResult.GetValue(option).Should().Be(123);
}

[Fact]
Expand All @@ -250,7 +250,7 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Bind

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

boundContext.ParseResult.GetValueForOption(option).Should().Be(123);
boundContext.ParseResult.GetValue(option).Should().Be(123);
}

[Fact]
Expand Down Expand Up @@ -282,7 +282,7 @@ public async Task Method_parameters_of_type_InvocationContext_receive_the_curren

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

boundContext.ParseResult.GetValueForOption(option).Should().Be(123);
boundContext.ParseResult.GetValue(option).Should().Be(123);
}

private class ExecuteTestClass
Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Suggest/SuggestionDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
};
CompleteScriptCommand.SetHandler(context =>
{
SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument));
SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValue(shellTypeArgument));
});

ListCommand = new Command("list")
Expand Down Expand Up @@ -63,7 +63,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug

RegisterCommand.SetHandler(context =>
{
Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console);
Register(context.ParseResult.GetValue(commandPathOption), context.Console);
return Task.FromResult(0);
});

Expand Down Expand Up @@ -130,7 +130,7 @@ private void Register(
private Task<int> Get(InvocationContext context)
{
var parseResult = context.ParseResult;
var commandPath = parseResult.GetValueForOption(ExecutableOption);
var commandPath = parseResult.GetValue(ExecutableOption);

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

var position = parseResult.GetValueForOption(PositionOption);
var position = parseResult.GetValue(PositionOption);

if (suggestionRegistration is null)
{
Expand Down
28 changes: 14 additions & 14 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_one_token()
var argument = new Argument<int>(result => int.Parse(result.Tokens.Single().Value));

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

argument.Parse("1,2,3")
.GetValueForArgument(argument)
.GetValue(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
}
Expand All @@ -208,7 +208,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke
});

argument.Parse("1 2 3")
.GetValueForArgument(argument)
.GetValue(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
}
Expand All @@ -222,7 +222,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens
};

argument.Parse("1 2 3")
.GetValueForArgument(argument)
.GetValue(argument)
.Should()
.Be(6);
}
Expand Down Expand Up @@ -367,7 +367,7 @@ public void Default_value_and_custom_argument_parser_can_be_used_together()

var result = argument.Parse("");

result.GetValueForArgument(argument)
result.GetValue(argument)
.Should()
.Be(123);
}
Expand Down Expand Up @@ -449,7 +449,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
var result = command.Parse("the-command -o not-an-int");

Action getValue = () =>
result.GetValueForOption(option);
result.GetValue(option);

getValue.Should()
.Throw<InvalidOperationException>()
Expand Down Expand Up @@ -511,7 +511,7 @@ public void Parse_delegate_is_called_when_Option_Arity_allows_zero_tokens(string
opt
};

rootCommand.Parse(commandLine).GetValueForOption(opt).Should().Be(expectedValue);
rootCommand.Parse(commandLine).GetValue(opt).Should().Be(expectedValue);
}

[Theory]
Expand Down Expand Up @@ -691,9 +691,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_one_multiple_arity_argument_to_

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

result.GetValueForArgument(argument1).Should().BeEmpty();
result.GetValue(argument1).Should().BeEmpty();

result.GetValueForArgument(argument2).Should().BeEquivalentSequenceTo(1, 2, 3);
result.GetValue(argument2).Should().BeEquivalentSequenceTo(1, 2, 3);
}

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

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

result.GetValueForArgument(scalar).Should().BeNull();
result.GetValue(scalar).Should().BeNull();

result.GetValueForArgument(multiple).Should().BeEquivalentSequenceTo(1, 2, 3);
result.GetValue(multiple).Should().BeEquivalentSequenceTo(1, 2, 3);
}


Expand Down Expand Up @@ -759,9 +759,9 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot

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

result.GetValueForArgument(first).Should().BeNull();
result.GetValueForArgument(second).Should().BeEmpty();
result.GetValueForArgument(third).Should().BeEquivalentSequenceTo("1", "2", "3");
result.GetValue(first).Should().BeNull();
result.GetValue(second).Should().BeEmpty();
result.GetValue(third).Should().BeEquivalentSequenceTo("1", "2", "3");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/Binding/SetHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ protected override CustomType GetBoundValue(BindingContext bindingContext)
return new CustomType
{
Console = bindingContext.Console,
IntValue = bindingContext.ParseResult.GetValueForOption(_intOption),
StringValue = bindingContext.ParseResult.GetValueForArgument(_stringArg),
IntValue = bindingContext.ParseResult.GetValue(_intOption),
StringValue = bindingContext.ParseResult.GetValue(_stringArg),
};
}
}
Expand Down
Loading

0 comments on commit 1fd1179

Please sign in to comment.