Permalink
Browse files

1. Added all help handlers as global parameters in the help string

2. Bug fix: errors when handling empty args for default vebrs were not handled (premature error handling)
3. Change: RunConsole() registers empty help handlers only for multi-parsers. A single parser executed with RunConsole will not have an empty help handler so default vebrs are executed instead.
  • Loading branch information...
1 parent 2d9a1c1 commit 8f6201c8170337068731b9667dccb917b27764c7 @adrianaisemberg committed May 7, 2012
Showing with 50 additions and 19 deletions.
  1. +7 −1 CLAP/EnvironmentParserHandlers.cs
  2. +29 −5 CLAP/HelpGenerator.cs
  3. +8 −8 CLAP/MultiParser.cs
  4. +2 −2 CLAP/ParserRegistration.cs
  5. +4 −3 ConsoleTest/Program.cs
View
8 CLAP/EnvironmentParserHandlers.cs
@@ -9,10 +9,16 @@ internal static class EnvironmentParserHandlers
internal static MultiParser Console(this MultiParser parser)
{
parser.Register.HelpHandler("help,h,?", help => System.Console.WriteLine(help));
- parser.Register.EmptyHelpHandler(help => System.Console.WriteLine(help));
parser.Register.ParameterHandler("debug", () => Debugger.Launch());
parser.Register.ErrorHandler(c => System.Console.Error.WriteLine(c.Exception.Message));
+ // a multi parser needs an empty help handler
+ //
+ if (parser.Types.Length > 1)
+ {
+ parser.Register.EmptyHelpHandler(help => System.Console.WriteLine(help));
+ }
+
return parser;
}
View
34 CLAP/HelpGenerator.cs
@@ -88,7 +88,12 @@ private static string GetHelpString(HelpInfo helpInfo)
sb.AppendFormat("{0} ", p.Description);
}
- sb.AppendFormat("({0}) ", GetTypeName(p.Type));
+ var typeName = GetTypeName(p.Type);
+
+ if (!string.IsNullOrEmpty(typeName))
+ {
+ sb.AppendFormat("({0}) ", typeName);
+ }
if (p.Required)
{
@@ -148,14 +153,19 @@ private static string GetHelpString(HelpInfo helpInfo)
sb.AppendFormat("{0} ", g.Description);
}
- sb.AppendFormat("({0}) ", GetTypeName(g.Type));
+ var typeName = GetTypeName(g.Type);
+
+ if (!string.IsNullOrEmpty(typeName))
+ {
+ sb.AppendFormat("({0}) ", typeName);
+ }
if (g.Separator != null && g.Separator != SeparatorAttribute.DefaultSeparator)
{
sb.AppendFormat("(Separator = {0}) ", g.Separator);
}
- if (g.Validations.Any())
+ if (g.Validations != null && g.Validations.Any())
{
sb.AppendFormat("({0}) ", g.Validations.StringJoin(", "));
}
@@ -164,6 +174,7 @@ private static string GetHelpString(HelpInfo helpInfo)
} // foreach (var g in parser.Globals
}
+
if (multi && i < count - 1)
{
sb.AppendLine();
@@ -182,6 +193,11 @@ private static string GetTypeName(Type type)
return string.Format("{0} ({1})", type.Name, string.Join("/", Enum.GetNames(type)));
}
+ if (type == typeof(bool))
+ {
+ return string.Empty;
+ }
+
return type.GetGenericTypeName();
}
@@ -229,9 +245,17 @@ private static ParserHelpInfo GetParserHelp(ParserRunner parser)
{
Names = handler.Names.OrderBy(n => n.Length).ToList(),
Type = handler.Type,
- Description = handler.Desription,
+ Description = handler.Description,
Validations = new List<string>(),
- })).ToList(),
+ })).Union(new GlobalParameterHelpInfo[]
+ {
+ new GlobalParameterHelpInfo
+ {
+ Names=parser.Register.RegisteredHelpHandlers.Keys.ToList(),
+ Type = typeof(bool),
+ Description = "Help",
+ }
+ }).ToList(),
};
}
}
View
16 CLAP/MultiParser.cs
@@ -188,17 +188,17 @@ public int RunTargets(string[] args, params object[] targets)
{
ParserRunner parser = null;
- // no args
- //
- if (args.None() || args.All(a => string.IsNullOrEmpty(a)))
+ try
{
- HandleEmptyArguments(targets);
+ // no args
+ //
+ if (args.None() || args.All(a => string.IsNullOrEmpty(a)))
+ {
+ HandleEmptyArguments(targets);
- return SuccessCode;
- }
+ return SuccessCode;
+ }
- try
- {
if (m_types.Length == 1)
{
parser = GetSingleTypeParser(args, targets, Register);
View
4 CLAP/ParserRegistration.cs
@@ -284,7 +284,7 @@ private void RegisterParameterHandlerInternal<TParameter>(string names, Action<T
{
Names = names.CommaSplit(),
Handler = objectAction,
- Desription = options.Description,
+ Description = options.Description,
Separator = options.Separator,
Type = typeof(TParameter),
});
@@ -333,7 +333,7 @@ internal class GlobalParameterHandler
{
internal IEnumerable<string> Names { get; set; }
internal Action<string> Handler { get; set; }
- internal string Desription { get; set; }
+ internal string Description { get; set; }
internal string Separator { get; set; }
internal Type Type { get; set; }
}
View
7 ConsoleTest/Program.cs
@@ -16,10 +16,11 @@ static int Main(string[] args)
//Debugger.Launch();
//Parser.RunConsole<TheApp>(args);
- //Parser.RunConsole<ClapApp, SomeApp, TheApp>(args);
+ return Parser.RunConsole<ClapApp, SomeApp, TheApp>(args);
+ //return Parser.RunConsole<ClapApp>(args);
//return Parser.RunConsole<SomeApp>(args);
//Parser.Run<SomeApp, ClapApp>(args);
- return Parser.Run<DefaultApp>(args);
+ //return Parser.RunConsole<DefaultApp>(args);
}
}
@@ -156,7 +157,7 @@ public override string Description
}
}
- [Help, Empty]
+ //[Help, Empty]
public static void Help(string h)
{
Console.WriteLine(h);

0 comments on commit 8f6201c

Please sign in to comment.