眼镜蛇未能将标志标记为必需。

huangapple go评论124阅读模式
英文:

cobra failing to mark flag as required

问题

我在cobra.Cmd中有以下的标志添加:

  1. myCmd.PersistentFlags().StringP(applicationLongFlag, applicationShortFlag, applicationDefaultValue, applicationFlagHelpMsg)

其中:

  1. applicationLongFlag = "application"
  2. applicationShortFlag = "a"
  3. applicationDefaultValue = ""
  4. applicationFlagHelpMsg = "The application name"

这个代码按预期工作,但是当尝试将上述标志设置为必需时,过程失败了:

  1. if err := myCmd.MarkFlagRequired(applicationShortFlag); err != nil {
  2. return errors.Wrapf(err, "error marking %s as required flag", applicationShortFlag)
  3. }

错误信息为:

  1. error marking a as required flag: no such flag -a

-a / --application 按预期工作,并且在帮助信息中也有打印出来:

  1. go run myprog.go mycommand --help
  2. Usage:
  3. myprog.go mycommand [flags]
  4. Flags:
  5. -a, --application string The application name

为什么它无法设置为必需标志呢?

英文:

I have the following flag addition in a cobra.Cmd

  1. myCmd.PersistentFlags().StringP(applicationLongFlag, applicationShortFlag, applicationDefaultValue, applicationFlagHelpMsg)

Where

  1. applicationLongFlag = "application"
  2. applicationShortFlag = "a"
  3. applicationDefaultValue = ""
  4. applicationFlagHelpMsg = "The application name"

This works as expected, however when trying to make the above flag as required the process fails

  1. if err := myCmd.MarkFlagRequired(applicationShortFlag); err != nil {
  2. return errors.Wrapf(err, "error marking %s as required flag", applicationShortFlag)
  3. }
  1. error marking a as required flag: no such flag -a

-a / --application works as expected and it is also printed in my help

  1. go run myprog.go mycommand --help
  2. Usage:
  3. myprog.go mycommand [flags]
  4. Flags:
  5. -a, --application string The application name

Why is it failing to be set as required?

答案1

得分: 1

我认为你应该使用MarkPersistentFlagRequired而不是MarkFlagRequired,因为你想要标记一个持久性标志。
你只能使用标志的name而不是shorthand

代码

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func main() {
  7. rootCmd := &cobra.Command{
  8. Use: "root",
  9. Run: func(cmd *cobra.Command, args []string) {
  10. fmt.Println("使用 `MarkPersistentFlagRequired` 进行标记")
  11. },
  12. }
  13. rootCmd.PersistentFlags().StringP("test", "t", "", "test required")
  14. // 改为以下代码
  15. if err := rootCmd.MarkPersistentFlagRequired("test"); err != nil {
  16. fmt.Println(err.Error())
  17. return
  18. }
  19. rootCmd.Execute()
  20. }

输出

  1. go run main.go
  2. Error: required flag(s) "test" not set
  3. Usage:
  4. root [flags]
  5. Flags:
  6. -h, --help help for root
  7. -t, --test string test required
英文:

I think you should use MarkPersistentFlagRequired instead of MarkFlagRequired as you are trying to mark a persistent flag.
You can only use name of the flag and not the shorthand.

Code

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func main() {
  7. rootCmd := &cobra.Command{
  8. Use: "root",
  9. Run: func(cmd *cobra.Command, args []string) {
  10. fmt.Println("Mark using `MarkPersistentFlagRequired` ")
  11. },
  12. }
  13. rootCmd.PersistentFlags().StringP("test", "t", "", "test required")
  14. // Change this
  15. if err := rootCmd.MarkPersistentFlagRequired("test"); err != nil {
  16. fmt.Println(err.Error())
  17. return
  18. }
  19. rootCmd.Execute()
  20. }

Output

  1. go run main.go
  2. Error: required flag(s) "test" not set
  3. Usage:
  4. root [flags]
  5. Flags:
  6. -h, --help help for root
  7. -t, --test string test required

huangapple
  • 本文由 发表于 2021年12月9日 19:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/70289408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定