英文:
Cannot insert dash in cobra parameters
问题
我查找了一些类似的问题,但除了这个链接之外,我找不到其他任何东西:https://github.com/spf13/cobra/issues/1025
我的问题是关于插入一些以破折号开头的字符串,就像下面的例子一样:
go run myapp exampleCmd set "-Dexample"
Cobra似乎将输入的-Dexample视为内部参数,因此返回以下输出:
Error: unknown shorthand flag: 'D' in -Dexample
Usage:
myapp exampleCmd set [flags]
Flags:
-h, --help help for set
Global Flags:
-s, --set string Set exampleCmd parameters. (default "default_param")
我的init()函数包含以下两行:
func init() {
...
exampleCmd.PersistentFlags().StringP("set", "s", defaultArgument, "Set parameters.")
exampleCmd.AddCommand(setCmd)
...
}
var exampleCmd = &cobra.Command{
Use: "set",
Short: "set parameter",
Long: `set parameter`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 && len(args) != 0 {
color.Red("Wrong usage, insert just a parameter")
} else if len(args) == 0 {
color.Yellow("Setting default parameter: " + defaultArgument)
internal.SetParams(defaultArgument)
} else {
internal.SetParams(args[0])
}
return nil
},
}
如果存在任何解决方案,我该如何使用cobra接受以破折号开头的参数?
英文:
I looked for some similar problems but I couldn't find anything except this: https://github.com/spf13/cobra/issues/1025
My problem is about inserting some string which contains a dash at the beginning like the following example,
go run myapp exampleCmd set "-Dexample"
Cobra seems to take the input -Dexample as internal parameter because returns this output:
Error: unknown shorthand flag: 'D' in -Dexample
Usage:
myapp exampleCmd set [flags]
Flags:
-h, --help help for set
Global Flags:
-s, --set string Set exampleCmd parameters. (default "default_param")
my init() function contains these two lines:
func init() {
...
exampleCmd.PersistentFlags().StringP("set", "s", defaultArgument, "Set parameters.")
exampleCmd.AddCommand(setCmd)
...
}
var exampleCmd = &cobra.Command{
Use: "set",
Short: "set parameter",
Long: `set parameter`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 && len(args) != 0 {
color.Red("Wrong usage, insert just a parameter")
} else if len(args) == 0 {
color.Yellow("Setting default parameter: " + defaultArgument)
internal.SetParams(defaultArgument)
} else {
internal.SetParams(args[0])
}
return nil
},
}
How can I accept parameters with dashes at beginning with cobra, if exists any solution?
答案1
得分: 6
与几乎所有的Unix风格命令行工具和标志解析库一样,Cobra使用--
将标志与参数分开,之后不再解析更多的参数作为标志,即使它们以-
开头。
这与您与其他CLI工具的交互方式没有任何区别。例如,rm -i
设置了interactive
标志,而rm -- -i
删除了一个名为-i
的文件。
您绝对不希望为某些命令或子命令任意禁用标志,这是不一致的(在您自己的应用程序内部和其他所有应用程序中都是如此),是不必要的,并且会破坏基本用户期望:有时,-h
将按照用户的期望执行操作,但对于某些命令,由于用户无法预测的原因,-h
将被视为参数并产生意外行为。
Unix已经解决了这个问题超过50年。让用户通过--
决定参数是否是标志。
英文:
As with virtual all Unix-style command line utilities and flag parsing libraries, Cobra separates flags from arguments with a --
, after which no more arguments will be parsed as flags, even if they start with a -
.
go run myapp exampleCmd set -- "-Dexample"
This is no different than how you interact with other CLI utilities. For example, rm -i
sets the interactive
flag, while rm -- -i
removes a file named -i
.
You definitely do not want to arbitrarily disable flags for certain commands or subcommands which is inconsistent (both within your own app and across all other apps), unnecessary, and breaks basic user expectations: Sometimes, -h
will do what the user expects, but for some commands, for reasons the user cannot predict, -h
will be treated as an argument and produce unexpected behavior.
Unix has solved this problem for more than 50 years. Let the user decide whether a argument is a flag via --
.
答案2
得分: -1
使用以下方法解决(如果可以的话):
我在&cobra.Command{}
的末尾添加了以下元素:
DisableFlagParsing: true,
在这里找到:https://github.com/spf13/cobra/issues/683
英文:
Solved using this workaround (if it is)
I added to the end of the &cobra.Command{} this element:
DisableFlagParsing: true,
found here: https://github.com/spf13/cobra/issues/683
答案3
得分: -1
我认为在使用类似于cobra
的工具时,不可能传递以dash
符号开头的参数。dash
是一个标志指示符,无论它是否被引号括起来,单个dash
都会被解读为一个简写标志,因此你输入的第一个字母被解释为一个未识别的标志,导致程序失败(并调用cmd.Help()
)。
你将set
同时设置为命令和标志(--set
-s
),所以它会出现在你的--help
输出中。
我建议考虑使用其他字符作为你的命令参数,或者以其他方式在内部添加它。
英文:
I don't think it is possible to pass an argument beginning with a dash
symbol while using an utility like cobra
. The dash
is a flag indicator and it doesn't matter if it is enclosed in quotes, single dash
is read as a shorthand flag, so the first letter of your input is getting interpreted as a flag and unrecognized, thus the program fails (and calls cmd.Help()
).
You've set set
as both command and a flag (--set
-s
), so it appears in your --help
output.
I would consider using a different character for your command argument or adding it in another way internally.
答案4
得分: -1
我在flagset interspersed选项中找到了解决方案:
https://github.com/spf13/cobra/issues/1307#issue-777308697
这个选项告诉cobra其他的标志在第一个标志之后也是标志。
英文:
I found the solution in the flagset interspersed option:
https://github.com/spf13/cobra/issues/1307#issue-777308697
This says to cobra that other flags after the first are also flags.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论