Cobra: flag accessed but not defined

Created on 13 Feb 2018  路  2Comments  路  Source: spf13/cobra

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

Most helpful comment

should be in cmd.Flags(), not cmd.PersistentFlags(). They get merged together at run time.

All 2 comments

should be in cmd.Flags(), not cmd.PersistentFlags(). They get merged together at run time.

Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

diwakergupta picture diwakergupta  路  5Comments

eine picture eine  路  5Comments

supershal picture supershal  路  4Comments

garthk picture garthk  路  3Comments

umbynos picture umbynos  路  5Comments