Permalink
Browse files

refactor: moved all help generation to HelpGenerator

  • Loading branch information...
1 parent 20a9a8e commit 2920911e3594f3143cdefcc7bd452f2456f147d8 @adrianaisemberg adrianaisemberg committed Nov 1, 2011
Showing with 19 additions and 190 deletions.
  1. +1 −0 CLAP/CLAP.csproj
  2. +1 −2 CLAP/ErrorAttribute.cs
  3. +6 −17 CLAP/MultiParser.cs
  4. +11 −171 CLAP/ParserRunner.cs
View
@@ -85,6 +85,7 @@
<Compile Include="EmptyAttribute.cs" />
<Compile Include="Exceptions.cs" />
<Compile Include="GlobalAttribute.cs" />
+ <Compile Include="HelpGenerator.cs" />
<Compile Include="Interception\IVerbInterceptor.cs" />
<Compile Include="Interception\PostVerbExecutionAttribute.cs" />
<Compile Include="Interception\PostVerbExecutionContext.cs" />
@@ -4,12 +4,11 @@ namespace CLAP
{
/// <summary>
/// Marks a method to be executed when an exception occurs.
- /// The method may accept no parameters or only a single System.Exception
+ /// The method may only accept one parameter of type CLAP.ExceptionContext
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ErrorAttribute : Attribute
{
-#warning TODO: change to return a bool instead
}
}
View
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
-using System.Text;
namespace CLAP
{
@@ -19,6 +18,11 @@ public abstract class MultiParser
#region Properties
+ internal Type[] Types
+ {
+ get { return m_types; }
+ }
+
/// <summary>
/// Parser registration
/// </summary>
@@ -214,22 +218,7 @@ public void RunTargets(string[] args, params object[] targets)
/// </summary>
public string GetHelpString()
{
-#warning TODO: this is crap
- 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();
+ return HelpGenerator.GetHelp(this);
}
#endregion Public Methods
View
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
-using System.Text;
using CLAP.Interception;
#if !FW2
@@ -28,6 +27,11 @@ internal class ParserRunner
internal Type Type { get; private set; }
+ internal ParserRegistration Register
+ {
+ get { return m_registration; }
+ }
+
#endregion Properties
#region Constructors
@@ -52,92 +56,6 @@ public void Run(string[] args, object obj)
TryRunInternal(args, obj);
}
- /// <summary>
- /// Returns a help string
- /// </summary>
- public string GetHelpString()
- {
- var verbs = GetVerbs();
-
- if (verbs.None())
- {
- return string.Empty;
- }
-
- var sb = new StringBuilder();
-
- foreach (var verb in verbs)
- {
- sb.AppendLine();
-
- sb.Append(verb.Names.StringJoin("/")).Append(":");
-
- if (verb.IsDefault)
- {
- sb.Append(" [Default]");
- }
-
- if (!string.IsNullOrEmpty(verb.Description))
- {
- sb.AppendFormat(" {0}", verb.Description);
- }
-
- var validators = verb.MethodInfo.GetInterfaceAttributes<ICollectionValidation>();
-
- if (validators.Any())
- {
- sb.AppendLine();
- sb.AppendLine("Validation:");
-
- foreach (var validator in validators)
- {
- sb.AppendLine("- {0}".FormatWith(validator.Description));
- }
- }
-
- sb.AppendLine();
-
- var parameters = GetParameters(verb.MethodInfo);
-
- foreach (var p in parameters)
- {
- sb.AppendLine(" -{0}: {1}".FormatWith(p.Names.StringJoin("/"), GetParameterOption(p)));
- }
- }
-
- var definedGlobals = GetDefinedGlobals();
-
- if (m_registration.RegisteredGlobalHandlers.Any() || definedGlobals.Any())
- {
- sb.AppendLine();
- sb.AppendLine("Global parameters:");
-
- foreach (var handler in m_registration.RegisteredGlobalHandlers.Values)
- {
- sb.AppendLine(" -{0}: {1} [{2}]".FormatWith(handler.Name, handler.Desription, handler.Type.Name));
- }
-
- foreach (var handler in definedGlobals)
- {
- sb.AppendLine(" -{0}".FormatWith(GetDefinedGlobalHelpString(handler)));
-
- var validators = handler.GetInterfaceAttributes<ICollectionValidation>();
-
- if (validators.Any())
- {
- sb.AppendLine(" Validation:");
-
- foreach (var validator in validators)
- {
- sb.AppendLine(" {0}".FormatWith(validator.Description));
- }
- }
- }
- }
-
- return sb.ToString();
- }
-
#endregion Public Methods
#region Private Methods
@@ -493,45 +411,6 @@ private static void ValidateDefinedEmptyHandlers(Type type)
}
}
- private string GetDefinedGlobalHelpString(MethodInfo method)
- {
- var sb = new StringBuilder();
-
- // name
- //
- var globalAtt = method.GetAttribute<GlobalAttribute>();
- var name = globalAtt.Name ?? method.Name;
- sb.Append(name);
-
- foreach (var alias in globalAtt.Aliases)
- {
- sb.AppendFormat("/{0}", alias);
- }
-
- sb.Append(": ");
-
- // description
- //
- sb.Append(globalAtt.Description);
- sb.Append(" ");
-
- // type
- //
- var parameters = GetParameters(method);
- if (parameters.Any())
- {
- var p = parameters.First();
-
- sb.Append(GetParameterOption(p));
- }
- else
- {
- sb.AppendFormat("[Boolean]");
- }
-
- return sb.ToString();
- }
-
private Action<string> GetHelpHandler(string input)
{
Debug.Assert(!string.IsNullOrEmpty(input));
@@ -547,45 +426,6 @@ private Action<string> GetHelpHandler(string input)
}
/// <summary>
- /// Used for GetHelpString
- /// </summary>
- private static string GetParameterOption(Parameter p)
- {
- var sb = new StringBuilder();
-
- sb.AppendFormat("{0} [", p.Description);
-
- sb.Append(p.ParameterInfo.ParameterType.Name);
-
- if (p.ParameterInfo.ParameterType.IsEnum)
- {
- var values = Enum.GetNames(p.ParameterInfo.ParameterType);
-
- sb.AppendFormat(":[{0}]", values.StringJoin(","));
- }
-
- if (p.Required)
- {
- sb.Append(", Required");
- }
- if (p.Default != null)
- {
- sb.AppendFormat(", Default = {0}", p.Default);
- }
-
- var validationAttributes = p.ParameterInfo.GetAttributes<ValidationAttribute>();
-
- foreach (var validationAttribute in validationAttributes)
- {
- sb.AppendFormat(", {0}", validationAttribute.Description);
- }
-
- sb.Append("]");
-
- return sb.ToString();
- }
-
- /// <summary>
/// Creates a map of the input arguments and their string values
/// </summary>
private static Dictionary<string, string> MapArguments(IEnumerable<string> args)
@@ -636,7 +476,7 @@ private static string GetParameterOption(Parameter p)
/// <summary>
/// Create a list of parameters for the given method
/// </summary>
- private static IEnumerable<Parameter> GetParameters(MethodInfo method)
+ internal static IEnumerable<Parameter> GetParameters(MethodInfo method)
{
var parameters = method.GetParameters().Select(p => new Parameter(p));
@@ -660,7 +500,7 @@ private static IEnumerable<Parameter> GetParameters(MethodInfo method)
/// <summary>
/// Create a list of methods (verbs) for the given type
/// </summary>
- private IEnumerable<Method> GetVerbs()
+ internal IEnumerable<Method> GetVerbs()
{
var verbMethods = Type.GetMethodsWith<VerbAttribute>().
Select(m => new Method(m));
@@ -707,7 +547,7 @@ private void HandleRegisteredGlobals(Dictionary<string, string> args)
}
}
- private IEnumerable<MethodInfo> GetDefinedGlobals()
+ internal IEnumerable<MethodInfo> GetDefinedGlobals()
{
var globals = Type.GetMethodsWith<GlobalAttribute>();
@@ -816,7 +656,7 @@ private bool HandleHelp(string firstArg, object target)
if (helpHandler != null)
{
- help = GetHelpString();
+ help = HelpGenerator.GetHelp(this);
helpHandler(help);
@@ -842,7 +682,7 @@ private bool HandleHelp(string firstArg, object target)
if (help == null)
{
- help = GetHelpString();
+ help = HelpGenerator.GetHelp(this);
}
MethodInvoker.Invoke(method, obj, new[] { help });
@@ -886,7 +726,7 @@ internal void HandleEmptyArguments(object target)
//
if (method.HasAttribute<HelpAttribute>())
{
- var help = GetHelpString();
+ var help = HelpGenerator.GetHelp(this);
// method should execute because it was already passed validation
//

0 comments on commit 2920911

Please sign in to comment.