diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index d28d9c7668..3d6e406334 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections; @@ -221,6 +221,21 @@ 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("-b"); + + var cmd = new RootCommand + { + option + }; + + var parseResult = cmd.Parse("-b"); + + parseResult.GetValueForOption((Option)option).Should().Be(true); + } + [Fact] public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value() { diff --git a/src/System.CommandLine/Binding/ArgumentConverter.cs b/src/System.CommandLine/Binding/ArgumentConverter.cs index 0cf0819c97..f7e0cba633 100644 --- a/src/System.CommandLine/Binding/ArgumentConverter.cs +++ b/src/System.CommandLine/Binding/ArgumentConverter.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections; @@ -204,8 +204,8 @@ internal static ArgumentConversionResult ConvertIfNeeded( toType, successful.Value, symbolResult.LocalizationResources), - - NoArgumentConversionResult _ when toType == typeof(bool) || toType == typeof(bool?) => + + NoArgumentConversionResult _ when conversionResult.Argument.ValueType == typeof(bool) || conversionResult.Argument.ValueType == typeof(bool?) => Success(conversionResult.Argument, true), NoArgumentConversionResult _ when conversionResult.Argument.Arity.MinimumNumberOfValues > 0 =>