Skip to content

Commit d886a77

Browse files
committed
fix #1623
1 parent 77c170f commit d886a77

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,28 @@ public void Values_can_be_correctly_converted_to_nullable_float_without_the_pars
633633
value.Should().Be(123.456f);
634634
}
635635

636+
[Fact]
637+
public void Values_can_be_correctly_converted_to_Guid_without_the_parser_specifying_a_custom_converter()
638+
{
639+
var guidString = "75517282-018F-46BB-B15F-1D8DBFE23F6E";
640+
var option = new Option<Guid>("-x");
641+
642+
var value = option.Parse($"-x {guidString}").GetValueForOption(option);
643+
644+
value.Should().Be(Guid.Parse(guidString));
645+
}
646+
647+
[Fact]
648+
public void Values_can_be_correctly_converted_to_nullable_Guid_without_the_parser_specifying_a_custom_converter()
649+
{
650+
var guidString = "75517282-018F-46BB-B15F-1D8DBFE23F6E";
651+
var option = new Option<Guid?>("-x");
652+
653+
var value = option.Parse($"-x {guidString}").GetValueForOption(option);
654+
655+
value.Should().Be(Guid.Parse(guidString));
656+
}
657+
636658
[Fact]
637659
public void Values_can_be_correctly_converted_to_Uri_without_the_parser_specifying_a_custom_converter()
638660
{

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ internal static partial class ArgumentConverter
112112
value = default;
113113
return false;
114114
},
115+
116+
[typeof(Guid)] = (string input, out object? value) =>
117+
{
118+
if (Guid.TryParse(input, out var parsed))
119+
{
120+
value = parsed;
121+
return true;
122+
}
123+
124+
value = default;
125+
return false;
126+
},
115127

116128
[typeof(int)] = (string token, out object? value) =>
117129
{

0 commit comments

Comments
 (0)