// 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 cobraimport ()typePositionalArgsfunc(cmd *Command, args []string) error// legacyArgs 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 argumentsfunc legacyArgs( *Command, []string) error {// no subcommand, always take argsif !.HasSubCommands() {returnnil }// root command with subcommands, do subcommand checking.if !.HasParent() && len() > 0 {returnfmt.Errorf("unknown command %q for %q%s", [0], .CommandPath(), .findSuggestions([0])) }returnnil}// NoArgs returns an error if any args are included.func ( *Command, []string) error {iflen() > 0 {returnfmt.Errorf("unknown command %q for %q", [0], .CommandPath()) }returnnil}// OnlyValidArgs returns an error if there are any positional args that are not in// the `ValidArgs` field of `Command`func ( *Command, []string) error {iflen(.ValidArgs) > 0 {// Remove any description that may be included in ValidArgs. // A description is following a tab character. := make([]string, 0, len(.ValidArgs))for , := range .ValidArgs { = append(, strings.SplitN(, "\t", 2)[0]) }for , := range {if !stringInSlice(, ) {returnfmt.Errorf("invalid argument %q for %q%s", , .CommandPath(), .findSuggestions([0])) } } }returnnil}// ArbitraryArgs never returns an error.func ( *Command, []string) error {returnnil}// MinimumNArgs returns an error if there is not at least N args.func ( int) PositionalArgs {returnfunc( *Command, []string) error {iflen() < {returnfmt.Errorf("requires at least %d arg(s), only received %d", , len()) }returnnil }}// MaximumNArgs returns an error if there are more than N args.func ( int) PositionalArgs {returnfunc( *Command, []string) error {iflen() > {returnfmt.Errorf("accepts at most %d arg(s), received %d", , len()) }returnnil }}// ExactArgs returns an error if there are not exactly n args.func ( int) PositionalArgs {returnfunc( *Command, []string) error {iflen() != {returnfmt.Errorf("accepts %d arg(s), received %d", , len()) }returnnil }}// RangeArgs returns an error if the number of args is not within the expected range.func ( int, int) PositionalArgs {returnfunc( *Command, []string) error {iflen() < || len() > {returnfmt.Errorf("accepts between %d and %d arg(s), received %d", , , len()) }returnnil }}// MatchAll allows combining several PositionalArgs to work in concert.func ( ...PositionalArgs) PositionalArgs {returnfunc( *Command, []string) error {for , := range {if := (, ); != nil {return } }returnnil }}// 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) insteadfunc ( int) PositionalArgs {returnMatchAll(ExactArgs(), OnlyValidArgs)}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.