Skip to content

Commit 6462288

Browse files
authored
Remove non generic GetValue overloads (#2059)
* remove non-generic GetValue overloads * fix nullable annotations for GetValue<T>(Argument<T>)
1 parent 5c362ae commit 6462288

File tree

6 files changed

+12
-105
lines changed

6 files changed

+12
-105
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ System.CommandLine
224224
public System.CommandLine.Parsing.SymbolResult FindResultFor(Symbol symbol)
225225
public System.CommandLine.Completions.CompletionContext GetCompletionContext()
226226
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.Nullable<System.Int32> position = null)
227-
public System.Object GetValue(Option option)
228-
public System.Object GetValue(Argument argument)
229227
public T GetValue<T>(Argument<T> argument)
230228
public T GetValue<T>(Option<T> option)
231229
public System.String ToString()
@@ -334,9 +332,7 @@ System.CommandLine.Invocation
334332
public System.CommandLine.LocalizationResources LocalizationResources { get; }
335333
public System.CommandLine.Parsing.Parser Parser { get; }
336334
public System.CommandLine.ParseResult ParseResult { get; set; }
337-
public System.Object GetValue(System.CommandLine.Option option)
338335
public T GetValue<T>(Option<T> option)
339-
public System.Object GetValue(System.CommandLine.Argument argument)
340336
public T GetValue<T>(Argument<T> argument)
341337
public delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
342338
.ctor(System.Object object, System.IntPtr method)
@@ -432,9 +428,7 @@ System.CommandLine.Parsing
432428
public CommandResult FindResultFor(System.CommandLine.Command command)
433429
public OptionResult FindResultFor(System.CommandLine.Option option)
434430
public T GetValue<T>(Argument<T> argument)
435-
public System.Object GetValue(System.CommandLine.Argument argument)
436431
public T GetValue<T>(Option<T> option)
437-
public System.Object GetValue(System.CommandLine.Option option)
438432
public System.String ToString()
439433
public class Token, System.IEquatable<Token>
440434
public static System.Boolean op_Equality(Token left, Token right)

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -790,22 +790,6 @@ public void InvocationContext_GetValue_with_generic_option_returns_value()
790790
.Be(42);
791791
}
792792

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-
809793
[Fact]
810794
public void InvocationContext_GetValue_with_generic_argument_returns_value()
811795
{
@@ -822,22 +806,6 @@ public void InvocationContext_GetValue_with_generic_argument_returns_value()
822806
.Be(42);
823807
}
824808

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-
841809
class DeployOptions
842810
{
843811
public string Bundle { get; set; }

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,6 @@ public void Nullable_bool_parses_as_null_when_the_option_has_not_been_applied()
238238
.Be(null);
239239
}
240240

241-
[Fact] // https://github.com/dotnet/command-line-api/issues/1647
242-
public void Generic_option_bool_parses_when_passed_to_non_generic_GetValueForOption()
243-
{
244-
var option = new Option<bool>("-b");
245-
246-
var cmd = new RootCommand
247-
{
248-
option
249-
};
250-
251-
var parseResult = cmd.Parse("-b");
252-
253-
parseResult.GetValue((Option)option).Should().Be(true);
254-
}
255-
256241
[Fact]
257242
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws()
258243
{

src/System.CommandLine/Invocation/InvocationContext.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,12 @@ public IConsole Console
7070
/// <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>
7171
public Action<InvocationContext>? InvocationResult { get; set; }
7272

73-
/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
74-
public object? GetValue(Option option) =>
75-
ParseResult.GetValue(option);
76-
77-
/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
73+
/// <inheritdoc cref="ParseResult.GetValue{T}(Option{T})"/>
7874
public T? GetValue<T>(Option<T> option)
7975
=> ParseResult.GetValue(option);
8076

81-
/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
82-
public object? GetValue(Argument argument) =>
83-
ParseResult.GetValue(argument);
84-
85-
/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
86-
public T GetValue<T>(Argument<T> argument)
77+
/// <inheritdoc cref="ParseResult.GetValue{T}(Argument{T})"/>
78+
public T? GetValue<T>(Argument<T> argument)
8779
=> ParseResult.GetValue(argument);
8880
}
8981
}

src/System.CommandLine/ParseResult.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,18 @@ CommandLineText is null
120120
};
121121

122122
/// <summary>
123-
/// Gets the parsed or default value for the specified option.
123+
/// Gets the parsed or default value for the specified argument.
124124
/// </summary>
125-
/// <param name="option">The option for which to get a value.</param>
125+
/// <param name="argument">The argument for which to get a value.</param>
126126
/// <returns>The parsed value or a configured default.</returns>
127-
public object? GetValue(Option option) =>
128-
RootCommandResult.GetValue(option);
127+
public T? GetValue<T>(Argument<T> argument)
128+
=> RootCommandResult.GetValue(argument);
129129

130130
/// <summary>
131-
/// Gets the parsed or default value for the specified argument.
131+
/// Gets the parsed or default value for the specified option.
132132
/// </summary>
133-
/// <param name="argument">The argument for which to get a value.</param>
133+
/// <param name="option">The option for which to get a value.</param>
134134
/// <returns>The parsed value or a configured default.</returns>
135-
public object? GetValue(Argument argument) =>
136-
RootCommandResult.GetValue(argument);
137-
138-
/// <inheritdoc cref="GetValue(Argument)"/>
139-
public T GetValue<T>(Argument<T> argument)
140-
=> RootCommandResult.GetValue(argument);
141-
142-
/// <inheritdoc cref="GetValue(Option)"/>
143135
public T? GetValue<T>(Option<T> option)
144136
=> RootCommandResult.GetValue(option);
145137

src/System.CommandLine/Parsing/SymbolResult.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ private protected SymbolResult(SymbolResultTree symbolResultTree, SymbolResult?
6565
/// <returns>An option result if the option was matched by the parser or has a default value; otherwise, <c>null</c>.</returns>
6666
public OptionResult? FindResultFor(Option option) => SymbolResultTree.FindResultFor(option);
6767

68-
/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
69-
public T GetValue<T>(Argument<T> argument)
68+
/// <inheritdoc cref="ParseResult.GetValue{T}(Argument{T})"/>
69+
public T? GetValue<T>(Argument<T> argument)
7070
{
7171
if (FindResultFor(argument) is { } result &&
7272
result.GetValueOrDefault<T>() is { } t)
@@ -77,19 +77,7 @@ public T GetValue<T>(Argument<T> argument)
7777
return (T)ArgumentConverter.GetDefaultValue(argument.ValueType)!;
7878
}
7979

80-
/// <inheritdoc cref="ParseResult.GetValue(Argument)"/>
81-
public object? GetValue(Argument argument)
82-
{
83-
if (FindResultFor(argument) is { } result &&
84-
result.GetValueOrDefault<object?>() is { } t)
85-
{
86-
return t;
87-
}
88-
89-
return ArgumentConverter.GetDefaultValue(argument.ValueType);
90-
}
91-
92-
/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
80+
/// <inheritdoc cref="ParseResult.GetValue{T}(Option{T})"/>
9381
public T? GetValue<T>(Option<T> option)
9482
{
9583
if (FindResultFor(option) is { } result &&
@@ -101,18 +89,6 @@ public T GetValue<T>(Argument<T> argument)
10189
return (T)ArgumentConverter.GetDefaultValue(option.Argument.ValueType)!;
10290
}
10391

104-
/// <inheritdoc cref="ParseResult.GetValue(Option)"/>
105-
public object? GetValue(Option option)
106-
{
107-
if (FindResultFor(option) is { } result &&
108-
result.GetValueOrDefault<object?>() is { } t)
109-
{
110-
return t;
111-
}
112-
113-
return ArgumentConverter.GetDefaultValue(option.Argument.ValueType);
114-
}
115-
11692
internal virtual bool UseDefaultValueFor(ArgumentResult argumentResult) => false;
11793

11894
/// <inheritdoc/>

0 commit comments

Comments
 (0)