简体   繁体   English

在眼镜蛇中仅检索一次持久标志

[英]Retrieve persistent flags only once in cobra

I have the following cobra setup我有以下cobra设置

var rootCmd = &cobra.Command{
    Use:   "basic",
    Short: "This is the basic command",
    PreRunE: func(cmd *cobra.Command, args []string) error {
        if err := viper.BindPFlags(cmd.Flags()); err != nil {
            return err
        }
        return nil
    },
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Hello")
        author := viper.GetString("author")
        fmt.Println(author)
    },
}

var subCmd1 = &cobra.Command{
    Use:   "subcommand1",
    Short: "This is test subcommand 1",
    PreRunE: func(cmd *cobra.Command, args []string) error {
        if err := viper.BindPFlags(cmd.Flags()); err != nil {
            return err
        }
        return nil
    },
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Executing subcommand 1")
        author := viper.GetString("author")
        location := viper.GetString("location")
        fmt.Println(author)
        fmt.Println(location)
    },
}

var subCmd2 = &cobra.Command{
    Use:   "subcommand2",
    Short: "This is test subcommand 2",
    PreRunE: func(cmd *cobra.Command, args []string) error {
        if err := viper.BindPFlags(cmd.Flags()); err != nil {
            return err
        }
        return nil
    },
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Executing subcommand 2")
        author := viper.GetString("author")
        duration := viper.GetInt("duration")
        fmt.Println(author)
        fmt.Printf("%d\n", duration)
    },
}

func Execute() {
    if err := rootCmd.Execute(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

func init() {
    rootCmd.PersistentFlags().StringP("author", "a", "Pantelis Karamolegkos", "author name for copyright attribution")
    subCmd1.Flags().StringP("location", "l", "Athens", "location of the command execution")
    subCmd2.Flags().IntP("duration", "d", 10, "duration of the event in minutes")
    rootCmd.AddCommand(subCmd1)
    rootCmd.AddCommand(subCmd2)
}

I use author as a PersistentFlag so that it is available in all subcommands.我使用author作为PersistentFlag以便它在所有子命令中都可用。

I am trying to find a way to skip the repetition of我试图找到一种方法来跳过重复

author := viper.GetString("author")

in each subcommand (I of course want it to be available in all subcommands)在每个子命令中(我当然希望它在所有子命令中都可用)

Although this snippet is not that convoluted, in my real code I have a few PerstitentFlags and and several subcommands requiring them, therefore the codebase can become very repetitive very quickly.虽然这个片段并不那么复杂,但在我的真实代码中,我有一些PerstitentFlags和几个需要它们的子命令,因此代码库很快就会变得非常重复。

Is there any way around this?有没有办法解决? Can the persistent flags be retrieved only once in the parent command and get passed on to the subcommands?可以在父命令中仅检索一次持久标志并传递给子命令吗? Could not find a suggestion here .在这里找不到建议。

Thanks.谢谢。

One way to deal with this is to use "flag structs":解决这个问题的一种方法是使用“标志结构”:

type Cmd1Flags struct {
  Author string
  ... // Other persistent flags for cmd1
}

func GetCmd1Flags() Cmd1Flags {
   ret:=Cmd1Flags{}
   ret.Author=viper.GetString("author")
   ... // Initialize other flags
   return ret
}

Then, use GetCmd1Flags() in all the subcommands.然后,在所有子命令中使用GetCmd1Flags()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM