Skip to content

Commit d653a8e

Browse files
authored
Stop parsing option (#98)
* Add stop processing option fixes #92
1 parent 374c080 commit d653a8e

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

CommandLineParser.Tests/CommandLineParserTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ public void OrderedOptions_With_Named_Option_Between_Does_Not_work2()
113113
Assert.NotEqual(to, result.Result.To);
114114
}
115115

116+
[Fact]
117+
public void StopProcessingWorks()
118+
{
119+
var options = new CommandLineParserOptions
120+
{
121+
StopParsingAfter = "--"
122+
};
123+
124+
var parser = new CommandLineParser<OrderModelInt>(options, Services);
125+
126+
var result = parser.Parse(new string[] { "app.exe", "10", "20", "--", "some random stuff", "nothing to see here", "yadi yadi yadi", "-r", "10" });
127+
128+
result.AssertNoErrors();
129+
130+
Assert.Equal(10, result.Result.From);
131+
Assert.Equal(20, result.Result.To);
132+
Assert.NotEqual(10, result.Result.Random);
133+
}
134+
116135
[Theory]
117136
[InlineData("", "--")]
118137
[InlineData("-", "")]

CommandLineParser/CommandLineParser.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CommandLineParser/CommandLineParserOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class CommandLineParserOptions
2222
/// </summary>
2323
public string PostfixOption { get; set; } = "=";
2424

25+
/// <summary>
26+
/// Stops parsing of remaining arguments after this has been found
27+
/// </summary>
28+
public string StopParsingAfter { get; set; }
29+
2530
/// <summary>
2631
/// Help option name.
2732
/// Accepts both formatted and unformatted help name.

CommandLineParser/Core/Parsing/ArgumentManager.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,23 @@ private bool ProcessNext()
8282
return ProcessOption(option);
8383
case CommandOrOptionValueRecord commandOrValue:
8484
return ProcessCommandOrOptionValue(commandOrValue);
85+
case StopProcessingRecord _:
86+
return StopProcessing();
8587
default:
8688
return false;
8789
}
8890
}
8991

92+
private bool StopProcessing()
93+
{
94+
while (enumerator.MoveNext())
95+
{
96+
// do nothing
97+
}
98+
99+
return true;
100+
}
101+
90102
private void AddUnprocessedArgument(ArgumentRecord rec)
91103
{
92104
var arg = CurrentContext.CurrentOption != null ? (IArgument)CurrentContext.CurrentOption : (IArgument)CurrentContext.CurrentCommand;
@@ -403,6 +415,12 @@ private ArgumentRecord CreateRecord(string current)
403415
{
404416
bool isLongOption = current.StartsWith(options.PrefixLongOption);
405417
bool isShortOption = current.StartsWith(options.PrefixShortOption);
418+
bool stopProcessing = !string.IsNullOrEmpty(options.StopParsingAfter) && current.Equals(options.StopParsingAfter);
419+
420+
if (stopProcessing)
421+
{
422+
return new StopProcessingRecord(current);
423+
}
406424

407425
if (isLongOption || isShortOption)
408426
{
@@ -425,6 +443,14 @@ public void Dispose()
425443
}
426444
}
427445

446+
private sealed class StopProcessingRecord : ArgumentRecord
447+
{
448+
public StopProcessingRecord(string data)
449+
: base(data)
450+
{
451+
}
452+
}
453+
428454
private sealed class CommandOrOptionValueRecord : ArgumentRecord
429455
{
430456
public CommandOrOptionValueRecord(string data)

0 commit comments

Comments
 (0)