Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/CommandLine/Core/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ private static Maybe<object> ChangeTypeScalar(string value, Type conversionType,
return result.ToMaybe();
}

private static object ConvertString(string value, Type type, CultureInfo conversionCulture)
{
try
{
return Convert.ChangeType(value, type, conversionCulture);
}
catch (InvalidCastException)
{
// Required for converting from string to TimeSpan because Convert.ChangeType can't
return System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertFrom(null, conversionCulture, value);
}
}

private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
{
Func<object> changeType = () =>
Expand All @@ -71,10 +84,9 @@ private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type
() =>
#if !SKIP_FSHARP
isFsOption
? FSharpOptionHelper.Some(type, Convert.ChangeType(value, type, conversionCulture)) :
? FSharpOptionHelper.Some(type, ConvertString(value, type, conversionCulture)) :
#endif
Convert.ChangeType(value, type, conversionCulture);

ConvertString(value, type, conversionCulture);
#if !SKIP_FSHARP
Func<object> empty = () => isFsOption ? FSharpOptionHelper.None(type) : null;
#else
Expand Down
1 change: 1 addition & 0 deletions tests/CommandLine.Tests/CommandLine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Fakes\Options_With_Two_Options_Having_Required_Set_To_True.cs" />
<Compile Include="Fakes\Options_With_Required_Set_To_True.cs" />
<Compile Include="Fakes\Options_With_Required_Set_To_True_Within_Same_Set.cs" />
<Compile Include="Fakes\Options_With_TimeSpan.cs" />
<Compile Include="Fakes\Help_Fakes.cs" />
<Compile Include="Fakes\IInterface_With_Two_Scalar_Options.cs" />
<Compile Include="Fakes\Immutable_Verb_Fakes.cs" />
Expand Down
12 changes: 12 additions & 0 deletions tests/CommandLine.Tests/Fakes/Options_With_TimeSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System;

namespace CommandLine.Tests.Fakes
{
public class Options_With_TimeSpan
{
[Option('d', "duration")]
public TimeSpan Duration { get; set; }
}
}
16 changes: 16 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,22 @@ public void Parse_Guid(string[] arguments, Options_With_Guid expected)
// Teardown
}

[Fact]
public void Parse_TimeSpan()
{
// Fixture setup
var expectedResult = new Options_With_TimeSpan { Duration = TimeSpan.FromMinutes(42) };

// Exercize system
var result = InvokeBuild<Options_With_TimeSpan>(
new[] { "--duration=00:42:00" });

// Verify outcome
expectedResult.ShouldBeEquivalentTo(((Parsed<Options_With_TimeSpan>)result).Value);

// Teardown
}

public static IEnumerable<object> RequiredValueStringData
{
get
Expand Down
16 changes: 8 additions & 8 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void Implicit_help_screen_in_verb_scenario()
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -397,7 +397,7 @@ public void Double_dash_help_dispalys_verbs_index_in_verbs_scenario()
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -452,7 +452,7 @@ public void Errors_of_type_MutuallyExclusiveSetError_are_properly_formatted()
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -501,7 +501,7 @@ public void Properly_formatted_help_screen_is_displayed_when_usage_is_defined_in
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -541,7 +541,7 @@ public void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_v
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -571,7 +571,7 @@ public void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_v
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -639,7 +639,7 @@ public void Specific_verb_help_screen_should_be_displayed_regardless_other_argum
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down Expand Up @@ -709,7 +709,7 @@ public void Properly_formatted_help_screen_excludes_help_as_unknown_option()
var lines = result.ToNotEmptyLines().TrimStringArray();
#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down
2 changes: 1 addition & 1 deletion tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public void Invoke_AutoBuild_for_Verbs_with_specific_verb_returns_appropriate_fo

#if !PLATFORM_DOTNET
lines[0].Should().StartWithEquivalent("CommandLine");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2015 Giacomo Stelluti Scala");
lines[1].ShouldBeEquivalentTo("Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors");
#else
// Takes the name of the xUnit test program
lines[0].Should().StartWithEquivalent("xUnit");
Expand Down