英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论