Skip to content

Commit

Permalink
added support for nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianaisemberg committed Nov 22, 2011
1 parent aab7167 commit 36d9a1d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CLAP/Properties/AssemblyInfo.cs
Expand Up @@ -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")]
14 changes: 14 additions & 0 deletions CLAP/ValuesFactory.cs
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using CLAP.Interception;
using System.ComponentModel;

namespace CLAP
{
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions Tests/Samples.cs
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions Tests/Tests.cs
Expand Up @@ -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.