2023-03-06 02:28:31 +00:00
// Copyright 2013-2023 The Cobra Authors
2022-09-16 13:55:56 +02:00
//
// 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.
2016-05-07 13:24:05 -04:00
package cobra
import (
"fmt"
2020-04-10 15:56:28 -04:00
"strings"
2016-05-07 13:24:05 -04:00
)
type PositionalArgs func ( cmd * Command , args [ ] string ) error
2025-04-26 20:11:13 +00:00
// legacyArgs validates the arguments for a given command based on whether it has subcommands or not.
//
// Behavior:
// - Root commands without subcommands can take arbitrary arguments.
// - Root commands with subcommands perform subcommand-specific validity checks.
// - Subcommands always accept arbitrary arguments.
//
// Parameters:
// - cmd: A pointer to the Command struct representing the command being checked.
// - args: A slice of strings representing the arguments passed to the command.
//
// Returns:
// - error: If the arguments are invalid for the given command, an error is returned. Otherwise, nil.
2016-05-07 13:24:05 -04:00
func legacyArgs ( cmd * Command , args [ ] string ) error {
// no subcommand, always take args
if ! cmd . HasSubCommands ( ) {
return nil
}
2017-10-31 19:57:53 +01:00
// root command with subcommands, do subcommand checking.
2016-05-07 13:24:05 -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
}
2025-04-26 20:11:13 +00:00
// NoArgs returns an error if any arguments are provided.
// It checks whether the given slice of arguments `args` is empty.
// If `args` contains elements, it indicates that unexpected arguments were provided,
// and thus returns a formatted error indicating the unknown command and its path.
// If no arguments are present, it returns nil, indicating successful validation.
2016-05-07 13:24:05 -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
}
2025-04-26 20:11:13 +00:00
// OnlyValidArgs checks if the provided arguments are valid based on the `ValidArgs` field of the given command.
// It returns an error if any argument is not a valid option. If no validation is required, it returns nil.
// The ValidArgs field can contain descriptions for each valid argument, which should be ignored during validation.
2016-05-07 13:24:05 -04:00
func OnlyValidArgs ( cmd * Command , args [ ] string ) error {
if len ( cmd . ValidArgs ) > 0 {
2020-04-10 15:56:28 -04:00
// Remove any description that may be included in ValidArgs.
// A description is following a tab character.
2023-11-23 19:24:33 +02:00
validArgs := make ( [ ] string , 0 , len ( cmd . ValidArgs ) )
2020-04-10 15:56:28 -04:00
for _ , v := range cmd . ValidArgs {
2023-11-23 19:24:33 +02:00
validArgs = append ( validArgs , strings . SplitN ( v , "\t" , 2 ) [ 0 ] )
2020-04-10 15:56:28 -04:00
}
2016-05-07 13:24:05 -04:00
for _ , v := range args {
2020-04-10 15:56:28 -04:00
if ! stringInSlice ( v , validArgs ) {
2016-05-07 13:24:05 -04:00
return fmt . Errorf ( "invalid argument %q for %q%s" , v , cmd . CommandPath ( ) , cmd . findSuggestions ( args [ 0 ] ) )
}
}
}
return nil
}
2025-04-26 20:11:13 +00:00
// ArbitraryArgs executes a command with arbitrary arguments and does not return an error.
2016-05-07 13:24:05 -04:00
func ArbitraryArgs ( cmd * Command , args [ ] string ) error {
return nil
}
2025-04-26 20:11:13 +00:00
// MinimumNArgs returns a PositionalArgs function that checks if there are at least N arguments provided.
// If fewer than N arguments are given, it returns an error with a message indicating the required number of arguments and the actual count received.
2016-05-07 13:24:05 -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
}
}
2025-04-26 20:11:13 +00:00
// MaximumNArgs returns a PositionalArgs function that ensures no more than N arguments are provided.
// If the number of arguments exceeds N, it returns an error indicating the maximum allowed arguments and the actual count received.
2016-05-07 13:24:05 -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
}
}
2025-04-26 20:11:13 +00:00
// ExactArgs returns a PositionalArgs function that checks if the command has exactly `n` arguments.
// If the number of arguments is not exactly `n`, it returns an error with a descriptive message.
// Otherwise, it returns nil indicating no error.
2016-05-07 13:24:05 -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-10-31 19:57:53 +01:00
// RangeArgs returns an error if the number of args is not within the expected range.
2025-04-26 20:11:13 +00:00
// It takes two integers, min and max, representing the minimum and maximum number of arguments allowed.
// The function returns a PositionalArgs closure that can be used as a validator for command arguments.
// If the number of arguments does not fall within the specified range (inclusive), it returns an error with details about the expected and received argument count.
2016-05-07 13:24:05 -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
}
}
2021-12-07 14:38:00 -08:00
2025-04-26 20:11:13 +00:00
// MatchAll combines multiple PositionalArgs to work as a single PositionalArg.
// It applies each provided PositionalArg in sequence to the command and arguments.
// If any of the PositionalArgs return an error, that error is returned immediately.
// If all PositionalArgs are successfully applied, nil is returned.
2021-12-07 14:38:00 -08:00
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-09-10 15:33:34 +02:00
// ExactValidArgs returns an error if there are not exactly N positional args OR
2025-04-26 20:11:13 +00:00
// there are any positional args that are not in the `ValidArgs` field of `Command`.
2022-09-10 15:33:34 +02:00
//
2025-04-26 20:11:13 +00:00
// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead.
2022-09-10 15:33:34 +02:00
func ExactValidArgs ( n int ) PositionalArgs {
return MatchAll ( ExactArgs ( n ) , OnlyValidArgs )
}