Skip to content

Commit f9235d5

Browse files
gpetrounrtjonsequitur
authored andcommitted
Fix short/ushort parsing
1 parent 15f51e1 commit f9235d5

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,45 @@ public void Values_can_be_correctly_converted_to_nullable_long_without_the_parse
681681
value.Should().Be(1234567890L);
682682
}
683683

684+
[Fact]
685+
public void Values_can_be_correctly_converted_to_short_without_the_parser_specifying_a_custom_converter()
686+
{
687+
var option = new Option<ushort>("-s");
688+
689+
var value = option.Parse("-s 1234").GetValueForOption(option);
690+
691+
value.Should().Be(1234);
692+
}
693+
694+
[Fact]
695+
public void Values_can_be_correctly_converted_to_nullable_short_without_the_parser_specifying_a_custom_converter()
696+
{
697+
var option = new Option<short?>("-s");
698+
699+
var value = option.Parse("-s 1234").GetValueForOption(option);
700+
701+
value.Should().Be(1234);
702+
}
703+
704+
[Fact]
705+
public void Values_can_be_correctly_converted_to_ushort_without_the_parser_specifying_a_custom_converter()
706+
{
707+
var option = new Option<ushort>("-us");
708+
709+
var value = option.Parse("-us 1234").GetValueForOption(option);
710+
711+
value.Should().Be(1234);
712+
}
713+
714+
[Fact]
715+
public void Values_can_be_correctly_converted_to_nullable_ushort_without_the_parser_specifying_a_custom_converter()
716+
{
717+
var option = new Option<ushort?>("-us");
718+
719+
var value = option.Parse("-us 1234").GetValueForOption(option);
720+
721+
value.Should().Be(1234);
722+
}
684723

685724
[Fact]
686725
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,54 @@ internal static partial class ArgumentConverter
234234
return false;
235235
},
236236

237+
[typeof(short)] = (string token, out object? value) =>
238+
{
239+
if (short.TryParse(token, out var shortValue))
240+
{
241+
value = shortValue;
242+
return true;
243+
}
244+
245+
value = default;
246+
return false;
247+
},
248+
249+
[typeof(short?)] = (string token, out object? value) =>
250+
{
251+
if (short.TryParse(token, out var shortValue))
252+
{
253+
value = shortValue;
254+
return true;
255+
}
256+
257+
value = default;
258+
return false;
259+
},
260+
261+
[typeof(ushort)] = (string token, out object? value) =>
262+
{
263+
if (ushort.TryParse(token, out var ushortValue))
264+
{
265+
value = ushortValue;
266+
return true;
267+
}
268+
269+
value = default;
270+
return false;
271+
},
272+
273+
[typeof(ushort?)] = (string token, out object? value) =>
274+
{
275+
if (ushort.TryParse(token, out var ushortValue))
276+
{
277+
value = ushortValue;
278+
return true;
279+
}
280+
281+
value = default;
282+
return false;
283+
},
284+
237285
[typeof(string)] = (string input, out object? value) =>
238286
{
239287
value = input;

0 commit comments

Comments
 (0)