Skip to content

Commit 021ec68

Browse files
Add string converters for uint/sbyte/byte (#1661)
* 1. Add string converters for uint/sbyte/byte 2. Fix typo in ulong converter * add unit test * fix typo
1 parent 3cb430c commit 021ec68

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,66 @@ public void Values_can_be_correctly_converted_to_nullable_ushort_without_the_par
758758

759759
value.Should().Be(1234);
760760
}
761+
762+
[Fact]
763+
public void Values_can_be_correctly_converted_to_sbyte_without_the_parser_specifying_a_custom_converter()
764+
{
765+
var option = new Option<sbyte>("-us");
766+
767+
var value = option.Parse("-us 123").GetValueForOption(option);
768+
769+
value.Should().Be(123);
770+
}
771+
772+
[Fact]
773+
public void Values_can_be_correctly_converted_to_nullable_sbyte_without_the_parser_specifying_a_custom_converter()
774+
{
775+
var option = new Option<sbyte?>("-x");
776+
777+
var value = option.Parse("-x 123").GetValueForOption(option);
778+
779+
value.Should().Be(123);
780+
}
781+
782+
[Fact]
783+
public void Values_can_be_correctly_converted_to_byte_without_the_parser_specifying_a_custom_converter()
784+
{
785+
var option = new Option<byte>("-us");
786+
787+
var value = option.Parse("-us 123").GetValueForOption(option);
788+
789+
value.Should().Be(123);
790+
}
791+
792+
[Fact]
793+
public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parser_specifying_a_custom_converter()
794+
{
795+
var option = new Option<byte?>("-x");
796+
797+
var value = option.Parse("-x 123").GetValueForOption(option);
798+
799+
value.Should().Be(123);
800+
}
801+
802+
[Fact]
803+
public void Values_can_be_correctly_converted_to_uint_without_the_parser_specifying_a_custom_converter()
804+
{
805+
var option = new Option<uint>("-us");
806+
807+
var value = option.Parse("-us 1234").GetValueForOption(option);
808+
809+
value.Should().Be(1234);
810+
}
811+
812+
[Fact]
813+
public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parser_specifying_a_custom_converter()
814+
{
815+
var option = new Option<uint?>("-x");
816+
817+
var value = option.Parse("-x 1234").GetValueForOption(option);
818+
819+
value.Should().Be(1234);
820+
}
761821

762822
[Fact]
763823
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter()

src/System.CommandLine/Binding/ArgumentConverter.StringConverters.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,42 @@ internal static partial class ArgumentConverter
163163
return false;
164164
},
165165

166+
[typeof(uint)] = (string token, out object? value) =>
167+
{
168+
if (uint.TryParse(token, out var uintValue))
169+
{
170+
value = uintValue;
171+
return true;
172+
}
173+
174+
value = default;
175+
return false;
176+
},
177+
178+
[typeof(sbyte)] = (string token, out object? value) =>
179+
{
180+
if (sbyte.TryParse(token, out var sbyteValue))
181+
{
182+
value = sbyteValue;
183+
return true;
184+
}
185+
186+
value = default;
187+
return false;
188+
},
189+
190+
[typeof(byte)] = (string token, out object? value) =>
191+
{
192+
if (byte.TryParse(token, out var byteValue))
193+
{
194+
value = byteValue;
195+
return true;
196+
}
197+
198+
value = default;
199+
return false;
200+
},
201+
166202
[typeof(string)] = (string input, out object? value) =>
167203
{
168204
value = input;
@@ -171,9 +207,9 @@ internal static partial class ArgumentConverter
171207

172208
[typeof(ulong)] = (string token, out object? value) =>
173209
{
174-
if (ulong.TryParse(token, out var ushortValue))
210+
if (ulong.TryParse(token, out var ulongValue))
175211
{
176-
value = ushortValue;
212+
value = ulongValue;
177213
return true;
178214
}
179215

0 commit comments

Comments
 (0)