Permalink
Browse files

full cover

  • Loading branch information...
1 parent fe6a62e commit 2dcb53df071226ff87a19b7697e0e0f65b96ba88 @adrianaisemberg adrianaisemberg committed Oct 30, 2011
Showing with 53 additions and 45 deletions.
  1. +22 −7 CLAP/MultiParser.cs
  2. +15 −38 CLAP/ParserRunner.cs
  3. +16 −0 Tests/Tests.cs
View
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
+using System.Text;
namespace CLAP
{
@@ -91,6 +92,25 @@ public void RunTargets(string[] args, params object[] targets)
parser.Run(args, target);
}
+ public string GetHelpString()
+ {
+ var sb = new StringBuilder();
+
+ foreach (var type in m_types)
+ {
+ var pr = new ParserRunner(type, Register);
+
+ sb.AppendLine(type.Name);
+ sb.AppendLine("".PadLeft(30, '-'));
+
+ sb.AppendLine(pr.GetHelpString());
+
+ sb.AppendLine();
+ }
+
+ return sb.ToString();
+ }
+
private void HandleEmptyArguments(object[] targets)
{
if (Register.RegisteredEmptyHandler != null)
@@ -107,13 +127,6 @@ private void HandleEmptyArguments(object[] targets)
}
}
- public string GetHelpString()
- {
- return "FOOOO!";
- }
-
- #endregion Methods
-
private ParserRunner GetMultiTypesParser(string[] args, object obj, ParserRegistration registration)
{
Debug.Assert(args.Any());
@@ -195,5 +208,7 @@ private ParserRunner GetSingleTypeParser(string[] args, object obj, ParserRegist
return parser;
}
+
+ #endregion Methods
}
}
View
@@ -47,11 +47,6 @@ internal ParserRunner(Type type, ParserRegistration parserRegistration)
#region Public Methods
- public void Run(string[] args)
- {
- TryRunInternal(args, null);
- }
-
public void Run(string[] args, object obj)
{
TryRunInternal(args, obj);
@@ -166,14 +161,12 @@ private void TryRunInternal(string[] args, object obj)
private void RunInternal(string[] args, object obj)
{
- // no args
//
- if (args.None() || args.All(a => string.IsNullOrEmpty(a)))
- {
- HandleEmptyArguments(obj);
+ // *** empty args are handled by the multi-parser
+ //
+ //
- return;
- }
+ Debug.Assert(args.Length > 0 && args.All(a => !string.IsNullOrEmpty(a)));
// the first arg should be the verb, unless there is no verb and a default is used
//
@@ -423,6 +416,7 @@ private static void ValidateVerbInput(Method method, List<object> parameterValue
internal static void Validate(Type type)
{
// no more than one default verb
+ //
var verbMethods = type.GetMethodsWith<VerbAttribute>().
Select(m => new Method(m));
@@ -480,6 +474,7 @@ private static void ValidateDefinedEmptyHandlers(Type type)
if (definedEmptyHandlerParameters.Length > 1 ||
definedEmptyHandlerParameters.First().ParameterType != typeof(string))
{
+ throw new InvalidHelpHandlerException(definedEmptyHandler);
}
}
else
@@ -874,10 +869,9 @@ private bool HandleHelp(string firstArg, object target)
internal void HandleEmptyArguments(object target)
{
- if (m_registration.RegisteredEmptyHandler != null)
- {
- m_registration.RegisteredEmptyHandler();
- }
+ //
+ // *** registered empty handlers are called by the multi-parser
+ //
var definedEmptyHandlers = Type.GetMethodsWith<EmptyAttribute>();
var definedEmptyHandlersCount = definedEmptyHandlers.Count();
@@ -893,35 +887,18 @@ internal void HandleEmptyArguments(object target)
var obj = method.IsStatic ? null : target;
// if it is a [Help] handler
+ //
if (method.HasAttribute<HelpAttribute>())
{
- try
- {
- var help = GetHelpString();
+ var help = GetHelpString();
- MethodInvoker.Invoke(method, obj, new[] { help });
- }
- catch (TargetParameterCountException ex)
- {
- throw new InvalidHelpHandlerException(method, ex);
- }
- catch (ArgumentException ex)
- {
- throw new InvalidHelpHandlerException(method, ex);
- }
+ // method should execute because it was already passed validation
+ //
+ MethodInvoker.Invoke(method, obj, new[] { help });
}
else
{
- try
- {
- MethodInvoker.Invoke(method, obj, null);
- }
- catch (TargetParameterCountException ex)
- {
- throw new ArgumentMismatchException(
- "Method '{0}' is marked as [Empty] so it should not have any parameters".FormatWith(method),
- ex);
- }
+ MethodInvoker.Invoke(method, obj, null);
}
}
View
@@ -868,6 +868,22 @@ public void RegisterEmptyHelpHandler_Called()
Assert.IsNotNull(help);
}
+ [Test]
+ public void RegisterEmptyHelpHandler_EmptyArguments_Called()
+ {
+ var p = new Parser<Sample_25>();
+
+ string help = null;
+
+ p.Register.EmptyHelpHandler(h => help = h);
+
+ Assert.IsNull(help);
+
+ p.RunStatic(new string[] { });
+
+ Assert.IsNotNull(help);
+ }
+
[Test]
[ExpectedException(typeof(MoreThanOneEmptyHandlerException))]
public void RegisterEmptyHelpHandler_MoreThanOnce_Exception()

0 comments on commit 2dcb53d

Please sign in to comment.