File tree Expand file tree Collapse file tree 4 files changed +56
-6
lines changed
System.CommandLine/Parsing Expand file tree Collapse file tree 4 files changed +56
-6
lines changed Original file line number Diff line number Diff line change @@ -297,6 +297,41 @@ public void Option_ArgumentResult_parentage_to_root_symbol_is_set_correctly_when
297
297
. Should ( )
298
298
. Be ( command ) ;
299
299
}
300
+
301
+ [ Theory ]
302
+ [ InlineData ( "-x value-x -y value-y" ) ]
303
+ [ InlineData ( "-y value-y -x value-x" ) ]
304
+ public void Symbol_can_be_found_without_explicitly_traversing_result_tree ( string commandLine )
305
+ {
306
+ SymbolResult resultForOptionX = null ;
307
+ var optionX = new Option < string > (
308
+ "-x" ,
309
+ parseArgument : _ => string . Empty ) ;
310
+
311
+ var optionY = new Option < string > (
312
+ "-y" ,
313
+ parseArgument : argResult =>
314
+ {
315
+ resultForOptionX = argResult . FindResultFor ( optionX ) ;
316
+ return string . Empty ;
317
+ } ) ;
318
+
319
+ var command = new Command ( "the-command" )
320
+ {
321
+ optionX ,
322
+ optionY ,
323
+ } ;
324
+
325
+ command . Parse ( commandLine ) ;
326
+
327
+ resultForOptionX
328
+ . Should ( )
329
+ . BeOfType < OptionResult > ( )
330
+ . Which
331
+ . Option
332
+ . Should ( )
333
+ . BeSameAs ( optionX ) ;
334
+ }
300
335
301
336
[ Fact ]
302
337
public void Command_ArgumentResult_Parent_is_set_correctly_when_token_is_implicit ( )
Original file line number Diff line number Diff line change @@ -325,7 +325,7 @@ private void PopulateDefaultValues()
325
325
{
326
326
foreach ( var symbol in commandResult . Command . Children )
327
327
{
328
- var symbolResult = _rootCommandResult ! . FindResultFor ( symbol ) ;
328
+ var symbolResult = _rootCommandResult ! . FindResultForSymbol ( symbol ) ;
329
329
330
330
if ( symbolResult is null )
331
331
{
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ private void EnsureResultMapsAreInitialized()
47
47
}
48
48
}
49
49
50
- public ArgumentResult ? FindResultFor ( IArgument argument )
50
+ public override ArgumentResult ? FindResultFor ( IArgument argument )
51
51
{
52
52
EnsureResultMapsAreInitialized ( ) ;
53
53
@@ -56,7 +56,7 @@ private void EnsureResultMapsAreInitialized()
56
56
return result ;
57
57
}
58
58
59
- public CommandResult ? FindResultFor ( ICommand command )
59
+ public override CommandResult ? FindResultFor ( ICommand command )
60
60
{
61
61
EnsureResultMapsAreInitialized ( ) ;
62
62
@@ -65,7 +65,7 @@ private void EnsureResultMapsAreInitialized()
65
65
return result ;
66
66
}
67
67
68
- public OptionResult ? FindResultFor ( IOption option )
68
+ public override OptionResult ? FindResultFor ( IOption option )
69
69
{
70
70
EnsureResultMapsAreInitialized ( ) ;
71
71
@@ -74,7 +74,7 @@ private void EnsureResultMapsAreInitialized()
74
74
return result ;
75
75
}
76
76
77
- public SymbolResult ? FindResultFor ( ISymbol symbol )
77
+ public SymbolResult ? FindResultForSymbol ( ISymbol symbol )
78
78
{
79
79
switch ( symbol )
80
80
{
Original file line number Diff line number Diff line change @@ -27,7 +27,13 @@ private protected SymbolResult(
27
27
28
28
public SymbolResult ? Parent { get ; }
29
29
30
- internal virtual RootCommandResult ? Root => ( Parent as CommandResult ) ? . Root ;
30
+ internal virtual RootCommandResult ? Root =>
31
+ Parent switch
32
+ {
33
+ CommandResult c => c . Root ,
34
+ OptionResult o => o . Parent ? . Root ,
35
+ _ => null
36
+ } ;
31
37
32
38
public ISymbol Symbol { get ; }
33
39
@@ -65,6 +71,15 @@ protected internal ValidationMessages ValidationMessages
65
71
66
72
internal void AddToken ( Token token ) => _tokens . Add ( token ) ;
67
73
74
+ public virtual ArgumentResult ? FindResultFor ( IArgument argument ) =>
75
+ Root ? . FindResultFor ( argument ) ;
76
+
77
+ public virtual CommandResult ? FindResultFor ( ICommand command ) =>
78
+ Root ? . FindResultFor ( command ) ;
79
+
80
+ public virtual OptionResult ? FindResultFor ( IOption option ) =>
81
+ Root ? . FindResultFor ( option ) ;
82
+
68
83
internal ArgumentResult GetOrCreateDefaultArgumentResult ( Argument argument ) =>
69
84
_defaultArgumentValues . GetOrAdd (
70
85
argument ,
You can’t perform that action at this time.
0 commit comments