Rob*_* M. 5 command-line go go-cobra
Usage如果在 Go 中的 cobra 命令上调用帮助函数,我希望能够设置该行以指定需要传递的参数。
这是常规帮助标志输出的内容:
Cancel the order specified by the order id by submitting a cancel order.
Optionally, an account ID may be supplied as well for extra measure.
Usage:
gbutil orders cancel [flags]
Flags:
-a, --account_id string the account id that the order belongs to
-h, --help help for cancel
Global Flags:
--config string config file (default is $HOME/.gbutil.yaml)
Run Code Online (Sandbox Code Playgroud)
我想要:
Cancel the order specified by the order id by submitting a cancel order.
Optionally, an account ID may be supplied as well for extra measure.
Usage:
gbutil orders cancel <order_id> [flags]
Flags:
-a, --account_id string the account id that the order belongs to
-h, --help help for cancel
Global Flags:
--config string config file (default is $HOME/.gbutil.yaml)
Run Code Online (Sandbox Code Playgroud)
我曾尝试SetUsageTemplate在init()函数中使用,但随后它删除了部分标志:
orderscancelCmd.SetUsageTemplate(strings.Replace(orderscancelCmd.UsageString(), "gbutil orders cancel [flags]", "gbutil orders cancel <order_id> [flags]", 1))
Run Code Online (Sandbox Code Playgroud)
这导致:
Cancel the order specified by the order id by submitting a cancel order.
Optionally, an account ID may be supplied as well for extra measure.
Usage:
gbutil orders cancel <order_id> [flags]
Flags:
-a, --account_id string the account id that the order belongs to
Run Code Online (Sandbox Code Playgroud)
在那里我丢失了-h标志和有关Global Flags.
如果他们不提供 arg,我可以通过以下方式使其工作:
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
fmt.Println(strings.Replace(cmd.UsageString(), "gbutil orders cancel [flags]", "gbutil orders cancel <order_id> [flags]", 1))
return
}
Run Code Online (Sandbox Code Playgroud)
但是该-h标志仍然输出错误的用法行。
有没有办法做到这一点?提前致谢!
更改用法名称的外观。您可以在cobra.Command.Use参数中传递它。所以对你来说,它可能看起来像这样:
var cmdCancel = &cobra.Command{
Use: "cancel <order_id>",
Args: cobra.ExactArgs(1), // make sure that only one arg can be passed
// Your logic here
}
Run Code Online (Sandbox Code Playgroud)