亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號