Skip to content

Commit e5c8961

Browse files
committed
Merge branch 'develop' of github.com:commandlineparser/commandline into develop
2 parents 286b724 + 3bb2f01 commit e5c8961

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ class Options
5555
public IEnumerable<string> InputFiles { get; set; }
5656

5757
// Omitting long name, defaults to name of property, ie "--verbose"
58-
[Option(Default = false, HelpText = "Prints all messages to standard output.")]
58+
[Option(
59+
Default = false,
60+
HelpText = "Prints all messages to standard output.")]
5961
public bool Verbose { get; set; }
60-
61-
[Option("stdin", Default = false, HelpText = "Read from stdin")]
62+
63+
[Option("stdin",
64+
Default = false
65+
HelpText = "Read from stdin")]
6266
public bool stdin { get; set; }
6367

6468
[Value(0, MetaName = "offset", HelpText = "File offset.")]
@@ -79,7 +83,7 @@ F# Examples:
7983
type options = {
8084
[<Option('r', "read", Required = true, HelpText = "Input files.")>] files : seq<string>;
8185
[<Option(HelpText = "Prints all messages to standard output.")>] verbose : bool;
82-
[<Option(DefaultValue = "русский", HelpText = "Content language.")>] language : string;
86+
[<Option(Default = "русский", HelpText = "Content language.")>] language : string;
8387
[<Value(0, MetaName="offset", HelpText = "File offset.")>] offset : int64 option;
8488
}
8589
@@ -94,18 +98,22 @@ VB.Net:
9498

9599
```VB.NET
96100
Class Options
97-
<CommandLine.Option("r", "read", Required:=True, HelpText:="Input files to be processed.")>
98-
Public Property InputFiles As IEnumerable(Of String)
99-
100-
' Omitting long name, defaults to name of property, ie "--verbose"
101-
<CommandLine.Option(HelpText:="Prints all messages to standard output.")>
102-
Public Property Verbose As Boolean
103-
104-
<CommandLine.Option([Default]:="中文", HelpText:="Content language.")>
105-
Public Property Language As String
106-
107-
<CommandLine.Value(0, MetaName:="offset", HelpText:="File offset.")>
108-
Public Property Offset As Long?
101+
<CommandLine.Option('r', "read", Required := true,
102+
HelpText:="Input files to be processed.")>
103+
Public Property InputFiles As IEnumerable(Of String)
104+
105+
' Omitting long name, defaults to name of property, ie "--verbose"
106+
<CommandLine.Option(
107+
HelpText:="Prints all messages to standard output.")>
108+
Public Property Verbose As Boolean
109+
110+
<CommandLine.Option(Default:="中文",
111+
HelpText:="Content language.")>
112+
Public Property Language As String
113+
114+
<CommandLine.Value(0, MetaName:="offset",
115+
HelpText:="File offset.")>
116+
Public Property Offset As Long?
109117
End Class
110118

111119
Sub Main(ByVal args As String())

src/CommandLine/Core/TypeConverter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ private static Maybe<object> ChangeTypeScalar(string value, Type conversionType,
4949
return result.ToMaybe();
5050
}
5151

52+
private static object ConvertString(string value, Type type, CultureInfo conversionCulture)
53+
{
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+
}
63+
}
64+
5265
private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
5366
{
5467
Func<object> changeType = () =>
@@ -71,10 +84,9 @@ private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type
7184
() =>
7285
#if !SKIP_FSHARP
7386
isFsOption
74-
? FSharpOptionHelper.Some(type, Convert.ChangeType(value, type, conversionCulture)) :
87+
? FSharpOptionHelper.Some(type, ConvertString(value, type, conversionCulture)) :
7588
#endif
76-
Convert.ChangeType(value, type, conversionCulture);
77-
89+
ConvertString(value, type, conversionCulture);
7890
#if !SKIP_FSHARP
7991
Func<object> empty = () => isFsOption ? FSharpOptionHelper.None(type) : null;
8092
#else

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
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" />
Lines changed: 12 additions & 0 deletions
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+
}

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

Lines changed: 16 additions & 0 deletions
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)