Skip to content

Commit 3aebaaf

Browse files
committed
Corrected issue when parsing a string containing only the strict operand dlimiter. Added test.
1 parent 746e434 commit 3aebaaf

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Utility.CommandLine.Arguments.Tests/Arguments.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ public void ParseShorts()
340340
Assert.Equal("hello world", test["c"]);
341341
}
342342

343+
/// <summary>
344+
/// Tests the <see cref="Utility.CommandLine.Arguments.Parse(string)"/> method with a a string containing only the
345+
/// strict operand delimiter.
346+
/// </summary>
347+
[Fact]
348+
public void ParseStrictOperandDelimiterOnly()
349+
{
350+
CommandLine.Arguments test = CommandLine.Arguments.Parse("--");
351+
352+
Assert.Equal(0, test.OperandList.Count);
353+
}
354+
355+
/// <summary>
356+
/// Tests the <see cref="Utility.CommandLine.Arguments.Parse(string)"/> method with a a string containing multiple
357+
/// strict operand delimiters.
358+
/// </summary>
359+
[Fact]
360+
public void ParseStrictOperandMultipleDelimiter()
361+
{
362+
CommandLine.Arguments test = CommandLine.Arguments.Parse("one -- two -- three");
363+
364+
Assert.Equal(4, test.OperandList.Count);
365+
Assert.Equal("one", test.OperandList[0]);
366+
Assert.Equal("two", test.OperandList[1]);
367+
Assert.Equal("--", test.OperandList[2]);
368+
Assert.Equal("three", test.OperandList[3]);
369+
}
370+
343371
/// <summary>
344372
/// Tests the <see cref="Utility.CommandLine.Arguments.Parse(string)"/> method with an explicit operand delimiter.
345373
/// </summary>
@@ -365,7 +393,7 @@ public void ParseStrictOperands()
365393
[Fact]
366394
public void ParseStrictOperandsEmpty()
367395
{
368-
CommandLine.Arguments test = CommandLine.Arguments.Parse("--test one two -- ");
396+
CommandLine.Arguments test = CommandLine.Arguments.Parse("--test one two --");
369397

370398
Assert.Equal(1, test.OperandList.Count);
371399
Assert.Equal("two", test.OperandList[0]);

Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class Arguments
150150
/// This regular expression effectively splits a string into two parts; the part before the first "--", and the part
151151
/// after. Instances of "--" not surrounded by a word boundary and those enclosed in quotes are ignored.
152152
/// </remarks>
153-
private const string StrictOperandSplitRegEx = "(.*[^\\\"\\\'])?\\B-{2}\\B[^\\\"\\\'](.*)";
153+
private const string StrictOperandSplitRegEx = "(.*?[^\\\"\\\'])?(\\B-{2}\\B)[^\\\"\\\']?(.*)";
154154

155155
#endregion Private Fields
156156

@@ -228,7 +228,7 @@ public string this[string index]
228228
MatchCollection matches = Regex.Matches(commandLineString, StrictOperandSplitRegEx);
229229

230230
// if there is a match, the string contains the strict operand delimiter. parse the first and second matches accordingly.
231-
if (matches.Count > 0 && matches[0].Groups.Count >= 1)
231+
if (matches.Count > 0)
232232
{
233233
// the first group of the first match will contain everything in the string prior to the strict operand delimiter,
234234
// so extract the argument key/value pairs and list of operands from that string.
@@ -237,9 +237,9 @@ public string this[string index]
237237

238238
// the first group of the second match will contain everything in the string after the strict operand delimiter, so
239239
// extract the operands from that string using the strict method.
240-
if (matches[0].Groups[2].Value != string.Empty)
240+
if (matches[0].Groups[3].Value != string.Empty)
241241
{
242-
List<string> operandListStrict = GetOperandListStrict(matches[0].Groups[2].Value);
242+
List<string> operandListStrict = GetOperandListStrict(matches[0].Groups[3].Value);
243243

244244
// join the operand lists.
245245
operandList.AddRange(operandListStrict);

0 commit comments

Comments
 (0)