From fba12974f183e4cebc72982feafb5f111585d6be Mon Sep 17 00:00:00 2001 From: Qingchuan Zhang Date: Tue, 1 Mar 2022 13:54:23 +0800 Subject: [PATCH 1/3] 1. Add string converters for uint/sbyte/byte 2. Fix typo in ulong converter --- .../ArgumentConverter.StringConverters.cs | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs index 604b7b920c..3314be2c01 100644 --- a/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs +++ b/src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs @@ -163,6 +163,42 @@ internal static partial class ArgumentConverter return false; }, + [typeof(uint)] = (string token, out object? value) => + { + if (uint.TryParse(token, out var uintValue)) + { + value = uintValue; + return true; + } + + value = default; + return false; + }, + + [typeof(sbyte)] = (string token, out object? value) => + { + if (sbyte.TryParse(token, out var sbyteValue)) + { + value = sbyteValue; + return true; + } + + value = default; + return false; + }, + + [typeof(byte)] = (string token, out object? value) => + { + if (byte.TryParse(token, out var byteValue)) + { + value = byteValue; + return true; + } + + value = default; + return false; + }, + [typeof(string)] = (string input, out object? value) => { value = input; @@ -171,9 +207,9 @@ internal static partial class ArgumentConverter [typeof(ulong)] = (string token, out object? value) => { - if (ulong.TryParse(token, out var ushortValue)) + if (ulong.TryParse(token, out var ulongValue)) { - value = ushortValue; + value = ulongValue; return true; } From 5acec0d3185941cb953598fe0c7d06b7e4f718e5 Mon Sep 17 00:00:00 2001 From: Qingchuan Zhang Date: Tue, 1 Mar 2022 16:03:07 +0800 Subject: [PATCH 2/3] add unit test --- .../Binding/TypeConversionTests.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index 3d6e406334..e739c24ab5 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -758,6 +758,66 @@ public void Values_can_be_correctly_converted_to_nullable_ushort_without_the_par value.Should().Be(1234); } + + [Fact] + public void Values_can_be_correctly_converted_to_sbyte_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-us"); + + var value = option.Parse("-us 123").GetValueForOption(option); + + value.Should().Be(123); + } + + [Fact] + public void Values_can_be_correctly_converted_to_nullable_sbyte_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-x"); + + var value = option.Parse("-x 123").GetValueForOption(option); + + value.Should().Be(123); + } + + [Fact] + public void Values_can_be_correctly_converted_to_byte_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-us"); + + var value = option.Parse("-us 1234").GetValueForOption(option); + + value.Should().Be(123); + } + + [Fact] + public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-x"); + + var value = option.Parse("-x 1234").GetValueForOption(option); + + value.Should().Be(123); + } + + [Fact] + public void Values_can_be_correctly_converted_to_uint_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-us"); + + var value = option.Parse("-us 1234").GetValueForOption(option); + + value.Should().Be(1234); + } + + [Fact] + public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parser_specifying_a_custom_converter() + { + var option = new Option("-x"); + + var value = option.Parse("-x 1234").GetValueForOption(option); + + value.Should().Be(1234); + } [Fact] public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter() From 32f4d6fbcbdaa7d4253a5f26ef1d6433652f52f5 Mon Sep 17 00:00:00 2001 From: Qingchuan Zhang Date: Tue, 1 Mar 2022 16:17:49 +0800 Subject: [PATCH 3/3] fix typo --- src/System.CommandLine.Tests/Binding/TypeConversionTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs index e739c24ab5..56474021db 100644 --- a/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs +++ b/src/System.CommandLine.Tests/Binding/TypeConversionTests.cs @@ -784,7 +784,7 @@ public void Values_can_be_correctly_converted_to_byte_without_the_parser_specify { var option = new Option("-us"); - var value = option.Parse("-us 1234").GetValueForOption(option); + var value = option.Parse("-us 123").GetValueForOption(option); value.Should().Be(123); } @@ -794,7 +794,7 @@ public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parse { var option = new Option("-x"); - var value = option.Parse("-x 1234").GetValueForOption(option); + var value = option.Parse("-x 123").GetValueForOption(option); value.Should().Be(123); }