Skip to content

Commit e744336

Browse files
committed
account for scalar arguments
1 parent 001dd30 commit e744336

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/System.CommandLine.Tests/ParserTests.CaptureRemainingTokens.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,47 @@ public void Leading_known_option_is_parsed_normally_when_capture_is_first_argume
256256
result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("foo", "--unknown");
257257
result.Errors.Should().BeEmpty();
258258
}
259+
260+
[Fact]
261+
public void Scalar_capture_escapes_one_token_then_resumes_normal_parsing()
262+
{
263+
var verbose = new Option<bool>("--verbose");
264+
var first = new Argument<string>("first");
265+
var target = new Argument<string>("target") { CaptureRemainingTokens = true };
266+
var command = new RootCommand
267+
{
268+
verbose,
269+
first,
270+
target
271+
};
272+
273+
var result = command.Parse("foo --verbose --verbose");
274+
275+
using var _ = new AssertionScope();
276+
result.GetValue(first).Should().Be("foo");
277+
result.GetValue(target).Should().Be("--verbose");
278+
result.GetValue(verbose).Should().BeTrue();
279+
result.Errors.Should().BeEmpty();
280+
}
281+
282+
[Fact]
283+
public void Options_after_scalar_capture_overflow_are_parsed_normally()
284+
{
285+
var verbose = new Option<bool>("--verbose");
286+
var target = new Argument<string>("target") { CaptureRemainingTokens = true };
287+
var command = new RootCommand
288+
{
289+
verbose,
290+
target
291+
};
292+
293+
var result = command.Parse("hello --verbose");
294+
295+
using var _ = new AssertionScope();
296+
result.GetValue(target).Should().Be("hello");
297+
result.GetValue(verbose).Should().BeTrue();
298+
result.Errors.Should().BeEmpty();
299+
}
259300
}
260301
}
261302
}

src/System.CommandLine/Parsing/ParseOperation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ private void ParseCommandArguments(ref int currentArgumentCount, ref int current
194194

195195
if (currentArgumentCount == 0) // no matching arguments found
196196
{
197+
if (captureRemaining)
198+
{
199+
// Return to ParseCommandChildren so that overflow tokens
200+
// are dispatched normally (e.g. as options or subcommands).
201+
break;
202+
}
203+
197204
AddCurrentTokenToUnmatched();
198205
Advance();
199206
}

0 commit comments

Comments
 (0)