2016-04-19 12:59:48 -04:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2016-05-26 17:57:31 -04:00
|
|
|
"strings"
|
2016-04-19 12:59:48 -04:00
|
|
|
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-07-03 08:47:39 -04:00
|
|
|
// NoArgs validates args and returns an error if there are any args
|
2016-05-26 17:57:31 -04:00
|
|
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
2016-04-19 12:59:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:57:31 -04:00
|
|
|
if cmd.HasSubCommands() {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
|
2016-05-16 17:20:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2017-08-12 12:25:38 -04:00
|
|
|
"%q accepts no arguments.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-06-04 10:19:54 -04:00
|
|
|
cmd.CommandPath(),
|
2016-05-16 17:20:29 -04:00
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
2016-05-26 17:57:31 -04:00
|
|
|
|
|
|
|
// RequiresMinArgs returns an error if there is not at least min args
|
2024-08-26 07:55:28 -04:00
|
|
|
func RequiresMinArgs(minArgs int) cobra.PositionalArgs {
|
2016-05-26 17:57:31 -04:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2024-08-26 07:55:28 -04:00
|
|
|
if len(args) >= minArgs {
|
2016-05-26 17:57:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2017-08-12 12:25:38 -04:00
|
|
|
"%q requires at least %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-05-26 17:57:31 -04:00
|
|
|
cmd.CommandPath(),
|
2024-08-26 07:55:28 -04:00
|
|
|
minArgs,
|
|
|
|
pluralize("argument", minArgs),
|
2016-06-04 10:19:54 -04:00
|
|
|
cmd.CommandPath(),
|
2016-05-26 17:57:31 -04:00
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 13:50:01 -04:00
|
|
|
|
2016-06-05 23:55:47 -04:00
|
|
|
// RequiresMaxArgs returns an error if there is not at most max args
|
2024-08-26 07:55:28 -04:00
|
|
|
func RequiresMaxArgs(maxArgs int) cobra.PositionalArgs {
|
2016-06-05 23:55:47 -04:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2024-08-26 07:55:28 -04:00
|
|
|
if len(args) <= maxArgs {
|
2016-06-05 23:55:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2017-08-12 12:25:38 -04:00
|
|
|
"%q requires at most %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-06-05 23:55:47 -04:00
|
|
|
cmd.CommandPath(),
|
2024-08-26 07:55:28 -04:00
|
|
|
maxArgs,
|
|
|
|
pluralize("argument", maxArgs),
|
2016-06-05 23:55:47 -04:00
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 10:57:44 -04:00
|
|
|
// RequiresRangeArgs returns an error if there is not at least min args and at most max args
|
2024-08-26 07:55:28 -04:00
|
|
|
func RequiresRangeArgs(minArgs int, maxArgs int) cobra.PositionalArgs {
|
2016-06-05 19:48:26 -04:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2024-08-26 07:55:28 -04:00
|
|
|
if len(args) >= minArgs && len(args) <= maxArgs {
|
2016-06-05 19:48:26 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2017-08-12 12:25:38 -04:00
|
|
|
"%q requires at least %d and at most %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-06-05 19:48:26 -04:00
|
|
|
cmd.CommandPath(),
|
2024-08-26 07:55:28 -04:00
|
|
|
minArgs,
|
|
|
|
maxArgs,
|
|
|
|
pluralize("argument", maxArgs),
|
2016-06-05 19:48:26 -04:00
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 13:50:01 -04:00
|
|
|
// ExactArgs returns an error if there is not the exact number of args
|
|
|
|
func ExactArgs(number int) cobra.PositionalArgs {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == number {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2017-08-12 12:25:38 -04:00
|
|
|
"%q requires exactly %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-06-03 13:50:01 -04:00
|
|
|
cmd.CommandPath(),
|
|
|
|
number,
|
2017-08-12 12:25:38 -04:00
|
|
|
pluralize("argument", number),
|
2016-06-04 10:19:54 -04:00
|
|
|
cmd.CommandPath(),
|
2016-06-03 13:50:01 -04:00
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 12:25:38 -04:00
|
|
|
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:unparam
|
2017-08-12 12:25:38 -04:00
|
|
|
func pluralize(word string, number int) string {
|
|
|
|
if number == 1 {
|
|
|
|
return word
|
|
|
|
}
|
|
|
|
return word + "s"
|
|
|
|
}
|