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
}
- 1 回答
- 0 關注
- 183 瀏覽
添加回答
舉報
0/150
提交
取消