@@ -76,6 +76,22 @@ func TestSimplecobra(t *testing.T) {
7676
7777}
7878
79+ func TestInitAncestorsOnly (t * testing.T ) {
80+ c := qt .New (t )
81+
82+ rootCmd := testCommands ()
83+ x , err := simplecobra .New (rootCmd )
84+ c .Assert (err , qt .IsNil )
85+ args := []string {"bar" , "baz" , "--persistentFlagName" , "baz_persistent" }
86+ cd3 , err := x .Execute (context .Background (), args )
87+ c .Assert (err , qt .IsNil )
88+ c .Assert (cd3 .Command .Name (), qt .Equals , "baz" )
89+ c .Assert (rootCmd .isInit , qt .IsTrue )
90+ c .Assert (rootCmd .commands [0 ].(* lvl1Command ).isInit , qt .IsFalse )
91+ c .Assert (rootCmd .commands [1 ].(* lvl1Command ).isInit , qt .IsTrue )
92+ c .Assert (cd3 .Command .(* lvl2Command ).isInit , qt .IsTrue )
93+ }
94+
7995func TestErrors (t * testing.T ) {
8096 c := qt .New (t )
8197
@@ -142,7 +158,8 @@ func Example() {
142158}
143159
144160type rootCommand struct {
145- name string
161+ name string
162+ isInit bool
146163
147164 // Flags
148165 persistentFlagName string
@@ -164,6 +181,7 @@ func (c *rootCommand) Commands() []simplecobra.Commander {
164181}
165182
166183func (c * rootCommand ) Init (* simplecobra.Commandeer ) error {
184+ c .isInit = true
167185 c .persistentFlagNameC = c .persistentFlagName + "_rootCommand_compiled"
168186 c .localFlagNameC = c .localFlagName + "_rootCommand_compiled"
169187 return nil
@@ -189,7 +207,8 @@ func (c *rootCommand) WithCobraCommand(cmd *cobra.Command) error {
189207}
190208
191209type lvl1Command struct {
192- name string
210+ name string
211+ isInit bool
193212
194213 localFlagName string
195214 localFlagNameC string
@@ -206,6 +225,7 @@ func (c *lvl1Command) Commands() []simplecobra.Commander {
206225}
207226
208227func (c * lvl1Command ) Init (cd * simplecobra.Commandeer ) error {
228+ c .isInit = true
209229 c .localFlagNameC = c .localFlagName + "_lvl1Command_compiled"
210230 c .rootCmd = cd .Root .Command .(* rootCommand )
211231 return nil
@@ -228,6 +248,7 @@ func (c *lvl1Command) WithCobraCommand(cmd *cobra.Command) error {
228248
229249type lvl2Command struct {
230250 name string
251+ isInit bool
231252 localFlagName string
232253
233254 ctx context.Context
@@ -240,6 +261,7 @@ func (c *lvl2Command) Commands() []simplecobra.Commander {
240261}
241262
242263func (c * lvl2Command ) Init (cd * simplecobra.Commandeer ) error {
264+ c .isInit = true
243265 c .rootCmd = cd .Root .Command .(* rootCommand )
244266 c .parentCmd = cd .Parent .Command .(* lvl1Command )
245267 return nil
0 commit comments