Permalink
Browse files

more multiparser implementations and tests

  • Loading branch information...
1 parent 42825cd commit 8eebdb27b37638a77ebefb16e62708831e8750ee @adrianaisemberg adrianaisemberg committed Oct 30, 2011
Showing with 377 additions and 86 deletions.
  1. +1 −0 CLAP/CLAP.csproj
  2. +17 −2 CLAP/Exceptions.cs
  3. +35 −0 CLAP/MethodInvoker.cs
  4. +41 −22 CLAP/MultiParser.cs
  5. +87 −10 CLAP/Parser.cs
  6. +34 −30 CLAP/ParserRunner.cs
  7. +1 −1 CLAP/Serialization.cs
  8. +22 −0 CLAP/Utils.cs
  9. +111 −0 Tests/MultiParserTests.cs
  10. +16 −0 Tests/Samples.cs
  11. +11 −21 Tests/Tests.cs
  12. +1 −0 Tests/Tests.csproj
View
@@ -88,6 +88,7 @@
<Compile Include="Interception\VerbExecutionContext.cs" />
<Compile Include="Interception\VerbInterception.cs" />
<Compile Include="Method.cs" />
+ <Compile Include="MethodInvoker.cs" />
<Compile Include="MultiParser.cs" />
<Compile Include="Parameter.cs" />
<Compile Include="ParameterAttribute.cs" />
View
@@ -44,16 +44,31 @@ public MissingDefaultVerbException()
}
[Serializable]
- public class MissingVerbException : CommandLineException
+ public class VerbNotFoundException : CommandLineException
{
public string Verb { get; private set; }
- public MissingVerbException(string verb)
+ public VerbNotFoundException(string verb)
: base("Verb '{0}' was not found".FormatWith(verb))
{
Verb = verb;
}
+ public VerbNotFoundException(string message, Exception inner) : base(message, inner) { }
+ protected VerbNotFoundException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+ }
+
+ [Serializable]
+ public class MissingVerbException : CommandLineException
+ {
+ public MissingVerbException()
+ : this("Arguments contain no verb", null)
+ {
+ }
+
public MissingVerbException(string message, Exception inner) : base(message, inner) { }
protected MissingVerbException(
System.Runtime.Serialization.SerializationInfo info,
View
@@ -0,0 +1,35 @@
+using System.Diagnostics;
+using System.Reflection;
+
+namespace CLAP
+{
+ public static class MethodInvoker
+ {
+ public static IMethodInvoker Invoker { get; set; }
+
+ static MethodInvoker()
+ {
+ Invoker = new DefaultMethodInvoker();
+ }
+
+ public static void Invoke(MethodInfo method, object obj, object[] parameters)
+ {
+ Debug.Assert(method != null);
+
+ Invoker.Invoke(method, obj, parameters);
+ }
+
+ private class DefaultMethodInvoker : IMethodInvoker
+ {
+ public void Invoke(MethodInfo method, object obj, object[] parameters)
+ {
+ method.Invoke(obj, parameters);
+ }
+ }
+ }
+
+ public interface IMethodInvoker
+ {
+ void Invoke(MethodInfo method, object obj, object[] parameters);
+ }
+}
View
@@ -21,10 +21,28 @@ public abstract class MultiParser
#region Constructors
- public MultiParser()
+ protected MultiParser(params Type[] types)
+ {
+ m_types = types;
+
+ Init();
+ }
+
+ protected MultiParser()
{
m_types = GetType().GetGenericArguments();
+ Init();
+ }
+
+ #endregion Constructors
+
+ #region Methods
+
+ private void Init()
+ {
+ Debug.Assert(m_types.Any());
+
foreach (var type in m_types)
{
ParserRunner.Validate(type);
@@ -33,22 +51,18 @@ public MultiParser()
Register = new ParserRegistration(GetHelpString, ValuesFactory.GetValueForParameter);
}
- #endregion Constructors
-
- #region Methods
-
- public void Run(string[] args)
+ public void StaticRun(string[] args)
{
- Run(args, null);
+ RunTargets(args, null);
}
- public void Run(string[] args, object obj)
+ internal void RunTargets(string[] args, params object[] targets)
{
// no args
//
if (args.None() || args.All(a => string.IsNullOrEmpty(a)))
{
- HandleEmptyArguments(obj);
+ HandleEmptyArguments(targets);
return;
}
@@ -57,21 +71,27 @@ public void Run(string[] args, object obj)
if (m_types.Length == 1)
{
- parser = GetSingleTypeParser(args, obj, Register);
+ parser = GetSingleTypeParser(args, targets, Register);
}
else
{
Debug.Assert(m_types.Length > 1);
- parser = GetMultiTypesParser(args, obj, Register);
+ parser = GetMultiTypesParser(args, targets, Register);
}
Debug.Assert(parser != null);
- parser.Run(args, obj);
+ var index = m_types.IndexOf(parser.Type);
+
+ Debug.Assert(index >= 0);
+
+ var target = targets.None() ? null : targets[index];
+
+ parser.Run(args, target);
}
- private void HandleEmptyArguments(object target)
+ private void HandleEmptyArguments(object[] targets)
{
if (Register.RegisteredEmptyHandler != null)
{
@@ -81,6 +101,8 @@ private void HandleEmptyArguments(object target)
{
var parser = new ParserRunner(m_types.First(), Register);
+ var target = targets == null ? null : targets[0];
+
parser.HandleEmptyArguments(target);
}
}
@@ -94,18 +116,15 @@ public string GetHelpString()
private ParserRunner GetMultiTypesParser(string[] args, object obj, ParserRegistration registration)
{
- if (args.Length == 0)
- {
-#warning TODO:
- throw new Exception("Multi parser needs a verb");
- }
+ Debug.Assert(args.Any());
var verb = args[0];
- if (ParserRunner.Prefixes.Any(p => verb.StartsWith(p)))
+ // if the first arg is not a verb - throw
+ //
+ if (verb.StartsWith(ParserRunner.ArgumentPrefixes))
{
-#warning TODO:
- throw new Exception("no verb");
+ throw new MissingVerbException();
}
if (!s_delimiters.Any(d => verb.Contains(d)))
@@ -147,7 +166,7 @@ private ParserRunner GetSingleTypeParser(string[] args, object obj, ParserRegist
// if there is no verb - leave all the args as is
//
- if (ParserRunner.Prefixes.Any(p => verb.StartsWith(p)))
+ if (verb.StartsWith(ParserRunner.ArgumentPrefixes))
{
return parser;
}
View
@@ -1,27 +1,104 @@
-
+using System;
+using System.Diagnostics;
+using System.Linq;
+
namespace CLAP
{
/// <summary>
/// A command-line arguments parser
/// </summary>
- public static class Parser
+ public class Parser : MultiParser
{
- public static void Run<T>(string[] args, T t)
+ public Parser(params Type[] types)
+ : base(types)
{
- var p = new Parser<T>();
+ }
- p.Run(args, t);
+ public void Run(string[] args, params object[] targets)
+ {
+ RunTargets(args, targets);
}
- public static void Run<T>(string[] args)
+ public static void DynamicRun(string[] args, params object[] targets)
{
- var p = new Parser<T>();
+ Debug.Assert(targets.Any());
+ Debug.Assert(targets.All(t => t != null));
+
+ var p = new Parser(targets.Select(t => t.GetType()).ToArray());
- p.Run(args);
+ ((MultiParser)p).RunTargets(args, targets);
}
+
+ public static void Run<T>(string[] args)
+ { new Parser<T>().RunTargets(args); }
+ public static void Run<T>(string[] args, T t)
+ { new Parser<T>().Run(args, t); }
+
+ public static void Run<T1, T2>(string[] args)
+ { new Parser<T1, T2>().RunTargets(args); }
+ public static void Run<T1, T2>(string[] args, T1 t1, T2 t2)
+ { new Parser<T1, T2>().Run(args, t1, t2); }
+
+ public static void Run<T1, T2, T3>(string[] args)
+ { new Parser<T1, T2, T3>().RunTargets(args); }
+ public static void Run<T1, T2, T3>(string[] args, T1 t1, T2 t2, T3 t3)
+ { new Parser<T1, T2, T3>().Run(args, t1, t2, t3); }
+
+ public static void Run<T1, T2, T3, T4>(string[] args)
+ { new Parser<T1, T2, T3, T4>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4)
+ { new Parser<T1, T2, T3, T4>().Run(args, t1, t2, t3, t4); }
+
+ public static void Run<T1, T2, T3, T4, T5>(string[] args)
+ { new Parser<T1, T2, T3, T4, T5>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4, T5>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
+ { new Parser<T1, T2, T3, T4, T5>().Run(args, t1, t2, t3, t4, t5); }
+
+ public static void Run<T1, T2, T3, T4, T5, T6>(string[] args)
+ { new Parser<T1, T2, T3, T4, T5, T6>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4, T5, T6>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
+ { new Parser<T1, T2, T3, T4, T5, T6>().Run(args, t1, t2, t3, t4, t5, t6); }
+
+ public static void Run<T1, T2, T3, T4, T5, T6, T7>(string[] args)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4, T5, T6, T7>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7>().Run(args, t1, t2, t3, t4, t5, t6, t7); }
+
+ public static void Run<T1, T2, T3, T4, T5, T6, T7, T8>(string[] args)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7, T8>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4, T5, T6, T7, T8>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7, T8>().Run(args, t1, t2, t3, t4, t5, t6, t7, t8); }
+
+ public static void Run<T1, T2, T3, T4, T5, T6, T7, T8, T9>(string[] args)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7, T8, T9>().RunTargets(args); }
+ public static void Run<T1, T2, T3, T4, T5, T6, T7, T8, T9>(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9)
+ { new Parser<T1, T2, T3, T4, T5, T6, T7, T8, T9>().Run(args, t1, t2, t3, t4, t5, t6, t7, t8, t9); }
}
public class Parser<T> : MultiParser
- {
- }
+ { public void Run(string[] args, T t) { base.RunTargets(args, t); } }
+
+ public class Parser<T1, T2> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2) { base.RunTargets(args, t1, t2); } }
+
+ public class Parser<T1, T2, T3> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3) { base.RunTargets(args, t1, t2, t3); } }
+
+ public class Parser<T1, T2, T3, T4> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4) { base.RunTargets(args, t1, t2, t3, t4); } }
+
+ public class Parser<T1, T2, T3, T4, T5> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { base.RunTargets(args, t1, t2, t3, t4, t5); } }
+
+ public class Parser<T1, T2, T3, T4, T5, T6> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) { base.RunTargets(args, t1, t2, t3, t4, t5, t6); } }
+
+ public class Parser<T1, T2, T3, T4, T5, T6, T7> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) { base.RunTargets(args, t1, t2, t3, t4, t5, t6, t7); } }
+
+ public class Parser<T1, T2, T3, T4, T5, T6, T7, T8> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) { base.RunTargets(args, t1, t2, t3, t4, t5, t6, t7, t8); } }
+
+ public class Parser<T1, T2, T3, T4, T5, T6, T7, T8, T9> : MultiParser
+ { public void Run(string[] args, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) { base.RunTargets(args, t1, t2, t3, t4, t5, t6, t7, t8, t9); } }
}
Oops, something went wrong.

0 comments on commit 8eebdb2

Please sign in to comment.