spf13/cobra

PersistantFlags are lost when calling up PersistentPostRun chain

eduser25 opened this issue · 1 comments

I was planning to divide some work across the chain, so that children do some specific implementations while parents gather the results in some generic interface, until the root takes it all in the most generic way possible.

for that I was using

cobra.EnableTraverseRunHooks = true

imagine 3 nested commands:

var root = &cobra.Command{
  PersistentPostRun: func(cmd *cobra.Command, args []string) {
     ...
  }
}

var intermediateCmd = &cobra.Command{
  PersistentPostRun: func(cmd *cobra.Command, args []string) {
     ...
  }
}

var childCmd = &cobra.Command{
  Run: func(cmd *cobra.Command, args []string) {
     ...
  }
}

root.addCmd(intermediateCmd)
intermediateCmd.addCmd(childCmd)

This achieves the effect I want, where I get a child-to-root order of execution of the commands, but I've found right after childCmd is done with Run and intermediateCmd is executing PersistentPostRun, the persistantFlags are empty and I'm not sure why. I have defined some persistant flags in intermediateCmd, and marked them as required so execution fails if I don't pass them, but during PersistentPostRun of intermediateCmd they seem to not be there:

err: flag accessed but not defined: <flag name> 

Is this expected?

Ok, I was using PersistentFlags() to read the flags later, fixed and working as expected.