Skip to content

Commit 4f686fe

Browse files
fix: treat null as 'true' by default when parsing Nullable<bool> types on command options (#397)
1 parent e621724 commit 4f686fe

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/CommandLineUtils/Internal/ValueParsers/StockValueParsers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class StockValueParsers
1414
switch (value)
1515
{
1616
case null:
17-
return default;
17+
return true;
1818
case "T":
1919
case "t":
2020
return true;

test/CommandLineUtils.Tests/ValueParserProviderTests.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Globalization;
7+
using System.Reflection;
78
using Xunit;
89

910
namespace McMaster.Extensions.CommandLineUtils.Tests
1011
{
11-
using System.Reflection;
12-
1312
public class ValueParserProviderTests
1413
{
1514
public enum Color
@@ -45,6 +44,12 @@ private class Program
4544
[Option("--bool-opt", CommandOptionType.SingleValue)]
4645
public bool? BoolOpt { get; }
4746

47+
[Option("--bool-with-optional-value", CommandOptionType.SingleOrNoValue)]
48+
public bool BoolWithOptionalValue { get; }
49+
50+
[Option("--bool-opt-with-optional-value", CommandOptionType.SingleOrNoValue)]
51+
public (bool, bool?) BoolOptWithOptionalValue { get; }
52+
4853
[Option("--int32-opt")]
4954
public int? Int32Opt { get; }
5055

@@ -300,6 +305,45 @@ public void ParsesNullableBool(string arg, bool? result)
300305
Assert.Equal(result, parsed.BoolOpt);
301306
}
302307

308+
[Theory]
309+
[InlineData("true", true)]
310+
[InlineData("True", true)]
311+
[InlineData("False", false)]
312+
[InlineData("false", false)]
313+
public void ParsesBoolWithOptionalValue(string arg, bool result)
314+
{
315+
var parsed = CommandLineParser.ParseArgs<Program>($"--bool-with-optional-value:{arg}");
316+
Assert.Equal(result, parsed.BoolWithOptionalValue);
317+
}
318+
319+
[Fact]
320+
public void ParsesBoolWithoutOptionalValue()
321+
{
322+
var parsed = CommandLineParser.ParseArgs<Program>("--bool-with-optional-value");
323+
Assert.True(parsed.BoolWithOptionalValue);
324+
}
325+
326+
[Theory]
327+
[InlineData("", null)]
328+
[InlineData("true", true)]
329+
[InlineData("True", true)]
330+
[InlineData("False", false)]
331+
[InlineData("false", false)]
332+
public void ParsesNullableBoolWithOptionalValue(string arg, bool? result)
333+
{
334+
var parsed = CommandLineParser.ParseArgs<Program>($"--bool-opt-with-optional-value:{arg}");
335+
Assert.True(parsed.BoolOptWithOptionalValue.Item1);
336+
Assert.Equal(result, parsed.BoolOptWithOptionalValue.Item2);
337+
}
338+
339+
[Fact]
340+
public void ParsesNullableBoolWithoutOptionalValue()
341+
{
342+
var parsed = CommandLineParser.ParseArgs<Program>("--bool-opt-with-optional-value");
343+
Assert.True(parsed.BoolOptWithOptionalValue.Item1);
344+
Assert.Null(parsed.BoolOptWithOptionalValue.Item2);
345+
}
346+
303347
[Theory]
304348
[InlineData("4294967295", uint.MaxValue)]
305349
[InlineData("1", 1)]

0 commit comments

Comments
 (0)