Cobra中的命名位置参数

huangapple go评论84阅读模式
英文:

Named Positional Arguments in Cobra

问题

我有以下的Cobra子命令:

package stripeCommands

import (
	"fmt"
	"cmd/cliConstants"
	"github.com/spf13/cobra"
	"log"
)

var (
	deleteCustomerCommand = &cobra.Command{
		Use:        "delete",
		Short:      "Delete Stripe customer(s) by ids.",
		Args:       cobra.MinimumNArgs(1),
		ArgAliases: []string{"stripe_customer_id"},
		PreRun: func(cmd *cobra.Command, args []string) {

		},
		Run: func(cmd *cobra.Command, args []string) {
			log.Printf("IDs: %v", args)
		},
	}
)

func init() {
	flags := deleteCustomerCommand.Flags()

	// -k|--stripe-api-key|STRIPE_API_KEY
	flags.StringP(cliConstants.CLIFlagStripeAPIKey, "k", "",
		fmt.Sprintf("The Stripe API key. [env: %s]", cliConstants.EnvVarStripeAPIKey))
}

这个命令可以通过./my-app stripe customers delete -k $STRIPE_API_KEY $CUSTOMER_ID_1 $CUSTOMER_ID_2来调用。

虽然cobra.MinimumNArgs(1)确保我至少得到一个位置参数,但我找不到一种方法将其显示在帮助文档中:

Error: requires at least 1 arg(s), only received 0
Usage:
  my-app stripe customers delete [flags]

Flags:
  -h, --help                    help for delete
  -k, --stripe-api-key string   The Stripe API key. [env: stripe_api_key]

2021/09/13 12:00:39 Failed to execute command: requires at least 1 arg(s), only received 0

有没有办法告诉Cobra在帮助文档中显示位置参数,像这样:

Usage:
  my-app stripe customers delete [flags] customer_id [...customer_id]

目前的帮助文档对于向用户显示应该传递什么作为位置参数并不是很有帮助。

英文:

I have the following Cobra sub-command:

package stripeCommands

import (
	"fmt"
	"cmd/cliConstants"
	"github.com/spf13/cobra"
	"log"
)

var (
	deleteCustomerCommand = &cobra.Command{
		Use:        "delete",
		Short:      "Delete Stripe customer(s) by ids.",
		Args:       cobra.MinimumNArgs(1),
		ArgAliases: []string{"stripe_customer_id"},
		PreRun: func(cmd *cobra.Command, args []string) {

		},
		Run: func(cmd *cobra.Command, args []string) {
			log.Printf("IDs: %v", args)
		},
	}
)

func init() {
	flags := deleteCustomerCommand.Flags()

	// -k|--stripe-api-key|STRIPE_API_KEY
	flags.StringP(cliConstants.CLIFlagStripeAPIKey, "k", "",
		fmt.Sprintf("The Stripe API key. [env: %s]", cliConstants.EnvVarStripeAPIKey))
}

The idea is to call this via ./my-app stripe customers delete -k $STRIPE_API_KEY $CUSTOMER_ID_1 $CUSTOMER_ID_2.

While cobra.MinimumNArgs(1) does ensure I get at least one positional argument, I can't find a way to make this show up in the help documentation:

Error: requires at least 1 arg(s), only received 0
Usage:
  my-app stripe customers delete [flags]

Flags:
  -h, --help                    help for delete
  -k, --stripe-api-key string   The Stripe API key. [env: stripe_api_key]

2021/09/13 12:00:39 Failed to execute command: requires at least 1 arg(s), only received 0

Is there a way to tell Cobra to display positional args in the help like:

Usage:
  my-app stripe customers delete [flags] customer_id [...customer_id]

Right now the help documentation is not very helpful in displaying to the user what they should pass as positional arguments.

答案1

得分: 1

将您的命令的Use字段设置为:

    deleteCustomerCommand = &cobra.Command{
        Use: "delete [flags] customer_id [...customer_id]",
        ...

有关如何使用它的完整详细信息可以在cmd.UseLine()的代码中找到:
https://github.com/spf13/cobra/blob/v1.2.1/command.go#L1245

英文:

Set the Use field for your command to :

    deleteCustomerCommand = &cobra.Command{
        Use: "delete [flags] customer_id [...customer_id]",
        ...

The complete details of how it is used can be found in the code for cmd.UseLine() :
https://github.com/spf13/cobra/blob/v1.2.1/command.go#L1245

huangapple
  • 本文由 发表于 2021年9月14日 03:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69167939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定