Skip to content

remove non-generic GetValueOrDefault and fix the usages #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2023
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 @@ -332,7 +332,6 @@ System.CommandLine.Parsing
public class ArgumentResult : SymbolResult
public System.CommandLine.Argument Argument { get; }
public System.Void AddError(System.String errorMessage)
public System.Object GetValueOrDefault()
public T GetValueOrDefault<T>()
public System.Void OnlyTake(System.Int32 numberOfTokens)
public System.String ToString()
Expand All @@ -351,7 +350,6 @@ System.CommandLine.Parsing
public System.Boolean IsImplicit { get; }
public System.CommandLine.Option Option { get; }
public Token Token { get; }
public System.Object GetValueOrDefault()
public T GetValueOrDefault<T>()
public System.String ToString()
public class ParseError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static bool TryGetValueForArgument(
{
if (commandResult.FindResultFor(argument) is { } argumentResult)
{
value = argumentResult.GetValueOrDefault();
value = argumentResult.GetValueOrDefault<object>();
}
else
{
Expand Down Expand Up @@ -57,7 +57,7 @@ internal static bool TryGetValueForOption(

if (optionResult is not null)
{
value = optionResult.GetValueOrDefault();
value = optionResult.GetValueOrDefault<object>();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public bool TryGetValue(IValueDescriptor valueDescriptor,
var optionResult = bindingContext?.ParseResult.FindResultFor(option);
if (optionResult is not null)
{
boundValue = optionResult.GetValueOrDefault();
boundValue = optionResult.GetValueOrDefault<object>();
return true;
}
break;
case Argument argument:
var argumentResult = bindingContext?.ParseResult.FindResultFor(argument);
if (argumentResult is not null)
{
boundValue = argumentResult.GetValueOrDefault();
boundValue = argumentResult.GetValueOrDefault<object>();
return true;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,13 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
var parseResult = command.Parse(commandLine);

parseResult.FindResultFor(argument1)
.GetValueOrDefault()
.GetValueOrDefault<int[]>()
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 },
options => options.WithStrictOrdering());

parseResult.FindResultFor(argument2)
.GetValueOrDefault()
.GetValueOrDefault<int[]>()
.Should()
.BeEquivalentTo(new[] { 4, 5, 6, 7, 8 },
options => options.WithStrictOrdering());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types(
var result = command.Parse("--items one --items two --items three");

result.Errors.Should().BeEmpty();
result.FindResultFor(option).GetValueOrDefault().Should().BeAssignableTo(argumentType);
result.FindResultFor(option).GetValueOrDefault<object>().Should().BeAssignableTo(argumentType);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void Option_T_default_value_can_be_set_via_the_constructor()
new RootCommand { option }
.Parse("")
.FindResultFor(option)
.GetValueOrDefault()
.GetValueOrDefault<int>()
.Should()
.Be(123);
}
Expand All @@ -253,7 +253,7 @@ public void Option_T_default_value_can_be_set_after_instantiation()
new RootCommand { option }
.Parse("")
.FindResultFor(option)
.GetValueOrDefault()
.GetValueOrDefault<int>()
.Should()
.Be(123);
}
Expand All @@ -268,7 +268,7 @@ public void Option_T_default_value_factory_can_be_set_after_instantiation()
new RootCommand { option }
.Parse("")
.FindResultFor(option)
.GetValueOrDefault()
.GetValueOrDefault<int>()
.Should()
.Be(123);
}
Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ public void Multiple_arguments_of_unspecified_type_are_parsed_correctly()
var result = root.Parse("src.txt dest.txt");

result.FindResultFor(sourceArg)
.GetValueOrDefault()
.GetValueOrDefault<string>()
.Should()
.Be("src.txt");

result.FindResultFor(destinationArg)
.GetValueOrDefault()
.GetValueOrDefault<string>()
.Should()
.Be("dest.txt");
}
Expand Down
4 changes: 0 additions & 4 deletions src/System.CommandLine/Parsing/ArgumentResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ internal ArgumentResult(
internal ArgumentConversionResult GetArgumentConversionResult() =>
_conversionResult ??= ValidateAndConvert(useValidators: true);

/// <inheritdoc cref="GetValueOrDefault{T}"/>
public object? GetValueOrDefault() =>
GetValueOrDefault<object?>();

/// <summary>
/// Gets the parsed value or the default value for <see cref="Argument"/>.
/// </summary>
Expand Down
6 changes: 0 additions & 6 deletions src/System.CommandLine/Parsing/OptionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ internal OptionResult(
/// <inheritdoc/>
public override string ToString() => $"{nameof(OptionResult)}: {Token?.Value ?? Option.Name} {string.Join(" ", Tokens.Select(t => t.Value))}";

/// <inheritdoc cref="GetValueOrDefault{T}"/>
public object? GetValueOrDefault() =>
Option.ValueType == typeof(bool)
? GetValueOrDefault<bool>()
: GetValueOrDefault<object?>();

/// <summary>
/// Gets the parsed value or the default value for <see cref="Option"/>.
/// </summary>
Expand Down