Skip to content

Commit

Permalink
Add TimeSpan string converter (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabeDeBacker authored Apr 22, 2022
1 parent 689dff5 commit 372d408
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,28 @@ public void Values_can_be_correctly_converted_to_nullable_Guid_without_the_parse
value.Should().Be(Guid.Parse(guidString));
}

[Fact]
public void Values_can_be_correctly_converted_to_TimeSpan_without_the_parser_specifying_a_custom_converter()
{
var timeSpanString = "30";
var option = new Option<TimeSpan>("-x");

var value = option.Parse($"-x {timeSpanString}").GetValueForOption(option);

value.Should().Be(TimeSpan.Parse(timeSpanString));
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_TimeSpan_without_the_parser_specifying_a_custom_converter()
{
var timeSpanString = "30";
var option = new Option<TimeSpan?>("-x");

var value = option.Parse($"-x {timeSpanString}").GetValueForOption(option);

value.Should().Be(TimeSpan.Parse(timeSpanString));
}

[Fact]
public void Values_can_be_correctly_converted_to_Uri_without_the_parser_specifying_a_custom_converter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,17 @@ internal static partial class ArgumentConverter
value = default;
return false;
},

[typeof(TimeSpan)] = (string input, out object? value) =>
{
if (TimeSpan.TryParse(input, out var timeSpan))
{
value = timeSpan;
return true;
}
value = default;
return false;
},
};
}

0 comments on commit 372d408

Please sign in to comment.