在帮助模板眼镜蛇改变使用习惯

问题描述 投票:1回答:1

我希望能够设置Usage行指定的参数需要传递如果帮助功能,在围棋眼镜蛇命令调用。

这就是经常帮助标志输出:

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)

我已经使用SetUsageTemplateinit()功能尝试,但则删除标志的一部分:

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标志输出错误的使用习惯依然。

有没有办法做到这一点?提前致谢!

go command-line go-cobra
1个回答
4
投票

要改变如何使用名称的样子。您可以在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
} 
© www.soinside.com 2019 - 2024. All rights reserved.