为了账号安全,请及时绑定邮箱和手机立即绑定

Cobra 更改帮助模板中的用法行

Cobra 更改帮助模板中的用法行

Go
莫回无 2023-06-05 17:37:45
Usage如果在 Go 中的 cobra 命令上调用帮助函数,我希望能够设置该行以指定要传递的参数 NEEDS。这是常规帮助标志输出的内容: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 cancelGlobal 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 cancelGlobal 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标志仍然输出错误的用法行。有没有办法做到这一点?提前致谢!
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

更改用法名称的外观。您可以将其传递给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


查看完整回答
反对 回复 2023-06-05
  • 1 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信