英文:
Retrieve persistent flags only once in cobra
问题
我有以下的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)
}
我使用author
作为PersistentFlag
,以便在所有子命令中都可以使用。
我正在尝试找到一种方法来避免在每个子命令中重复使用
author := viper.GetString("author")
尽管这个片段并不复杂,但在我的真实代码中,我有几个PerstitentFlags
和几个需要它们的子命令,因此代码库很快就会变得非常重复。
有没有办法解决这个问题?可以在父命令中只获取一次持久标志,并传递给子命令吗?
在这里没有找到建议。
谢谢。
英文:
I have the following cobra
setup
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.
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.
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.
答案1
得分: 1
一种处理这个问题的方法是使用“标志结构体”:
type Cmd1Flags struct {
Author string
... // cmd1 的其他持久标志
}
func GetCmd1Flags() Cmd1Flags {
ret := Cmd1Flags{}
ret.Author = viper.GetString("author")
... // 初始化其他标志
return ret
}
然后,在所有的子命令中使用 GetCmd1Flags()
。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论