Skip to content

Commit f5a35f7

Browse files
committed
refactor and add tests for null / empty tokens when default value is used
1 parent 2ac4bce commit f5a35f7

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

src/System.CommandLine.Tests/ParseDiagramTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public void Parse_diagram_identifies_options_where_default_values_have_been_appl
9090
}
9191
};
9292

93-
9493
var result = rootCommand.Parse("-w 9000");
9594

9695
var diagram = result.Diagram();

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,45 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_r
13711371
.BeTrue();
13721372
}
13731373

1374+
[Fact]
1375+
public void When_an_option_with_a_default_value_is_not_matched_then_there_are_no_tokens()
1376+
{
1377+
var option = new Option<string>(
1378+
"-o",
1379+
() => "the-default");
1380+
1381+
var command = new Command("command")
1382+
{
1383+
option
1384+
};
1385+
1386+
var result = command.Parse("command");
1387+
1388+
result.FindResultFor(option)
1389+
.Token
1390+
.Should()
1391+
.BeNull();
1392+
}
1393+
1394+
[Fact]
1395+
public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_no_tokens()
1396+
{
1397+
var argument = new Argument<string>(
1398+
"o",
1399+
() => "the-default");
1400+
1401+
var command = new Command("command")
1402+
{
1403+
argument
1404+
};
1405+
var result = command.Parse("command");
1406+
1407+
result.FindResultFor(argument)
1408+
.Tokens
1409+
.Should()
1410+
.BeEmpty();
1411+
}
1412+
13741413
[Fact]
13751414
public void Command_default_argument_value_does_not_override_parsed_value()
13761415
{

src/System.CommandLine/Parsing/OptionResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ public class OptionResult : SymbolResult
1212

1313
internal OptionResult(
1414
IOption option,
15-
Token token,
16-
CommandResult parent) :
15+
Token token = null,
16+
CommandResult parent = null) :
1717
base(option ?? throw new ArgumentNullException(nameof(option)),
1818
parent)
1919
{
2020
Option = option;
21-
Token = token ?? throw new ArgumentNullException(nameof(token));
21+
Token = token;
2222
}
2323

2424
public IOption Option { get; }
2525

26-
public bool IsImplicit => Token is ImplicitToken;
26+
public bool IsImplicit => Token is ImplicitToken || Token is null;
2727

2828
public Token Token { get; }
2929

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private void PopulateDefaultValues()
318318

319319
var optionResult = new OptionResult(
320320
option,
321-
option.CreateImplicitToken(),
321+
null,
322322
commandResult);
323323

324324
var childArgumentResult = optionResult.GetOrCreateDefaultArgumentResult(

src/System.CommandLine/Parsing/SymbolResultExtensions.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56

67
namespace System.CommandLine.Parsing
78
{
@@ -24,12 +25,24 @@ internal static IEnumerable<SymbolResult> AllSymbolResults(this SymbolResult sym
2425
}
2526
}
2627

27-
internal static Token Token(this SymbolResult symbolResult) =>
28-
symbolResult switch
28+
internal static Token Token(this SymbolResult symbolResult)
29+
{
30+
return symbolResult switch
2931
{
3032
CommandResult commandResult => commandResult.Token,
31-
OptionResult optionResult => optionResult.Token,
33+
OptionResult optionResult => optionResult.Token ??
34+
CreateImplicitToken(optionResult.Option),
3235
_ => throw new ArgumentOutOfRangeException(nameof(symbolResult))
3336
};
37+
38+
Token CreateImplicitToken(IOption option)
39+
{
40+
var optionName = option.Name;
41+
42+
var defaultAlias = option.RawAliases.First(alias => alias.RemovePrefix() == optionName);
43+
44+
return new ImplicitToken(defaultAlias, TokenType.Option);
45+
}
46+
}
3447
}
35-
}
48+
}

src/System.CommandLine/SymbolExtensions.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5-
using System.CommandLine.Parsing;
65
using System.Linq;
76

87
namespace System.CommandLine
@@ -11,8 +10,8 @@ internal static class SymbolExtensions
1110
{
1211
internal static IEnumerable<string> ChildSymbolAliases(this ISymbol symbol) =>
1312
symbol.Children
14-
.Where(s => !s.IsHidden)
15-
.SelectMany(s => s.RawAliases);
13+
.Where(s => !s.IsHidden)
14+
.SelectMany(s => s.RawAliases);
1615

1716
internal static IEnumerable<IArgument> Arguments(this ISymbol symbol)
1817
{
@@ -34,14 +33,5 @@ internal static IEnumerable<IArgument> Arguments(this ISymbol symbol)
3433
throw new NotSupportedException();
3534
}
3635
}
37-
38-
internal static Token CreateImplicitToken(this IOption option)
39-
{
40-
var optionName = option.Name;
41-
42-
var defaultAlias = option.RawAliases.First(alias => alias.RemovePrefix() == optionName);
43-
44-
return new ImplicitToken(defaultAlias, TokenType.Option);
45-
}
4636
}
47-
}
37+
}

0 commit comments

Comments
 (0)