2016-04-19 16:59:48 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-26 21:57:31 +00:00
|
|
|
"strings"
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-05-26 21:57:31 +00:00
|
|
|
// NoArgs validate args and returns an error if there are any args
|
|
|
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
2016-04-19 16:59:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:57:31 +00:00
|
|
|
if cmd.HasSubCommands() {
|
|
|
|
return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
|
2016-05-16 21:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(
|
2016-06-04 14:19:54 +00:00
|
|
|
"\"%s\" accepts no argument(s).\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
|
|
|
cmd.CommandPath(),
|
2016-05-16 21:20:29 +00:00
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
2016-05-26 21:57:31 +00: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
|
|
|
|
}
|
|
|
|
return fmt.Errorf(
|
2016-06-04 14:19:54 +00:00
|
|
|
"\"%s\" requires at least %d argument(s).\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-05-26 21:57:31 +00:00
|
|
|
cmd.CommandPath(),
|
|
|
|
min,
|
2016-06-04 14:19:54 +00:00
|
|
|
cmd.CommandPath(),
|
2016-05-26 21:57:31 +00:00
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 17:50:01 +00: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
|
|
|
|
}
|
|
|
|
return fmt.Errorf(
|
2016-06-04 14:19:54 +00:00
|
|
|
"\"%s\" requires exactly %d argument(s).\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
2016-06-03 17:50:01 +00:00
|
|
|
cmd.CommandPath(),
|
|
|
|
number,
|
2016-06-04 14:19:54 +00:00
|
|
|
cmd.CommandPath(),
|
2016-06-03 17:50:01 +00:00
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|