Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ 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 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()
Expand Down Expand Up @@ -334,9 +332,7 @@ System.CommandLine.Invocation
public System.CommandLine.LocalizationResources LocalizationResources { get; }
public System.CommandLine.Parsing.Parser Parser { get; }
public System.CommandLine.ParseResult ParseResult { get; set; }
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 delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
.ctor(System.Object object, System.IntPtr method)
Expand Down Expand Up @@ -432,9 +428,7 @@ System.CommandLine.Parsing
public CommandResult FindResultFor(System.CommandLine.Command command)
public OptionResult FindResultFor(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 @@ -790,22 +790,6 @@ public void InvocationContext_GetValue_with_generic_option_returns_value()
.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()
{
Expand All @@ -822,22 +806,6 @@ public void InvocationContext_GetValue_with_generic_argument_returns_value()
.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
15 changes: 0 additions & 15 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,6 @@ public void Nullable_bool_parses_as_null_when_the_option_has_not_been_applied()
.Be(null);
}

[Fact] // https://github.com/dotnet/command-line-api/issues/1647
public void Generic_option_bool_parses_when_passed_to_non_generic_GetValueForOption()
{
var option = new Option<bool>("-b");

var cmd = new RootCommand
{
option
};

var parseResult = cmd.Parse("-b");

parseResult.GetValue((Option)option).Should().Be(true);
}

[Fact]
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws()
{
Expand Down
14 changes: 3 additions & 11 deletions src/System.CommandLine/Invocation/InvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,12 @@ public IConsole Console
/// <remarks>As the <see cref="InvocationContext"/> is passed through the invocation pipeline to the <see cref="ICommandHandler"/> associated with the invoked command, only the last value of this property will be the one applied.</remarks>
public Action<InvocationContext>? InvocationResult { get; set; }

/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
public object? GetValue(Option option) =>
ParseResult.GetValue(option);

/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
/// <inheritdoc cref="ParseResult.GetValue{T}(Option{T})"/>
public T? GetValue<T>(Option<T> option)
=> ParseResult.GetValue(option);

/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
public object? GetValue(Argument argument) =>
ParseResult.GetValue(argument);

/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
public T GetValue<T>(Argument<T> argument)
/// <inheritdoc cref="ParseResult.GetValue{T}(Argument{T})"/>
public T? GetValue<T>(Argument<T> argument)
=> ParseResult.GetValue(argument);
}
}
20 changes: 6 additions & 14 deletions src/System.CommandLine/ParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,18 @@ CommandLineText is null
};

/// <summary>
/// Gets the parsed or default value for the specified option.
/// Gets the parsed or default value for the specified argument.
/// </summary>
/// <param name="option">The option for which to get a value.</param>
/// <param name="argument">The argument for which to get a value.</param>
/// <returns>The parsed value or a configured default.</returns>
public object? GetValue(Option option) =>
RootCommandResult.GetValue(option);
public T? GetValue<T>(Argument<T> argument)
=> RootCommandResult.GetValue(argument);

/// <summary>
/// Gets the parsed or default value for the specified argument.
/// Gets the parsed or default value for the specified option.
/// </summary>
/// <param name="argument">The argument for which to get a value.</param>
/// <param name="option">The option for which to get a value.</param>
/// <returns>The parsed value or a configured default.</returns>
public object? GetValue(Argument argument) =>
RootCommandResult.GetValue(argument);

/// <inheritdoc cref="GetValue(Argument)"/>
public T GetValue<T>(Argument<T> argument)
=> RootCommandResult.GetValue(argument);

/// <inheritdoc cref="GetValue(Option)"/>
public T? GetValue<T>(Option<T> option)
=> RootCommandResult.GetValue(option);

Expand Down
30 changes: 3 additions & 27 deletions src/System.CommandLine/Parsing/SymbolResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private protected SymbolResult(SymbolResultTree symbolResultTree, SymbolResult?
/// <returns>An option result if the option was matched by the parser or has a default value; otherwise, <c>null</c>.</returns>
public OptionResult? FindResultFor(Option option) => SymbolResultTree.FindResultFor(option);

/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
public T GetValue<T>(Argument<T> argument)
/// <inheritdoc cref="ParseResult.GetValue{T}(Argument{T})"/>
public T? GetValue<T>(Argument<T> argument)
{
if (FindResultFor(argument) is { } result &&
result.GetValueOrDefault<T>() is { } t)
Expand All @@ -77,19 +77,7 @@ public T GetValue<T>(Argument<T> argument)
return (T)ArgumentConverter.GetDefaultValue(argument.ValueType)!;
}

/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
public object? GetValue(Argument argument)
{
if (FindResultFor(argument) is { } result &&
result.GetValueOrDefault<object?>() is { } t)
{
return t;
}

return ArgumentConverter.GetDefaultValue(argument.ValueType);
}

/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
/// <inheritdoc cref="ParseResult.GetValue{T}(Option{T})"/>
public T? GetValue<T>(Option<T> option)
{
if (FindResultFor(option) is { } result &&
Expand All @@ -101,18 +89,6 @@ public T GetValue<T>(Argument<T> argument)
return (T)ArgumentConverter.GetDefaultValue(option.Argument.ValueType)!;
}

/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
public object? GetValue(Option option)
{
if (FindResultFor(option) is { } result &&
result.GetValueOrDefault<object?>() is { } t)
{
return t;
}

return ArgumentConverter.GetDefaultValue(option.Argument.ValueType);
}

internal virtual bool UseDefaultValueFor(ArgumentResult argumentResult) => false;

/// <inheritdoc/>
Expand Down