Hi,
maybe I am doing sth. wrong. I have a command index and a subcommand init. index has a persistent flag. Problem: I cannot access that flag from init.
My Code:
index:
var indexCmd = &cobra.Command{
Use: "index",
Short: "Commands related to elasticsearch indexes",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
func init() {
RootCmd.AddCommand(indexCmd)
indexCmd.PersistentFlags().StringP("tenant", "t", "all", "Provide tenant id or \"all\"")
}
init:
var initCmd = &cobra.Command{
Use: "init",
Short: "(Re-)initialize one or all indices",
RunE: func(cmd *cobra.Command, args []string) error {
tenant, err := cmd.PersistentFlags().GetString("tenant")
if err != nil {
return err
}
return nil
},
}
func init() {
indexCmd.AddCommand(initCmd)
}
The message that I get when I execute
... index init --tenant all
is
flag accessed but not defined: tenant
should be in cmd.Flags(), not cmd.PersistentFlags(). They get merged together at run time.
Thank you
Most helpful comment
should be in cmd.Flags(), not cmd.PersistentFlags(). They get merged together at run time.