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
25 changes: 25 additions & 0 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using Xunit;

namespace System.CommandLine.Tests.Binding
Expand Down Expand Up @@ -195,6 +196,30 @@ public void Bool_parses_as_true_when_the_option_has_been_applied(string commandL
.BeTrue();
}

[Fact] // https://github.com/dotnet/command-line-api/issues/2210
public void Nullable_bool_with_unparseable_argument_does_not_throw()
{
CliRootCommand rootCommand = new();
CliOption<bool?> option = new("--test");
rootCommand.Options.Add(option);
var result = rootCommand.Parse("--test ouch");

result.Invoking(r => r.GetValue(option))
.Should().NotThrow();
}

[Fact] // https://github.com/dotnet/command-line-api/issues/2210
public void Bool_with_unparseable_argument_does_not_throw()
{
CliRootCommand rootCommand = new();
CliOption<bool> option = new("--test");
rootCommand.Options.Add(option);
var result = rootCommand.Parse("--test ouch");

result.Invoking(r => r.GetValue(option))
.Should().NotThrow();
}

[Theory]
[InlineData("the-command -x")]
[InlineData("the-command -x true")]
Expand Down
7 changes: 4 additions & 3 deletions src/System.CommandLine/ParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ CommandLineText is null
public T? GetValue<T>(string name)
{
var command = CommandResult.Command;

if (_namedResults is null)
{
// A null value means that given name exists, but was not parsed
Expand Down Expand Up @@ -180,12 +181,12 @@ void Populate<TSymbol>(Dictionary<string, SymbolResult?> cache, IList<TSymbol> s
}
}

static T? Convert(ArgumentConversionResult validatedResult)
static T Convert(ArgumentConversionResult validatedResult)
{
var convertedResult = validatedResult.ConvertIfNeeded(typeof(T));

if (validatedResult.Result == ArgumentConversionResultType.Successful
&& convertedResult.Result == ArgumentConversionResultType.NoArgument)
if (validatedResult is { Result: ArgumentConversionResultType.Successful }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you find this syntax more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer it as a best practice because the behavior of == can be changed by operator overloading.

&& convertedResult is { Result: ArgumentConversionResultType.NoArgument })
{
// invalid cast has been detected, InvalidCastException will be thrown
return (T)validatedResult.Value!;
Expand Down
3 changes: 2 additions & 1 deletion src/System.CommandLine/Parsing/ParseOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ private void ParseOptionArguments(OptionResult optionResult)
break;
}
}
else if (argument.ValueType == typeof(bool) &&
else if ((argument.ValueType == typeof(bool) || argument.ValueType == typeof(bool?)) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

BTW a while ago I have introduced a helper method for that:

internal bool IsBoolean() => ValueType == typeof(bool) || ValueType == typeof(bool?);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out! I've updated to use that in my other PR.

!bool.TryParse(CurrentToken.Value, out _))
{
// Don't greedily consume the following token for bool. The presence of the option token (i.e. a flag) is sufficient.
break;
}

Expand Down