Skip to content

Commit d46d332

Browse files
committed
Add support for TimeSpan options
Fixes #173
1 parent 734519c commit d46d332

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Diff for: src/CommandLine/Core/TypeConverter.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ private static Maybe<object> ChangeTypeScalar(string value, Type conversionType,
5151

5252
private static object ConvertString(string value, Type type, CultureInfo conversionCulture)
5353
{
54-
return Convert.ChangeType(value, type, conversionCulture);
54+
try
55+
{
56+
return Convert.ChangeType(value, type, conversionCulture);
57+
}
58+
catch (InvalidCastException)
59+
{
60+
// Required for converting from string to TimeSpan because Convert.ChangeType can't
61+
return System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertFrom(null, conversionCulture, value);
62+
}
5563
}
56-
64+
5765
private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
5866
{
5967
Func<object> changeType = () =>

Diff for: tests/CommandLine.Tests/CommandLine.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Fakes\Options_With_Two_Options_Having_Required_Set_To_True.cs" />
9696
<Compile Include="Fakes\Options_With_Required_Set_To_True.cs" />
9797
<Compile Include="Fakes\Options_With_Required_Set_To_True_Within_Same_Set.cs" />
98+
<Compile Include="Fakes\Options_With_TimeSpan.cs" />
9899
<Compile Include="Fakes\Help_Fakes.cs" />
99100
<Compile Include="Fakes\IInterface_With_Two_Scalar_Options.cs" />
100101
<Compile Include="Fakes\Immutable_Verb_Fakes.cs" />
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System;
4+
5+
namespace CommandLine.Tests.Fakes
6+
{
7+
public class Options_With_TimeSpan
8+
{
9+
[Option('d', "duration")]
10+
public TimeSpan Duration { get; set; }
11+
}
12+
}

Diff for: tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,22 @@ public void Parse_Guid(string[] arguments, Options_With_Guid expected)
10231023
// Teardown
10241024
}
10251025

1026+
[Fact]
1027+
public void Parse_TimeSpan()
1028+
{
1029+
// Fixture setup
1030+
var expectedResult = new Options_With_TimeSpan { Duration = TimeSpan.FromMinutes(42) };
1031+
1032+
// Exercize system
1033+
var result = InvokeBuild<Options_With_TimeSpan>(
1034+
new[] { "--duration=00:42:00" });
1035+
1036+
// Verify outcome
1037+
expectedResult.ShouldBeEquivalentTo(((Parsed<Options_With_TimeSpan>)result).Value);
1038+
1039+
// Teardown
1040+
}
1041+
10261042
public static IEnumerable<object> RequiredValueStringData
10271043
{
10281044
get

0 commit comments

Comments
 (0)