Skip to content
Browse files

critical bug fix. cannot run parser when multiple params have the sam…

…e attribute
  • Loading branch information...
1 parent 6572d54 commit 2fbef2e2aa94a0d76a360ed7e041930fc33c4687 @adrianaisemberg committed Mar 25, 2012
Showing with 29 additions and 9 deletions.
  1. +8 −7 CLAP/ParserRunner.cs
  2. +5 −2 ConsoleTest/Program.cs
  3. +10 −0 Tests/Samples.cs
  4. +6 −0 Tests/Tests.cs
View
15 CLAP/ParserRunner.cs
@@ -400,30 +400,31 @@ private static void ValidateDuplicateGlobals(Type type, ParserRegistration regis
private static void ValidateParameterDefaults(IEnumerable<Method> verbs)
{
- var parameters = verbs.SelectMany(v => v.MethodInfo.GetParameters());
+ var parameters = verbs.SelectMany(v => v.MethodInfo.GetParameters()).ToList();
+
var dict = parameters.
Where(p => p.HasAttribute<ParameterAttribute>()).
- ToDictionary(p => p.GetAttribute<ParameterAttribute>(), p => p);
+ ToDictionary(p => p, p => p.GetAttribute<ParameterAttribute>());
// find one with both a Default and a DefaultProvider
//
- var bad = dict.Where(kvp => kvp.Key.DefaultProvider != null && kvp.Key.Default != null);
+ var bad = dict.Where(kvp => kvp.Value.DefaultProvider != null && kvp.Value.Default != null);
if (bad.Any())
{
- throw new AmbiguousParameterDefaultException(bad.First().Value);
+ throw new AmbiguousParameterDefaultException(bad.First().Key);
}
// make sure all default providers are DefaultProvider
//
bad = dict.Where(kvp =>
- kvp.Key.DefaultProvider != null &&
- !typeof(DefaultProvider).IsAssignableFrom(kvp.Key.DefaultProvider));
+ kvp.Value.DefaultProvider != null &&
+ !typeof(DefaultProvider).IsAssignableFrom(kvp.Value.DefaultProvider));
if (bad.Any())
{
- throw new InvalidParameterDefaultProviderException(bad.First().Value);
+ throw new InvalidParameterDefaultProviderException(bad.First().Key);
}
}
View
7 ConsoleTest/Program.cs
@@ -16,7 +16,8 @@ static int Main(string[] args)
//Debugger.Launch();
//Parser.RunConsole<TheApp>(args);
- return Parser.RunConsole<ClapApp, SomeApp, TheApp>(args);
+ //Parser.RunConsole<ClapApp, SomeApp, TheApp>(args);
+ return Parser.RunConsole<SomeApp>(args);
//Parser.Run<SomeApp, ClapApp>(args);
}
}
@@ -38,7 +39,9 @@ public static void Foo([Parameter(Default = "zoo")]string bar)
class SomeApp
{
[Verb(IsDefault = true)]
- static void Foo(string x)
+ static void Foo(
+ [Parameter(Required = true, Default = "aaa")] string x,
+ [Parameter(Required = true)] string y)
{
Console.WriteLine(x);
}
View
10 Tests/Samples.cs
@@ -1369,4 +1369,14 @@ public class PhoneNumber
[MoreThan(10)]
public int SomeNumber { get; set; }
}
+
+ public class Sample_67
+ {
+ [Verb]
+ public static void Foo(
+ [Parameter(Required = true)] string x,
+ [Parameter(Required = true)] string y)
+ {
+ }
+ }
}
View
6 Tests/Tests.cs
@@ -2450,5 +2450,11 @@ public void DeserializeComplexTypeWithCollection_Xml()
Assert.AreEqual("Adrian", s.Person.Name);
Assert.AreEqual(3, s.Person.PhoneNumbers.Count);
}
+
+ [Test]
+ public void ParametersWithSameAttributes_NoError()
+ {
+ Parser.Run<Sample_67>(new[] { "foo", "-x=a", "-y=b" });
+ }
}
}

0 comments on commit 2fbef2e

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