mirror of https://github.com/docker/cli.git
Use Args in cobra.Command to validate args.
Also re-use context. Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
11ede59379
commit
25892d27be
39
required.go
39
required.go
|
@ -2,30 +2,19 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MinRequiredArgs checks if the minimum number of args exists, and returns an
|
// NoArgs validate args and returns an error if there are any args
|
||||||
// error if they do not.
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
||||||
func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
|
if len(args) == 0 {
|
||||||
if len(args) >= min {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf(
|
if cmd.HasSubCommands() {
|
||||||
"\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s",
|
return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
|
||||||
cmd.CommandPath(),
|
|
||||||
min,
|
|
||||||
cmd.UseLine(),
|
|
||||||
cmd.Short,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AcceptsNoArgs returns an error message if there are args
|
|
||||||
func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
|
|
||||||
if len(args) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
|
@ -35,3 +24,19 @@ func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
|
||||||
cmd.Short,
|
cmd.Short,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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(
|
||||||
|
"\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s",
|
||||||
|
cmd.CommandPath(),
|
||||||
|
min,
|
||||||
|
cmd.UseLine(),
|
||||||
|
cmd.Short,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue