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
|
|
|
|
func RequiresMinArgs(min int) cobra.PositionalArgs {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) >= min {
|
|
|
|
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(),
|
|
|
|
min,
|
2017-08-12 12:25:38 -04:00
|
|
|
pluralize("argument", min),
|
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
|
|
|
|
func RequiresMaxArgs(max int) cobra.PositionalArgs {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) <= max {
|
|
|
|
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(),
|
|
|
|
max,
|
2017-08-12 12:25:38 -04:00
|
|
|
pluralize("argument", max),
|
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
|
|
|
|
func RequiresRangeArgs(min int, max int) cobra.PositionalArgs {
|
2016-06-05 19:48:26 -04:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) >= min && len(args) <= max {
|
|
|
|
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(),
|
|
|
|
min,
|
|
|
|
max,
|
2017-08-12 12:25:38 -04:00
|
|
|
pluralize("argument", max),
|
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
|
|
|
|
2019-04-02 10:17:53 -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"
|
|
|
|
}
|