Skip to content

Commit 7ff5435

Browse files
committed
fix #1294
1 parent dfdf5ce commit 7ff5435

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/System.CommandLine/Argument.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public void SetDefaultValueFactory(Func<ArgumentResult, object?> getDefaultValue
178178
/// </summary>
179179
public bool HasDefaultValue => _defaultValueFactory is not null;
180180

181+
internal virtual bool HasCustomParser => false;
182+
181183
internal static Argument None() => new()
182184
{
183185
Arity = ArgumentArity.Zero,

src/System.CommandLine/Argument{T}.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace System.CommandLine
99
/// <inheritdoc cref="Argument" />
1010
public class Argument<T> : Argument, IValueDescriptor<T>
1111
{
12+
private bool _hasCustomParser;
13+
1214
/// <summary>
1315
/// Initializes a new instance of the Argument class.
1416
/// </summary>
@@ -110,6 +112,8 @@ public Argument(
110112
}
111113
};
112114

115+
_hasCustomParser = true;
116+
113117
Description = description;
114118
}
115119

@@ -122,6 +126,8 @@ public Argument(ParseArgument<T> parse, bool isDefault = false) : this(null, par
122126
{
123127
}
124128

129+
internal override bool HasCustomParser => _hasCustomParser;
130+
125131
/// <inheritdoc />
126132
public override Type ValueType
127133
{

src/System.CommandLine/Parsing/ParseOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void ParseOptionArguments(OptionNode optionNode)
192192
var argument = optionNode.Option.Argument;
193193

194194
var contiguousTokens = 0;
195-
var continueProcessing = true;
195+
var continueProcessing = true;
196196

197197
while (More() &&
198198
CurrentToken.Type == TokenType.Argument &&

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,27 @@ private void ValidateAndConvertOptionResult(OptionResult optionResult)
372372
}
373373
}
374374

375-
for (var i = 0; i < optionResult.Children.Count; i++)
375+
if (optionResult.Children.Count == 0)
376376
{
377-
var result = optionResult.Children[i];
378-
if (result is ArgumentResult argumentResult)
377+
if (optionResult.Option.Argument is Argument { HasCustomParser: true })
379378
{
380-
ValidateAndConvertArgumentResult(argumentResult);
379+
if (optionResult.Option is Option opt)
380+
{
381+
var argResult = optionResult.GetOrCreateDefaultArgumentResult(opt.Argument);
382+
optionResult.Children.Add(argResult);
383+
ValidateAndConvertArgumentResult(argResult);
384+
}
385+
}
386+
}
387+
else
388+
{
389+
for (var i = 0; i < optionResult.Children.Count; i++)
390+
{
391+
var result = optionResult.Children[i];
392+
if (result is ArgumentResult argumentResult)
393+
{
394+
ValidateAndConvertArgumentResult(argumentResult);
395+
}
381396
}
382397
}
383398
}

0 commit comments

Comments
 (0)