@@ -16,37 +16,46 @@ public void AddCommands(IEnumerable<CliCommandInfo> commands)
1616 commands . ForEach ( AddCommand ) ;
1717 }
1818
19- public CliCommandInfo ? FindCommandForArgs ( string [ ] args )
19+ public FindCommandNodeResult < CliCommandGroupNode > ? FindCommandGroupNode ( string [ ] args )
2020 {
21- return FindCommandForArgs ( _treeRootNodes , args ) ;
21+ return FindCommandGroupNode ( null , _treeRootNodes , args ) ;
2222 }
2323
24- public CliCommandGroupNode ? FindGroupNodeForArgs ( string [ ] args )
25- {
26- return FindGroupNodeForArgs ( null , _treeRootNodes , args ) ;
27- }
28-
29- public CliCommandGroupNode ? FindGroupNodeForArgs ( CliCommandGroupNode ? lastGroupNode ,
24+ private FindCommandNodeResult < CliCommandGroupNode > ? FindCommandGroupNode (
25+ CliCommandGroupNode ? lastGroupNode ,
3026 IEnumerable < CliTreeNode > nodes , string [ ] args )
3127 {
3228 if ( args . Length == 0 )
3329 {
34- return null ;
30+ return lastGroupNode == null
31+ ? null
32+ : new FindCommandNodeResult < CliCommandGroupNode > ( lastGroupNode , [ ] ) ;
3533 }
3634
3735 var node = nodes . FirstOrDefault ( x => x . Name == args [ 0 ] ) ;
3836
37+ var remainingArgs = args . Skip ( 1 ) . ToArray ( ) ;
38+
3939 return node switch
4040 {
41- null => lastGroupNode ,
42- CliCommandNode commandNode => commandNode . Parent ,
43- CliCommandGroupNode groupNode => FindGroupNodeForArgs ( groupNode , groupNode . ChildNodes ,
44- args . Skip ( 1 ) . ToArray ( ) ) ,
41+ null => lastGroupNode == null
42+ ? null
43+ : new FindCommandNodeResult < CliCommandGroupNode > ( lastGroupNode , args ) ,
44+ CliCommandNode commandNode => new FindCommandNodeResult < CliCommandGroupNode > ( commandNode . Parent ,
45+ args ) ,
46+ CliCommandGroupNode groupNode => FindCommandGroupNode ( groupNode , groupNode . ChildNodes ,
47+ remainingArgs ) ,
4548 _ => null
4649 } ;
4750 }
4851
49- private CliCommandInfo ? FindCommandForArgs ( IEnumerable < CliTreeNode > nodes , string [ ] args )
52+ public FindCommandNodeResult < CliCommandNode > ? FindCommandNode ( string [ ] args )
53+ {
54+ return FindCommandNode ( _treeRootNodes , args ) ;
55+ }
56+
57+ private static FindCommandNodeResult < CliCommandNode > ? FindCommandNode ( IEnumerable < CliTreeNode > nodes ,
58+ string [ ] args )
5059 {
5160 if ( args . Length == 0 )
5261 {
@@ -58,8 +67,10 @@ public void AddCommands(IEnumerable<CliCommandInfo> commands)
5867 return node switch
5968 {
6069 null => null ,
61- CliCommandNode commandNode => commandNode . CommandInfo ,
62- CliCommandGroupNode groupNode => FindCommandForArgs ( groupNode . ChildNodes , args . Skip ( 1 ) . ToArray ( ) ) ,
70+ CliCommandNode commandNode => new FindCommandNodeResult < CliCommandNode > ( commandNode ,
71+ args . Skip ( 1 ) . ToArray ( ) ) ,
72+ CliCommandGroupNode groupNode => FindCommandNode ( groupNode . ChildNodes ,
73+ args . Skip ( 1 ) . ToArray ( ) ) ,
6374 _ => null
6475 } ;
6576 }
@@ -79,7 +90,7 @@ private void AddCommand(CliCommandInfo command, string[] commands)
7990 case 0 :
8091 throw new InvalidOperationException ( "Commands must not be empty" ) ;
8192 case 1 :
82- _treeRootNodes . Add ( new CliCommandNode ( command , commands [ 0 ] ) ) ;
93+ _treeRootNodes . Add ( new CliCommandNode ( command , commands [ 0 ] , null ) ) ;
8394 break ;
8495 default :
8596 {
@@ -92,7 +103,7 @@ private void AddCommand(CliCommandInfo command, string[] commands)
92103
93104 _treeRootNodes . Add ( groupNode ) ;
94105
95- groupNode . ChildNodes . Add ( new CliCommandNode ( command , commands [ ^ 1 ] ) ) ;
106+ groupNode . ChildNodes . Add ( new CliCommandNode ( command , commands [ ^ 1 ] , groupNode ) ) ;
96107
97108 break ;
98109 }
@@ -101,10 +112,11 @@ private void AddCommand(CliCommandInfo command, string[] commands)
101112
102113 private CliCommandGroupNode ? GetGroupNode ( string [ ] commands )
103114 {
104- return GetGroupNode ( _treeRootNodes , commands ) ;
115+ return GetGroupNode ( null , _treeRootNodes , commands ) ;
105116 }
106117
107- private static CliCommandGroupNode ? GetGroupNode ( List < CliTreeNode > nodes , string [ ] cmds )
118+ private static CliCommandGroupNode ? GetGroupNode ( CliCommandGroupNode ? parent , List < CliTreeNode > nodes ,
119+ string [ ] cmds )
108120 {
109121 var childNode = nodes
110122 . OfType < CliCommandGroupNode > ( )
@@ -118,16 +130,16 @@ private void AddCommand(CliCommandInfo command, string[] commands)
118130
119131 if ( childNode != null )
120132 {
121- return GetGroupNode ( childNode . ChildNodes , cmds . Skip ( 1 ) . ToArray ( ) ) ;
133+ return GetGroupNode ( childNode , childNode . ChildNodes , cmds . Skip ( 1 ) . ToArray ( ) ) ;
122134 }
123135
124- var groupNode = new CliCommandGroupNode ( cmds [ 0 ] ) ;
136+ var groupNode = new CliCommandGroupNode ( cmds [ 0 ] , parent ) ;
125137
126138 nodes . Add ( groupNode ) ;
127139
128140 return cmds . Length == 1
129141 ? groupNode
130- : GetGroupNode ( groupNode . ChildNodes , cmds . Skip ( 1 ) . ToArray ( ) ) ;
142+ : GetGroupNode ( groupNode , groupNode . ChildNodes , cmds . Skip ( 1 ) . ToArray ( ) ) ;
131143 }
132144
133145 public IEnumerable < CliTreeNode > TreeRootNodes => _treeRootNodes ;
0 commit comments