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
297297 . Should ( )
298298 . Be ( command ) ;
299299 }
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+ }
300335
301336 [ Fact ]
302337 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()
325325 {
326326 foreach ( var symbol in commandResult . Command . Children )
327327 {
328- var symbolResult = _rootCommandResult ! . FindResultFor ( symbol ) ;
328+ var symbolResult = _rootCommandResult ! . FindResultForSymbol ( symbol ) ;
329329
330330 if ( symbolResult is null )
331331 {
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ private void EnsureResultMapsAreInitialized()
4747 }
4848 }
4949
50- public ArgumentResult ? FindResultFor ( IArgument argument )
50+ public override ArgumentResult ? FindResultFor ( IArgument argument )
5151 {
5252 EnsureResultMapsAreInitialized ( ) ;
5353
@@ -56,7 +56,7 @@ private void EnsureResultMapsAreInitialized()
5656 return result ;
5757 }
5858
59- public CommandResult ? FindResultFor ( ICommand command )
59+ public override CommandResult ? FindResultFor ( ICommand command )
6060 {
6161 EnsureResultMapsAreInitialized ( ) ;
6262
@@ -65,7 +65,7 @@ private void EnsureResultMapsAreInitialized()
6565 return result ;
6666 }
6767
68- public OptionResult ? FindResultFor ( IOption option )
68+ public override OptionResult ? FindResultFor ( IOption option )
6969 {
7070 EnsureResultMapsAreInitialized ( ) ;
7171
@@ -74,7 +74,7 @@ private void EnsureResultMapsAreInitialized()
7474 return result ;
7575 }
7676
77- public SymbolResult ? FindResultFor ( ISymbol symbol )
77+ public SymbolResult ? FindResultForSymbol ( ISymbol symbol )
7878 {
7979 switch ( symbol )
8080 {
Original file line number Diff line number Diff line change @@ -27,7 +27,13 @@ private protected SymbolResult(
2727
2828 public SymbolResult ? Parent { get ; }
2929
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+ } ;
3137
3238 public ISymbol Symbol { get ; }
3339
@@ -65,6 +71,15 @@ protected internal ValidationMessages ValidationMessages
6571
6672 internal void AddToken ( Token token ) => _tokens . Add ( token ) ;
6773
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+
6883 internal ArgumentResult GetOrCreateDefaultArgumentResult ( Argument argument ) =>
6984 _defaultArgumentValues . GetOrAdd (
7085 argument ,
You can’t perform that action at this time.
0 commit comments