@@ -47,6 +47,8 @@ func TestSimpleCobra(t *testing.T) {
4747 c .Assert (tc .persistentFlagName , qt .Equals , "root_persistent" )
4848 c .Assert (tc .persistentFlagNameC , qt .Equals , "root_persistent_rootCommand_compiled" )
4949 c .Assert (tc .localFlagNameC , qt .Equals , "root_local_rootCommand_compiled" )
50+ c .Assert (tc .initRunner , qt .Equals , cd )
51+ c .Assert (tc .initThis , qt .Equals , cd )
5052
5153 // Execute a level 1 command.
5254 // This may not be very realistic, but it works. The common use case for a CLI app is to run one command and then exit.
@@ -62,6 +64,8 @@ func TestSimpleCobra(t *testing.T) {
6264 c .Assert (tc2 .localFlagNameC , qt .Equals , "bar_local_lvl1Command_compiled" )
6365 c .Assert (tc .persistentFlagName , qt .Equals , "bar_persistent" )
6466 c .Assert (tc .persistentFlagNameC , qt .Equals , "bar_persistent_rootCommand_compiled" )
67+ c .Assert (tc2 .rootCmd .initRunner , qt .Equals , cd2 )
68+ c .Assert (tc2 .rootCmd .initThis , qt .Equals , cd2 .Root )
6569
6670 // Execute a level 2 command.
6771 args = []string {"bar" , "baz" , "--persistentFlagName" , "baz_persistent" }
@@ -73,6 +77,8 @@ func TestSimpleCobra(t *testing.T) {
7377 c .Assert (tc3 .rootCmd , qt .Equals , rootCmd )
7478 c .Assert (tc3 .parentCmd , qt .Equals , tc2 )
7579 c .Assert (tc3 .ctx , qt .Equals , ctx )
80+ c .Assert (tc3 .rootCmd .initRunner , qt .Equals , cd3 )
81+ c .Assert (tc3 .rootCmd .initThis , qt .Equals , cd3 .Root )
7682
7783}
7884
@@ -170,7 +176,9 @@ type rootCommand struct {
170176 localFlagNameC string
171177
172178 // For testing.
173- ctx context.Context
179+ ctx context.Context
180+ initThis * simplecobra.Commandeer
181+ initRunner * simplecobra.Commandeer
174182
175183 // Sub commands.
176184 commands []simplecobra.Commander
@@ -180,10 +188,12 @@ func (c *rootCommand) Commands() []simplecobra.Commander {
180188 return c .commands
181189}
182190
183- func (c * rootCommand ) Init (* simplecobra.Commandeer ) error {
191+ func (c * rootCommand ) Init (this , runner * simplecobra.Commandeer ) error {
184192 c .isInit = true
185193 c .persistentFlagNameC = c .persistentFlagName + "_rootCommand_compiled"
186194 c .localFlagNameC = c .localFlagName + "_rootCommand_compiled"
195+ c .initThis = this
196+ c .initRunner = runner
187197 return nil
188198}
189199
@@ -224,10 +234,10 @@ func (c *lvl1Command) Commands() []simplecobra.Commander {
224234 return c .commands
225235}
226236
227- func (c * lvl1Command ) Init (cd * simplecobra.Commandeer ) error {
237+ func (c * lvl1Command ) Init (this , runner * simplecobra.Commandeer ) error {
228238 c .isInit = true
229239 c .localFlagNameC = c .localFlagName + "_lvl1Command_compiled"
230- c .rootCmd = cd .Root .Command .(* rootCommand )
240+ c .rootCmd = this .Root .Command .(* rootCommand )
231241 return nil
232242}
233243
@@ -260,10 +270,10 @@ func (c *lvl2Command) Commands() []simplecobra.Commander {
260270 return nil
261271}
262272
263- func (c * lvl2Command ) Init (cd * simplecobra.Commandeer ) error {
273+ func (c * lvl2Command ) Init (this , runner * simplecobra.Commandeer ) error {
264274 c .isInit = true
265- c .rootCmd = cd .Root .Command .(* rootCommand )
266- c .parentCmd = cd .Parent .Command .(* lvl1Command )
275+ c .rootCmd = this .Root .Command .(* rootCommand )
276+ c .parentCmd = this .Parent .Command .(* lvl1Command )
267277 return nil
268278}
269279
0 commit comments