Skip to content

Commit 538b4dc

Browse files
committed
Fix parameter names not accepting hyphens
1 parent cc58bb0 commit 538b4dc

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

Commander.NET.Tests/IntegrationTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static IEnumerable<object[]> BasicTestCases()
2020
{
2121
Assert.AreEqual(0, b.Row);
2222
Assert.AreEqual(null, b.Name);
23+
Assert.IsFalse(b.Flag);
2324
})
2425
};
2526
yield return
@@ -30,6 +31,7 @@ public static IEnumerable<object[]> BasicTestCases()
3031
{
3132
Assert.AreEqual(5, b.Row);
3233
Assert.AreEqual(null, b.Name);
34+
Assert.IsFalse(b.Flag);
3335
})
3436
};
3537
yield return
@@ -40,6 +42,7 @@ public static IEnumerable<object[]> BasicTestCases()
4042
{
4143
Assert.AreEqual(5, b.Row);
4244
Assert.AreEqual("John", b.Name);
45+
Assert.IsFalse(b.Flag);
4346
})
4447
};
4548
yield return
@@ -50,6 +53,7 @@ public static IEnumerable<object[]> BasicTestCases()
5053
{
5154
Assert.AreEqual(0, b.Row);
5255
Assert.AreEqual("John", b.Name);
56+
Assert.IsFalse(b.Flag);
5357
})
5458
};
5559
yield return
@@ -60,6 +64,7 @@ public static IEnumerable<object[]> BasicTestCases()
6064
{
6165
Assert.AreEqual(0, b.Row);
6266
Assert.AreEqual("John&Doe", b.Name);
67+
Assert.IsFalse(b.Flag);
6368
})
6469
};
6570
yield return
@@ -71,6 +76,7 @@ public static IEnumerable<object[]> BasicTestCases()
7176
Assert.AreEqual(0, b.Row);
7277
Assert.AreEqual("John&Doe", b.Name);
7378
Assert.AreEqual(0.34, b.Positional);
79+
Assert.IsFalse(b.Flag);
7480
})
7581
};
7682
yield return
@@ -82,6 +88,39 @@ public static IEnumerable<object[]> BasicTestCases()
8288
Assert.AreEqual(12, b.Row);
8389
Assert.AreEqual("John&Doe", b.Name);
8490
Assert.AreEqual(0.34, b.Positional);
91+
Assert.IsFalse(b.Flag);
92+
})
93+
};
94+
yield return
95+
new object[]
96+
{
97+
new string[]{ "--extra-args", "someValue" },
98+
new AssertBasic(b =>
99+
{
100+
Assert.AreEqual("someValue", b.ExtraArg);
101+
Assert.IsFalse(b.Flag);
102+
})
103+
};
104+
yield return
105+
new object[]
106+
{
107+
new string[]{ "--extra-args", "someValue", "-f" },
108+
new AssertBasic(b =>
109+
{
110+
Assert.AreEqual("someValue", b.ExtraArg);
111+
Assert.IsTrue(b.Flag);
112+
})
113+
};
114+
yield return
115+
new object[]
116+
{
117+
new string[]{ "--name", "John&Doe", "0.34", "--for-sure", "-r", "12" },
118+
new AssertBasic(b =>
119+
{
120+
Assert.AreEqual(12, b.Row);
121+
Assert.AreEqual("John&Doe", b.Name);
122+
Assert.AreEqual(0.34, b.Positional);
123+
Assert.IsTrue(b.Flag);
85124
})
86125
};
87126
}

Commander.NET.Tests/Samples/Basic.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class Basic
1616

1717
[Parameter("n", "name", Required = Required.No)]
1818
public string Name;
19+
20+
[Parameter("extra-args")]
21+
public string ExtraArg = "default";
22+
23+
[Parameter("f", "for-sure")]
24+
public bool Flag;
1925

2026
[PositionalParameter(0, "positional")]
2127
public double Positional;

Commander.NET/CommanderParserT.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public CommanderParser(params string[] args) : this()
2929
Add(args);
3030
}
3131

32-
#region options
32+
#region Options
3333

3434
public CommanderParser<T> Add(params string[] args)
3535
{
@@ -75,7 +75,7 @@ public CommanderParser<T> Bindings(BindingFlags bindingFlags)
7575

7676
#endregion
7777

78-
#region parsing
78+
#region Parsing
7979

8080
/// <summary>
8181
/// Parse the given arguments - along with any other arguments added to this object - and serialize them into a new instance of a T object.
@@ -244,7 +244,7 @@ public T Parse(T obj)
244244

245245
#endregion
246246

247-
#region usage
247+
#region Usage
248248

249249
/// <summary>
250250
/// Generate the usage string based on the given type's attributes.

Commander.NET/Models/RawArguments.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ internal RawArguments<T> Parse(string[] args, Separators separators)
6666
if (string.IsNullOrWhiteSpace(args[i]))
6767
continue;
6868

69-
if ((args[i].Matches(@"^-[a-zA-Z0-9_]=\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_]{2,}=\w+$")) && separators.HasFlag(Separators.Equals)
70-
|| (args[i].Matches(@"^-[a-zA-Z0-9_]:\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_]{2,}:\w+$")) && separators.HasFlag(Separators.Colon))
69+
if ((args[i].Matches(@"^-[a-zA-Z0-9_]=\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}=\w+$")) && separators.HasFlag(Separators.Equals)
70+
|| (args[i].Matches(@"^-[a-zA-Z0-9_]:\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}:\w+$")) && separators.HasFlag(Separators.Colon))
7171
{
7272
string key = args[i].TrimStart('-').Split(':')[0].Split('=')[0];
7373
string value = args[i].Split(':').Last().Split('=').Last();
7474

7575
TryAddKeyValuePair(key, value);
7676
}
77-
else if (args[i].Matches(@"^-[a-zA-Z0-9_]$") || args[i].Matches(@"^--[a-zA-Z0-9_]{2,}$"))
77+
else if (args[i].Matches(@"^-[a-zA-Z0-9_]$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}$"))
7878
{
7979
string key = args[i].TrimStart('-');
8080

Commander.NET/Utils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ internal static string[] NormalizeParameterNames(this string[] names)
6666
{
6767
return names.Select(n =>
6868
{
69-
if (n.Matches(@"^-[a-zA-Z0-9_]$") || n.Matches(@"^--[a-zA-Z0-9_]{2,}$"))
69+
if (n.Matches(@"^-[a-zA-Z0-9_]$") || n.Matches(@"^--[a-zA-Z0-9_-]{2,}$"))
7070
return n;
71-
else if (n.Matches(@"^[a-zA-Z0-9_]$"))
71+
else if (n.Matches(@"^[a-zA-Z0-9_]$") && !n.StartsWith("-"))
7272
return "-" + n;
73-
else if (n.Matches(@"^[a-zA-Z0-9_]{2,}$"))
73+
else if (n.Matches(@"^[a-zA-Z0-9_-]{2,}$") && !n.StartsWith("-"))
7474
return "--" + n;
7575
else
7676
throw new FormatException("Invalid parameter name: " + n);

0 commit comments

Comments
 (0)