Skip to content
Browse files

added support for nullable types

  • Loading branch information...
1 parent aab7167 commit 36d9a1de3b401acd110b0f5fa7309ce3bf994ae9 @adrianaisemberg committed Nov 22, 2011
Showing with 76 additions and 2 deletions.
  1. +2 −2 CLAP/Properties/AssemblyInfo.cs
  2. +14 −0 CLAP/ValuesFactory.cs
  3. +32 −0 Tests/Samples.cs
  4. +28 −0 Tests/Tests.cs
View
4 CLAP/Properties/AssemblyInfo.cs
@@ -29,5 +29,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("3.1.0.0")]
-[assembly: AssemblyFileVersion("3.1.0.0")]
+[assembly: AssemblyVersion("3.2.0.0")]
+[assembly: AssemblyFileVersion("3.2.0.0")]
View
14 CLAP/ValuesFactory.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using CLAP.Interception;
+using System.ComponentModel;
namespace CLAP
{
@@ -124,6 +125,19 @@ private static object ConvertString(string value, Type type)
{
return string.IsNullOrEmpty(value) ? (object)null : new Uri(Environment.ExpandEnvironmentVariables(value));
}
+ else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
+ {
+ if (string.IsNullOrEmpty(value))
+ {
+ return null;
+ }
+ else
+ {
+ var converter = new NullableConverter(type);
+
+ return Convert.ChangeType(value, converter.UnderlyingType);
+ }
+ }
else
{
return Convert.ChangeType(value, type);
View
32 Tests/Samples.cs
@@ -1050,6 +1050,38 @@ static void Post(int c)
}
}
+ public class Sample_60
+ {
+ [Verb]
+ static void Foo([Parameter(Aliases = "c")]int c)
+ {
+ }
+ }
+
+ public class Sample_61
+ {
+ public int? P1 { get; set; }
+ public int? P2 { get; set; }
+ public int? P3 { get; set; }
+ public int? P4 { get; set; }
+ public int? P5 { get; set; }
+
+ [Verb]
+ void Bar(
+ int? p1,
+ [Parameter(Default = 5)] int? p2,
+ [Parameter(Default = "5")] int? p3,
+ [Parameter(Default = "")] int? p4,
+ [Parameter(Default = null)] int? p5)
+ {
+ P1 = p1;
+ P2 = p2;
+ P3 = p3;
+ P4 = p4;
+ P5 = p5;
+ }
+ }
+
public enum Case
{
Upper,
View
28 Tests/Tests.cs
@@ -2193,5 +2193,33 @@ public void PostVerbExecution_MismatchArgs_Exception_2()
{
Parser.Run<Sample_59>(new[] { "" });
}
+
+ [Test, Ignore]
+ public void Parameter_NameAsAlias()
+ {
+ Parser.Run<Sample_60>(new[]
+ {
+ "foo",
+ "-c=5",
+ });
+ }
+
+ [Test]
+ public void Parameter_NullableInt()
+ {
+ var s = new Sample_61();
+
+ Parser.Run(new[]
+ {
+ "bar",
+ "-p1=5",
+ }, s);
+
+ Assert.AreEqual(5, s.P1);
+ Assert.AreEqual(5, s.P2);
+ Assert.AreEqual(5, s.P3);
+ Assert.AreEqual(null, s.P4);
+ Assert.AreEqual(null, s.P5);
+ }
}
}

0 comments on commit 36d9a1d

Please sign in to comment.
Something went wrong with that request. Please try again.