2022-11-03 09:48:27 -04:00
|
|
|
// Copyright 2013-2022 The Cobra Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-03 16:38:09 -04:00
|
|
|
"strings"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type PositionalArgs func(cmd *Command, args []string) error
|
|
|
|
|
|
|
|
// Legacy arg validation has the following behaviour:
|
|
|
|
// - root commands with no subcommands can take arbitrary arguments
|
|
|
|
// - root commands with subcommands will do subcommand validity checking
|
|
|
|
// - subcommands will always accept arbitrary arguments
|
|
|
|
func legacyArgs(cmd *Command, args []string) error {
|
|
|
|
// no subcommand, always take args
|
|
|
|
if !cmd.HasSubCommands() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// root command with subcommands, do subcommand checking.
|
2017-04-17 18:08:24 -04:00
|
|
|
if !cmd.HasParent() && len(args) > 0 {
|
|
|
|
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// NoArgs returns an error if any args are included.
|
2017-04-17 18:08:24 -04:00
|
|
|
func NoArgs(cmd *Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 09:48:27 -04:00
|
|
|
// OnlyValidArgs returns an error if there are any positional args that are not in
|
|
|
|
// the `ValidArgs` field of `Command`
|
2017-04-17 18:08:24 -04:00
|
|
|
func OnlyValidArgs(cmd *Command, args []string) error {
|
|
|
|
if len(cmd.ValidArgs) > 0 {
|
2020-05-03 16:38:09 -04:00
|
|
|
// Remove any description that may be included in ValidArgs.
|
|
|
|
// A description is following a tab character.
|
|
|
|
var validArgs []string
|
|
|
|
for _, v := range cmd.ValidArgs {
|
|
|
|
validArgs = append(validArgs, strings.Split(v, "\t")[0])
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
for _, v := range args {
|
2020-05-03 16:38:09 -04:00
|
|
|
if !stringInSlice(v, validArgs) {
|
2017-04-17 18:08:24 -04:00
|
|
|
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// ArbitraryArgs never returns an error.
|
2017-04-17 18:08:24 -04:00
|
|
|
func ArbitraryArgs(cmd *Command, args []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// MinimumNArgs returns an error if there is not at least N args.
|
2017-04-17 18:08:24 -04:00
|
|
|
func MinimumNArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) < n {
|
|
|
|
return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// MaximumNArgs returns an error if there are more than N args.
|
2017-04-17 18:08:24 -04:00
|
|
|
func MaximumNArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) > n {
|
|
|
|
return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// ExactArgs returns an error if there are not exactly n args.
|
2017-04-17 18:08:24 -04:00
|
|
|
func ExactArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) != n {
|
|
|
|
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:42:11 -05:00
|
|
|
// RangeArgs returns an error if the number of args is not within the expected range.
|
2017-04-17 18:08:24 -04:00
|
|
|
func RangeArgs(min int, max int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) < min || len(args) > max {
|
|
|
|
return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 09:15:48 -04:00
|
|
|
|
|
|
|
// MatchAll allows combining several PositionalArgs to work in concert.
|
|
|
|
func MatchAll(pargs ...PositionalArgs) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
for _, parg := range pargs {
|
|
|
|
if err := parg(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 09:48:27 -04:00
|
|
|
|
|
|
|
// ExactValidArgs returns an error if there are not exactly N positional args OR
|
|
|
|
// there are any positional args that are not in the `ValidArgs` field of `Command`
|
|
|
|
//
|
|
|
|
// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead
|
|
|
|
func ExactValidArgs(n int) PositionalArgs {
|
|
|
|
return MatchAll(ExactArgs(n), OnlyValidArgs)
|
|
|
|
}
|