我希望能够设置该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)
我想:
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)
我曾尝试SetUsageTemplate
在init()
函数中使用,但随后它删除了部分标志:
orderscancelCmd.SetUsageTemplate(strings.Replace(orderscancelCmd.UsageString(), "gbutil orders cancel [flags]", "gbutil orders cancel <order_id> [flags]", 1))
这导致:
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
标志和有关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
}
但随后-h
标志仍然输出错误的使用行。
有没有办法做到这一点?提前致谢!