2025-04-14 21:51:45 -07:00
|
|
|
// Copyright 2013-2023 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.
|
|
|
|
|
|
|
|
package cobra
|
|
|
|
|
2025-04-14 22:24:17 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2025-04-14 21:51:45 -07:00
|
|
|
|
|
|
|
// InvalidArgCountError is the error returned when the wrong number of arguments
|
|
|
|
// are supplied to a command.
|
|
|
|
type InvalidArgCountError struct {
|
|
|
|
cmd *Command
|
|
|
|
args []string
|
|
|
|
atLeast int
|
|
|
|
atMost int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error.
|
|
|
|
func (e *InvalidArgCountError) Error() string {
|
|
|
|
if e.atMost == -1 && e.atLeast >= 0 { // MinimumNArgs
|
|
|
|
return fmt.Sprintf("requires at least %d arg(s), only received %d", e.atLeast, len(e.args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.atLeast == -1 && e.atMost >= 0 { // MaximumNArgs
|
|
|
|
return fmt.Sprintf("accepts at most %d arg(s), received %d", e.atMost, len(e.args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.atLeast == e.atMost && e.atLeast != -1 { // ExactArgs
|
|
|
|
return fmt.Sprintf("accepts %d arg(s), received %d", e.atLeast, len(e.args))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeArgs
|
|
|
|
return fmt.Sprintf("accepts between %d and %d arg(s), received %d", e.atLeast, e.atMost, len(e.args))
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:44:55 -07:00
|
|
|
// GetCommand returns the Command that the error occurred in.
|
|
|
|
func (e *InvalidArgCountError) GetCommand() *Command {
|
|
|
|
return e.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetArguments returns the invalid arguments.
|
|
|
|
func (e *InvalidArgCountError) GetArguments() []string {
|
|
|
|
return e.args
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMinArgumentCount returns the minimum (inclusive) number of arguments
|
|
|
|
// that the command requires. If there is no minimum, a value of -1 is returned.
|
|
|
|
func (e *InvalidArgCountError) GetMinArgumentCount() int {
|
|
|
|
return e.atLeast
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxArgumentCount returns the maximum (inclusive) number of arguments
|
|
|
|
// that the command requires. If there is no maximum, a value of -1 is returned.
|
|
|
|
func (e *InvalidArgCountError) GetMaxArgumentCount() int {
|
|
|
|
return e.atMost
|
|
|
|
}
|
|
|
|
|
2025-04-14 21:51:45 -07:00
|
|
|
// InvalidArgCountError is the error returned an invalid argument is present.
|
|
|
|
type InvalidArgValueError struct {
|
|
|
|
cmd *Command
|
|
|
|
arg string
|
|
|
|
suggestions string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error.
|
|
|
|
func (e *InvalidArgValueError) Error() string {
|
|
|
|
return fmt.Sprintf("invalid argument %q for %q%s", e.arg, e.cmd.CommandPath(), e.suggestions)
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:44:55 -07:00
|
|
|
// GetCommand returns the Command that the error occurred in.
|
|
|
|
func (e *InvalidArgValueError) GetCommand() *Command {
|
|
|
|
return e.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetArgument returns the invalid argument.
|
|
|
|
func (e *InvalidArgValueError) GetArgument() string {
|
|
|
|
return e.arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSuggestions returns suggestions, if there are any.
|
|
|
|
func (e *InvalidArgValueError) GetSuggestions() string {
|
|
|
|
return e.suggestions
|
|
|
|
}
|
|
|
|
|
2025-04-14 21:51:45 -07:00
|
|
|
// UnknownSubcommandError is the error returned when a subcommand can not be
|
|
|
|
// found.
|
|
|
|
type UnknownSubcommandError struct {
|
|
|
|
cmd *Command
|
|
|
|
subcmd string
|
|
|
|
suggestions string
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:44:55 -07:00
|
|
|
// GetCommand returns the Command that the error occurred in.
|
|
|
|
func (e *UnknownSubcommandError) GetCommand() *Command {
|
|
|
|
return e.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubcommand returns the unknown subcommand name.
|
|
|
|
func (e *UnknownSubcommandError) GetSubcommand() string {
|
|
|
|
return e.subcmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSuggestions returns suggestions, if there are any.
|
|
|
|
func (e *UnknownSubcommandError) GetSuggestions() string {
|
|
|
|
return e.suggestions
|
|
|
|
}
|
|
|
|
|
2025-04-14 21:51:45 -07:00
|
|
|
// Error implements error.
|
|
|
|
func (e *UnknownSubcommandError) Error() string {
|
|
|
|
return fmt.Sprintf("unknown command %q for %q%s", e.subcmd, e.cmd.CommandPath(), e.suggestions)
|
|
|
|
}
|
2025-04-14 22:24:17 -07:00
|
|
|
|
|
|
|
// RequiredFlagError is the error returned when a required flag is not set.
|
|
|
|
type RequiredFlagError struct {
|
2025-04-14 22:44:55 -07:00
|
|
|
cmd *Command
|
2025-04-14 22:24:17 -07:00
|
|
|
missingFlagNames []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error.
|
|
|
|
func (e *RequiredFlagError) Error() string {
|
|
|
|
return fmt.Sprintf(`required flag(s) "%s" not set`, strings.Join(e.missingFlagNames, `", "`))
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:44:55 -07:00
|
|
|
// GetCommand returns the Command that the error occurred in.
|
|
|
|
func (e *RequiredFlagError) GetCommand() *Command {
|
|
|
|
return e.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFlags returns the names of the missing flags.
|
|
|
|
func (e *RequiredFlagError) GetFlags() []string {
|
|
|
|
return e.missingFlagNames
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:24:17 -07:00
|
|
|
// FlagGroupError is the error returned when mutually-required or
|
|
|
|
// mutually-exclusive flags are not properly specified.
|
|
|
|
type FlagGroupError struct {
|
2025-04-14 22:44:55 -07:00
|
|
|
cmd *Command
|
2025-04-14 22:24:17 -07:00
|
|
|
flagList string
|
2025-04-14 22:44:55 -07:00
|
|
|
flagGroupType FlagGroupType
|
2025-04-14 22:24:17 -07:00
|
|
|
problemFlags []string
|
|
|
|
}
|
|
|
|
|
2025-04-14 22:44:55 -07:00
|
|
|
// GetCommand returns the Command that the error occurred in.
|
|
|
|
func (e *FlagGroupError) GetCommand() *Command {
|
|
|
|
return e.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFlagList returns the flags in the group.
|
|
|
|
func (e *FlagGroupError) GetFlags() []string {
|
|
|
|
return strings.Split(e.flagList, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFlagGroupType returns the type of flag group causing the error.
|
|
|
|
//
|
|
|
|
// Valid types are:
|
|
|
|
// - FlagsAreMutuallyExclusive for mutually-exclusive flags.
|
|
|
|
// - FlagsAreRequiredTogether for mutually-required flags.
|
|
|
|
// - FlagsAreOneRequired for flags where at least one must be present.
|
|
|
|
func (e *FlagGroupError) GetFlagGroupType() FlagGroupType {
|
|
|
|
return e.flagGroupType
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProblemFlags returns the flags causing the error.
|
|
|
|
//
|
|
|
|
// For flag groups where:
|
|
|
|
// - FlagsAreMutuallyExclusive, these are all the flags set.
|
|
|
|
// - FlagsAreRequiredTogether, these are the missing flags.
|
|
|
|
// - FlagsAreOneRequired, this is empty.
|
|
|
|
func (e *FlagGroupError) GetProblemFlags() []string {
|
|
|
|
return e.problemFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagGroupType identifies which failed validation caused a FlagGroupError.
|
|
|
|
type FlagGroupType string
|
2025-04-14 22:24:17 -07:00
|
|
|
|
|
|
|
const (
|
2025-04-14 22:44:55 -07:00
|
|
|
FlagsAreMutuallyExclusive FlagGroupType = "if any is set, none of the others can be"
|
|
|
|
FlagsAreRequiredTogether FlagGroupType = "if any is set, they must all be set"
|
|
|
|
FlagsAreOneRequired FlagGroupType = "at least one of the flags is required"
|
2025-04-14 22:24:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error implements error.
|
|
|
|
func (e *FlagGroupError) Error() string {
|
|
|
|
switch e.flagGroupType {
|
2025-04-14 22:44:55 -07:00
|
|
|
case FlagsAreRequiredTogether:
|
2025-04-14 22:24:17 -07:00
|
|
|
return fmt.Sprintf("if any flags in the group [%v] are set they must all be set; missing %v", e.flagList, e.problemFlags)
|
2025-04-14 22:44:55 -07:00
|
|
|
case FlagsAreOneRequired:
|
2025-04-14 22:24:17 -07:00
|
|
|
return fmt.Sprintf("at least one of the flags in the group [%v] is required", e.flagList)
|
2025-04-14 22:44:55 -07:00
|
|
|
case FlagsAreMutuallyExclusive:
|
2025-04-14 22:24:17 -07:00
|
|
|
return fmt.Sprintf("if any flags in the group [%v] are set none of the others can be; %v were all set", e.flagList, e.problemFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("invalid flagGroupType")
|
|
|
|
}
|