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

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

cobra failing to mark flag as required

问题

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

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

其中:

applicationLongFlag     = "application"
applicationShortFlag    = "a"
applicationDefaultValue = ""
applicationFlagHelpMsg  = "The application name"

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

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

错误信息为:

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

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

▶ go run myprog.go mycommand --help

Usage:
  myprog.go mycommand [flags]

Flags:
  -a, --application string   The application name

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

英文:

I have the following flag addition in a cobra.Cmd

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

Where

	applicationLongFlag     = "application"
	applicationShortFlag    = "a"
	applicationDefaultValue = ""
	applicationFlagHelpMsg  = "The application name"

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


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

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

▶ go run myprog.go mycommand --help

Usage:
  myprog.go mycommand [flags]

Flags:
  -a, --application string   The application name

Why is it failing to be set as required?

答案1

得分: 1

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

代码

package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

func main() {
	rootCmd := &cobra.Command{
		Use: "root",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("使用 `MarkPersistentFlagRequired` 进行标记")
		},
	}

	rootCmd.PersistentFlags().StringP("test", "t", "", "test required")
    // 改为以下代码
	if err := rootCmd.MarkPersistentFlagRequired("test"); err != nil {
		fmt.Println(err.Error())
		return
	}
	rootCmd.Execute()
}

输出

  go run main.go 
Error: required flag(s) "test" not set
Usage:
  root [flags]

Flags:
  -h, --help          help for root
  -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

package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

func main() {
	rootCmd := &cobra.Command{
		Use: "root",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("Mark using `MarkPersistentFlagRequired` ")
		},
	}

	rootCmd.PersistentFlags().StringP("test", "t", "", "test required")
    // Change this
	if err := rootCmd.MarkPersistentFlagRequired("test"); err != nil {
		fmt.Println(err.Error())
		return
	}
	rootCmd.Execute()
}

Output

  go run main.go 
Error: required flag(s) "test" not set
Usage:
  root [flags]

Flags:
  -h, --help          help for root
  -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:

确定