Skip to content

Commit 15f51e1

Browse files
committed
fix #1615
1 parent bcd1df5 commit 15f51e1

File tree

2 files changed

+218
-1
lines changed

2 files changed

+218
-1
lines changed

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

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,53 @@ public void Specifying_an_option_argument_overrides_the_default_value()
526526

527527
value.Should().Be(456);
528528
}
529-
529+
530+
531+
[Fact]
532+
public void Values_can_be_correctly_converted_to_DateTime_without_the_parser_specifying_a_custom_converter()
533+
{
534+
var option = new Option<DateTime>("-x");
535+
536+
var dateString = "2022-02-06T01:46:03.0000000-08:00";
537+
var value = option.Parse($"-x {dateString}").GetValueForOption(option);
538+
539+
value.Should().Be(DateTime.Parse(dateString));
540+
}
541+
542+
543+
[Fact]
544+
public void Values_can_be_correctly_converted_to_nullable_DateTime_without_the_parser_specifying_a_custom_converter()
545+
{
546+
var option = new Option<DateTime?>("-x");
547+
548+
var dateString = "2022-02-06T01:46:03.0000000-08:00";
549+
var value = option.Parse($"-x {dateString}").GetValueForOption(option);
550+
551+
value.Should().Be(DateTime.Parse(dateString));
552+
}
553+
554+
[Fact]
555+
public void Values_can_be_correctly_converted_to_DateTimeOffset_without_the_parser_specifying_a_custom_converter()
556+
{
557+
var option = new Option<DateTimeOffset>("-x");
558+
559+
var dateString = "2022-02-06T09:52:54.5275055-08:00";
560+
var value = option.Parse($"-x {dateString}").GetValueForOption(option);
561+
562+
value.Should().Be(DateTime.Parse(dateString));
563+
}
564+
565+
[Fact]
566+
public void Values_can_be_correctly_converted_to_nullable_DateTimeOffset_without_the_parser_specifying_a_custom_converter()
567+
{
568+
var option = new Option<DateTimeOffset?>("-x");
569+
570+
var dateString = "2022-02-06T09:52:54.5275055-08:00";
571+
var value = option.Parse($"-x {dateString}").GetValueForOption(option);
572+
573+
value.Should().Be(DateTime.Parse(dateString));
574+
}
575+
530576
[Fact]
531577
public void Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter()
532578
{
@@ -536,6 +582,16 @@ public void Values_can_be_correctly_converted_to_decimal_without_the_parser_spec
536582

537583
value.Should().Be(123.456m);
538584
}
585+
586+
[Fact]
587+
public void Values_can_be_correctly_converted_to_nullable_decimal_without_the_parser_specifying_a_custom_converter()
588+
{
589+
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
590+
591+
var value = option.Parse("-x 123.456").GetValueForOption<decimal?>(option);
592+
593+
value.Should().Be(123.456m);
594+
}
539595

540596
[Fact]
541597
public void Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter()
@@ -546,6 +602,36 @@ public void Values_can_be_correctly_converted_to_double_without_the_parser_speci
546602

547603
value.Should().Be(123.456d);
548604
}
605+
606+
[Fact]
607+
public void Values_can_be_correctly_converted_to_nullable_double_without_the_parser_specifying_a_custom_converter()
608+
{
609+
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
610+
611+
var value = option.Parse("-x 123.456").GetValueForOption<double?>(option);
612+
613+
value.Should().Be(123.456d);
614+
}
615+
616+
[Fact]
617+
public void Values_can_be_correctly_converted_to_float_without_the_parser_specifying_a_custom_converter()
618+
{
619+
var option = new Option<float>("-x");
620+
621+
var value = option.Parse("-x 123.456").GetValueForOption(option);
622+
623+
value.Should().Be(123.456f);
624+
}
625+
626+
[Fact]
627+
public void Values_can_be_correctly_converted_to_nullable_float_without_the_parser_specifying_a_custom_converter()
628+
{
629+
var option = new Option<float?>("-x");
630+
631+
var value = option.Parse("-x 123.456").GetValueForOption(option);
632+
633+
value.Should().Be(123.456f);
634+
}
549635

550636
[Fact]
551637
public void Values_can_be_correctly_converted_to_Uri_without_the_parser_specifying_a_custom_converter()
@@ -574,6 +660,28 @@ public void Options_with_arguments_specified_can_be_correctly_converted_to_bool_
574660
option.Parse("-x true").GetValueForOption<bool>(option).Should().BeTrue();
575661
}
576662

663+
664+
[Fact]
665+
public void Values_can_be_correctly_converted_to_long_without_the_parser_specifying_a_custom_converter()
666+
{
667+
var option = new Option<long>("-x");
668+
669+
var value = option.Parse("-x 123456790").GetValueForOption(option);
670+
671+
value.Should().Be(123456790L);
672+
}
673+
674+
[Fact]
675+
public void Values_can_be_correctly_converted_to_nullable_long_without_the_parser_specifying_a_custom_converter()
676+
{
677+
var option = new Option<long?>("-x");
678+
679+
var value = option.Parse("-x 1234567890").GetValueForOption(option);
680+
681+
value.Should().Be(1234567890L);
682+
}
683+
684+
577685
[Fact]
578686
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter()
579687
{

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,55 @@ internal static partial class ArgumentConverter
3434
return false;
3535
},
3636

37+
[typeof(DateTime)] = (string input, out object? value) =>
38+
{
39+
if (DateTime.TryParse(input, out var parsed))
40+
{
41+
value = parsed;
42+
return true;
43+
}
44+
45+
value = default;
46+
return false;
47+
},
48+
49+
[typeof(DateTime?)] = (string input, out object? value) =>
50+
{
51+
if (DateTime.TryParse(input, out var parsed))
52+
{
53+
value = parsed;
54+
return true;
55+
}
56+
57+
value = default;
58+
return false;
59+
},
60+
61+
[typeof(DateTimeOffset)] = (string input, out object? value) =>
62+
{
63+
if (DateTimeOffset.TryParse(input, out var parsed))
64+
{
65+
value = parsed;
66+
return true;
67+
}
68+
69+
value = default;
70+
return false;
71+
},
72+
73+
[typeof(DateTimeOffset?)] = (string input, out object? value) =>
74+
{
75+
if (DateTimeOffset.TryParse(input, out var parsed))
76+
{
77+
value = parsed;
78+
return true;
79+
}
80+
81+
value = default;
82+
return false;
83+
},
84+
85+
3786
[typeof(decimal)] = (string input, out object? value) =>
3887
{
3988
if (decimal.TryParse(input, out var parsed))
@@ -46,6 +95,18 @@ internal static partial class ArgumentConverter
4695
return false;
4796
},
4897

98+
[typeof(decimal?)] = (string input, out object? value) =>
99+
{
100+
if (decimal.TryParse(input, out var parsed))
101+
{
102+
value = parsed;
103+
return true;
104+
}
105+
106+
value = default;
107+
return false;
108+
},
109+
49110
[typeof(DirectoryInfo)] = (string path, out object? value) =>
50111
{
51112
value = new DirectoryInfo(path);
@@ -64,6 +125,18 @@ internal static partial class ArgumentConverter
64125
return false;
65126
},
66127

128+
[typeof(double?)] = (string input, out object? value) =>
129+
{
130+
if (double.TryParse(input, out var parsed))
131+
{
132+
value = parsed;
133+
return true;
134+
}
135+
136+
value = default;
137+
return false;
138+
},
139+
67140
[typeof(FileInfo)] = (string path, out object? value) =>
68141
{
69142
value = new FileInfo(path);
@@ -100,6 +173,18 @@ internal static partial class ArgumentConverter
100173
value = default;
101174
return false;
102175
},
176+
177+
[typeof(float?)] = (string input, out object? value) =>
178+
{
179+
if (float.TryParse(input, out var parsed))
180+
{
181+
value = parsed;
182+
return true;
183+
}
184+
185+
value = default;
186+
return false;
187+
},
103188

104189
[typeof(int)] = (string token, out object? value) =>
105190
{
@@ -124,6 +209,30 @@ internal static partial class ArgumentConverter
124209
value = default;
125210
return false;
126211
},
212+
213+
[typeof(long)] = (string token, out object? value) =>
214+
{
215+
if (long.TryParse(token, out var longValue))
216+
{
217+
value = longValue;
218+
return true;
219+
}
220+
221+
value = default;
222+
return false;
223+
},
224+
225+
[typeof(long?)] = (string token, out object? value) =>
226+
{
227+
if (long.TryParse(token, out var longValue))
228+
{
229+
value = longValue;
230+
return true;
231+
}
232+
233+
value = default;
234+
return false;
235+
},
127236

128237
[typeof(string)] = (string input, out object? value) =>
129238
{

0 commit comments

Comments
 (0)