Skip to content
Browse files

bug fix: default values were not applied when no args are set and a d…

…efault verb is used
  • Loading branch information...
1 parent 586f310 commit ff18a8eab958b439e2c26ee14b0cb41e5cf3f680 @adrianaisemberg committed
Showing with 59 additions and 4 deletions.
  1. +5 −3 CLAP/ParserRunner.cs
  2. +10 −1 ConsoleTest/Program.cs
  3. +18 −0 Tests/Samples.cs
  4. +26 −0 Tests/Tests.cs
View
8 CLAP/ParserRunner.cs
@@ -734,10 +734,12 @@ internal void HandleEmptyArguments(object target)
//
if (defaultVerb != null)
{
- // create an array of (null) arguments that matches the method
+ // create an array of arguments that matches the method
//
- var parameters = defaultVerb.MethodInfo.GetParameters().
- Select(p => new ParameterAndValue(new Parameter(p), (object)null)).ToArray();
+ var parameters = ValuesFactory.CreateParameterValues(
+ defaultVerb.MethodInfo.Name,
+ new Dictionary<string, string>(),
+ GetParameters(defaultVerb.MethodInfo));
Execute(target, defaultVerb, parameters);
}
View
11 ConsoleTest/Program.cs
@@ -13,7 +13,16 @@ class Program
{
static void Main(string[] args)
{
- Parser.Run<SomeApp>(args);
+ Parser.Run<DefaultApp>(args);
+ }
+ }
+
+ class DefaultApp
+ {
+ [Verb(IsDefault = true)]
+ public static void Foo([Parameter(Default = "zoo")]string bar)
+ {
+ Console.WriteLine("bar = {0}", bar);
}
}
View
18 Tests/Samples.cs
@@ -52,6 +52,24 @@ public class Sample_02 : BaseSample
}
}
+ public class Sample_02_Default : BaseSample
+ {
+ [Verb(IsDefault = true)]
+ public void Print([Parameter(Default = "bar")]string foo)
+ {
+ Printer.Print(foo);
+ }
+ }
+
+ public class Sample_02_No_Default : BaseSample
+ {
+ [Verb(IsDefault = true)]
+ public void Print(string foo)
+ {
+ Printer.Print(foo);
+ }
+ }
+
public class ValidationSample_01 : BaseSample
{
[Verb]
View
26 Tests/Tests.cs
@@ -46,6 +46,32 @@ public void Execute_DefaultVerb()
}
[Test]
+ public void Execute_DefaultVerbWithDefaultValue()
+ {
+ var printer = new Printer();
+ var sample = new Sample_02_Default { Printer = printer };
+
+ Parser.Run(new string[]
+ {
+ }, sample);
+
+ Assert.AreEqual("bar", printer.PrintedTexts[0]);
+ }
+
+ [Test]
+ public void Execute_DefaultVerbWithoutDefaultValue()
+ {
+ var printer = new Printer();
+ var sample = new Sample_02_No_Default { Printer = printer };
+
+ Parser.Run(new string[]
+ {
+ }, sample);
+
+ Assert.AreEqual(null, printer.PrintedTexts[0]);
+ }
+
+ [Test]
public void Execute_DefaultVerb_Switch()
{
var printer = new Printer();

0 comments on commit ff18a8e

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