Permalink
Browse files

removed the ambiguous thing. empty handlers get a priority over defau…

…lt verbs
  • Loading branch information...
1 parent d09ce1e commit aab71674726b3e77b37c6205e4c6e471cab4e2ae @adrianaisemberg committed Nov 13, 2011
Showing with 16 additions and 119 deletions.
  1. +0 −33 CLAP/Exceptions.cs
  2. +0 −17 CLAP/ParserRegistration.cs
  3. +0 −36 CLAP/ParserRunner.cs
  4. +16 −1 ConsoleTest/Program.cs
  5. +0 −32 Tests/Tests.cs
View
33 CLAP/Exceptions.cs
@@ -377,37 +377,4 @@ public InvalidHelpHandlerException(MethodInfo method, Exception ex)
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
-
- [Serializable]
- public class AmbiguousEmptyHandlerException : CommandLineParserException
- {
- /// <summary>
- /// The default verb that has no arguments
- /// </summary>
- public MethodInfo Verb { get; private set; }
-
- /// <summary>
- /// The method that is defined as Empty
- /// </summary>
- public MethodInfo Empty { get; private set; }
-
- public AmbiguousEmptyHandlerException(Type type, MethodInfo verb)
- : base("Ambiguous empty handlers: {0}.{1}. When a verb that has no parameters is declared as IsDefault, an empty handler cannot be registered.".
- FormatWith(type.Name, verb.Name))
- {
- }
-
- public AmbiguousEmptyHandlerException(MethodInfo verb, MethodInfo empty)
- : base("Ambiguous empty handlers: {0}, {1}. When a method is marked as [Empty], a verb that has no parameters cannot be declared as IsDefault.".
- FormatWith(verb.Name, empty.Name))
- {
- Verb = verb;
- Empty = empty;
- }
-
- protected AmbiguousEmptyHandlerException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
}
View
17 CLAP/ParserRegistration.cs
@@ -98,8 +98,6 @@ public void EmptyHandler(Action handler)
}
RegisteredEmptyHandler = handler;
-
- ValidateEmptyAndDefault();
}
/// <summary>
@@ -290,21 +288,6 @@ private void RegisterHelpHandlerInternal(IEnumerable<string> names, Action<strin
}
}
- private void ValidateEmptyAndDefault()
- {
- Debug.Assert(RegisteredEmptyHandler != null);
-
- foreach (var type in m_types)
- {
- var defaultEmpty = ParserRunner.GetDefaultEmptyVerb(type);
-
- if (defaultEmpty != null)
- {
- throw new AmbiguousEmptyHandlerException(type, defaultEmpty.MethodInfo);
- }
- }
- }
-
#endregion Private Methods
}
View
36 CLAP/ParserRunner.cs
@@ -357,42 +357,6 @@ internal static void Validate(Type type)
//
ValidateDefinedPreInterceptors(type);
ValidateDefinedPostInterceptors(type);
-
- // can't have both empty handler and default empty verb
- //
- ValidateEmptyAndDefault(type);
- }
-
- private static void ValidateEmptyAndDefault(Type type)
- {
- var definedEmptyHandlers = type.GetMethodsWith<EmptyAttribute>();
-
- if (!definedEmptyHandlers.Any())
- {
- return;
- }
-
- var defaultEmptyVerb = GetDefaultEmptyVerb(type);
-
- // if the default verb has no args - throw
- //
- if (defaultEmptyVerb != null)
- {
- throw new AmbiguousEmptyHandlerException(defaultEmptyVerb.MethodInfo, definedEmptyHandlers.First());
- }
- }
-
- internal static Method GetDefaultEmptyVerb(Type type)
- {
- var verbMethods = type.GetMethodsWith<VerbAttribute>().
- Select(m => new Method(m)).
- Where(m => m.IsDefault && !m.MethodInfo.GetParameters().Any());
-
- // this should have been validated already
- //
- Debug.Assert(verbMethods.Count() <= 1);
-
- return verbMethods.FirstOrDefault();
}
private static void ValidateDefinedEmptyHandlers(Type type)
View
17 ConsoleTest/Program.cs
@@ -13,7 +13,22 @@ class Program
{
static void Main(string[] args)
{
- Parser.Run<EmptyApp>(args);
+ Parser.Run<SomeApp>(args);
+ }
+ }
+
+ class SomeApp
+ {
+ [Verb(IsDefault = true)]
+ static void Foo(string x)
+ {
+ Console.WriteLine(x);
+ }
+
+ [Empty, Help]
+ static void Help(string h)
+ {
+ Console.WriteLine(h);
}
}
View
32 Tests/Tests.cs
@@ -2077,38 +2077,6 @@ public void Interception_DefinedOverType_PreAndPostExecuted_VerbExecuted()
}
[Test]
- [ExpectedException(typeof(AmbiguousEmptyHandlerException))]
- public void TestAmbiguous_Exception_1()
- {
- Parser.Run<Sample_53>(new string[] { });
- }
-
- [Test]
- [ExpectedException(typeof(AmbiguousEmptyHandlerException))]
- public void TestAmbiguous_Exception_2()
- {
- Parser.Run<Sample_54>(new string[] { });
- }
-
- [Test]
- [ExpectedException(typeof(AmbiguousEmptyHandlerException))]
- public void TestAmbiguous_Exception_3()
- {
- var p = new Parser<Sample_55>();
-
- p.Register.EmptyHandler(delegate { });
- }
-
- [Test]
- [ExpectedException(typeof(AmbiguousEmptyHandlerException))]
- public void TestAmbiguous_Exception_4()
- {
- var p = new Parser<Sample_55>();
-
- p.Register.EmptyHelpHandler(delegate { });
- }
-
- [Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Register_EmptyHandler_Null_Exception()
{

0 comments on commit aab7167

Please sign in to comment.