|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Globalization;
|
| 7 | +using System.Reflection; |
7 | 8 | using Xunit;
|
8 | 9 |
|
9 | 10 | namespace McMaster.Extensions.CommandLineUtils.Tests
|
10 | 11 | {
|
11 |
| - using System.Reflection; |
12 |
| - |
13 | 12 | public class ValueParserProviderTests
|
14 | 13 | {
|
15 | 14 | public enum Color
|
@@ -45,6 +44,12 @@ private class Program
|
45 | 44 | [Option("--bool-opt", CommandOptionType.SingleValue)]
|
46 | 45 | public bool? BoolOpt { get; }
|
47 | 46 |
|
| 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 | + |
48 | 53 | [Option("--int32-opt")]
|
49 | 54 | public int? Int32Opt { get; }
|
50 | 55 |
|
@@ -300,6 +305,45 @@ public void ParsesNullableBool(string arg, bool? result)
|
300 | 305 | Assert.Equal(result, parsed.BoolOpt);
|
301 | 306 | }
|
302 | 307 |
|
| 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 | + |
303 | 347 | [Theory]
|
304 | 348 | [InlineData("4294967295", uint.MaxValue)]
|
305 | 349 | [InlineData("1", 1)]
|
|
0 commit comments