Skip to content

Commit 43f8d11

Browse files
committed
fix #792 (again)
1 parent 6eed95e commit 43f8d11

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ public void Multiple_arguments_of_unspecified_type_are_parsed_correctly()
144144
.Be("dest.txt");
145145
}
146146

147+
148+
[Fact]
149+
public void When_multiple_arguments_are_defined_but_not_provided_then_option_parses_correctly()
150+
{
151+
var option = new Option<string>("-e");
152+
var command = new Command("the-command") { option };
153+
154+
command.AddArgument(new Argument<string>
155+
{
156+
Name = "arg1",
157+
});
158+
159+
command.AddArgument(new Argument<string>
160+
{
161+
Name = "arg2",
162+
});
163+
164+
var result = command.Parse("-e foo");
165+
166+
var optionResult = result.ValueForOption(option);
167+
168+
optionResult.Should().Be("foo");
169+
}
170+
171+
147172
[Fact]
148173
public void tokens_that_cannot_be_converted_by_multiple_arity_argument_flow_to_next_multiple_arity_argument()
149174
{

src/System.CommandLine/Parsing/OptionResult.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ internal ArgumentConversionResult ArgumentConversionResult
4848
{
4949
if (_argumentConversionResult is null)
5050
{
51-
var results = Children
52-
.OfType<ArgumentResult>()
53-
.Select(r => r.GetArgumentConversionResult());
51+
for (var i = 0; i < Children.Count; i++)
52+
{
53+
var child = Children[i];
5454

55-
_argumentConversionResult = results.SingleOrDefault() ??
56-
ArgumentConversionResult.None(Option.Argument);
55+
if (child is ArgumentResult argumentResult)
56+
{
57+
return _argumentConversionResult = argumentResult.GetArgumentConversionResult();
58+
}
59+
}
60+
61+
return _argumentConversionResult = ArgumentConversionResult.None(Option.Argument);
5762
}
5863

5964
return _argumentConversionResult;

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ protected override void Stop(SyntaxNode node)
199199

200200
argumentResults.Add(nextArgumentResult);
201201

202-
previousArgumentResult.Parent!.Children.Add(nextArgumentResult);
202+
if (previousArgumentResult.Parent is CommandResult commandResult)
203+
{
204+
commandResult.Children.Add(nextArgumentResult);
205+
}
203206

204207
_rootCommandResult.AddToSymbolMap(nextArgumentResult);
205208
}

0 commit comments

Comments
 (0)