Permalink
Browse files

Add better detection of short and long flags

  • Loading branch information...
1 parent 5f6ba1c commit bb23f0692d83166b0844826d7fc634371667677d @chadmyers committed Apr 15, 2012
Showing with 70 additions and 1 deletion.
  1. +54 −0 src/FubuCore.Testing/CommandLine/InputParserTester.cs
  2. +16 −1 src/FubuCore/CommandLine/InputParser.cs
View
54 src/FubuCore.Testing/CommandLine/InputParserTester.cs
@@ -266,6 +266,60 @@ public void enumeration_short_flag_positive()
handle(x => x.ColorFlag, "-c", "blue").ShouldBeTrue();
theInput.ColorFlag.ShouldEqual(Color.blue);
}
+
+ [Test]
+ public void IsFlag_should_match_for_short_flag()
+ {
+ InputParser.IsFlag("-x").ShouldBeTrue();
+ }
+
+ [Test]
+ public void IsFlag_should_match_for_long_flag()
+ {
+ InputParser.IsFlag("--xerces").ShouldBeTrue();
+ }
+
+ [Test]
+ public void IsFlag_negative()
+ {
+ InputParser.IsFlag("x").ShouldBeFalse();
+ }
+
+ [Test]
+ public void IsFlag_negative_2()
+ {
+ InputParser.IsFlag("---x").ShouldBeFalse();
+ }
+
+ [Test]
+ public void IsShortFlag_should_match_for_short_flag()
+ {
+ InputParser.IsShortFlag("-x").ShouldBeTrue();
+ }
+
+ [Test]
+ public void IsShortFlag_should_not_match_for_long_flag()
+ {
+ InputParser.IsShortFlag("--xerces").ShouldBeFalse();
+ }
+
+ [Test]
+ public void IsLongFlag_should_not_match_for_short_flag()
+ {
+ InputParser.IsLongFlag("-x").ShouldBeFalse();
+ }
+
+ [Test]
+ public void IsLongFlag_should_match_for_long_flag()
+ {
+ InputParser.IsLongFlag("--xerces").ShouldBeTrue();
+ }
+
+ [Test]
+ public void IsLongFlag_should_not_match_for_triple_long_flag()
+ {
+ InputParser.IsLongFlag("---xerces").ShouldBeFalse();
+ }
}
View
17 src/FubuCore/CommandLine/InputParser.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
+using System.Text.RegularExpressions;
using FubuCore.Conversion;
using FubuCore.Reflection;
@@ -10,7 +11,11 @@ namespace FubuCore.CommandLine
public static class InputParser
{
private static readonly string LONG_FLAG_PREFIX = "--";
+ private static readonly Regex LONG_FLAG_REGEX = new Regex("^{0}[^-]+".ToFormat(LONG_FLAG_PREFIX));
+
private static readonly string SHORT_FLAG_PREFIX = "-";
+ private static readonly Regex SHORT_FLAG_REGEX = new Regex("^{0}[^-]+".ToFormat(SHORT_FLAG_PREFIX));
+
private static readonly string FLAG_SUFFIX = "Flag";
private static readonly ObjectConverter _converter = new ObjectConverter();
@@ -45,7 +50,17 @@ public static ITokenHandler BuildHandler(PropertyInfo property)
public static bool IsFlag(string token)
{
- return token.StartsWith(SHORT_FLAG_PREFIX) || token.StartsWith(LONG_FLAG_PREFIX);
+ return IsShortFlag(token) || IsLongFlag(token);
+ }
+
+ public static bool IsShortFlag(string token)
+ {
+ return SHORT_FLAG_REGEX.IsMatch(token);
+ }
+
+ public static bool IsLongFlag(string token)
+ {
+ return LONG_FLAG_REGEX.IsMatch(token);
}
public static bool IsFlagFor(string token, PropertyInfo property)

0 comments on commit bb23f06

Please sign in to comment.