podman
131 строка · 4.0 Кб
1// Copyright 2013-2023 The Cobra Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cobra16
17import (18"fmt"19"strings"20)
21
22type PositionalArgs func(cmd *Command, args []string) error23
24// legacyArgs validation has the following behaviour:
25// - root commands with no subcommands can take arbitrary arguments
26// - root commands with subcommands will do subcommand validity checking
27// - subcommands will always accept arbitrary arguments
28func legacyArgs(cmd *Command, args []string) error {29// no subcommand, always take args30if !cmd.HasSubCommands() {31return nil32}33
34// root command with subcommands, do subcommand checking.35if !cmd.HasParent() && len(args) > 0 {36return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))37}38return nil39}
40
41// NoArgs returns an error if any args are included.
42func NoArgs(cmd *Command, args []string) error {43if len(args) > 0 {44return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())45}46return nil47}
48
49// OnlyValidArgs returns an error if there are any positional args that are not in
50// the `ValidArgs` field of `Command`
51func OnlyValidArgs(cmd *Command, args []string) error {52if len(cmd.ValidArgs) > 0 {53// Remove any description that may be included in ValidArgs.54// A description is following a tab character.55var validArgs []string56for _, v := range cmd.ValidArgs {57validArgs = append(validArgs, strings.Split(v, "\t")[0])58}59for _, v := range args {60if !stringInSlice(v, validArgs) {61return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))62}63}64}65return nil66}
67
68// ArbitraryArgs never returns an error.
69func ArbitraryArgs(cmd *Command, args []string) error {70return nil71}
72
73// MinimumNArgs returns an error if there is not at least N args.
74func MinimumNArgs(n int) PositionalArgs {75return func(cmd *Command, args []string) error {76if len(args) < n {77return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))78}79return nil80}81}
82
83// MaximumNArgs returns an error if there are more than N args.
84func MaximumNArgs(n int) PositionalArgs {85return func(cmd *Command, args []string) error {86if len(args) > n {87return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))88}89return nil90}91}
92
93// ExactArgs returns an error if there are not exactly n args.
94func ExactArgs(n int) PositionalArgs {95return func(cmd *Command, args []string) error {96if len(args) != n {97return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))98}99return nil100}101}
102
103// RangeArgs returns an error if the number of args is not within the expected range.
104func RangeArgs(min int, max int) PositionalArgs {105return func(cmd *Command, args []string) error {106if len(args) < min || len(args) > max {107return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))108}109return nil110}111}
112
113// MatchAll allows combining several PositionalArgs to work in concert.
114func MatchAll(pargs ...PositionalArgs) PositionalArgs {115return func(cmd *Command, args []string) error {116for _, parg := range pargs {117if err := parg(cmd, args); err != nil {118return err119}120}121return nil122}123}
124
125// ExactValidArgs returns an error if there are not exactly N positional args OR
126// there are any positional args that are not in the `ValidArgs` field of `Command`
127//
128// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead
129func ExactValidArgs(n int) PositionalArgs {130return MatchAll(ExactArgs(n), OnlyValidArgs)131}
132