args.go
This file
Path
args.go
144 lines · 4.4 KiB · Go
Last changed by
Add cobra unique args validator (#2397)takanuva15before adoptionad460ea
History of this fileTake it
The walk stopped after 200 commits: lines older than 89d7b83 point at it.
update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a
line 1why, one step back1// Copyright 2013-2023 The Cobra Authorsadd missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9
lines 2–14why, one step back2//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 at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.14Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 15–23why, one step back15package cobra1617import (18 "fmt"19 "strings"20)2122type PositionalArgs func(cmd *Command, args []string) error23fix: func name in doc strings (#1885)Yash Ladhabefore adoptionbf11ab6
line 24why, one step back24// legacyArgs validation has the following behaviour:Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 25–48why, one step back25// - root commands with no subcommands can take arbitrary arguments26// - root commands with subcommands will do subcommand validity checking27// - subcommands will always accept arbitrary arguments28func legacyArgs(cmd *Command, args []string) error {29 // no subcommand, always take args30 if !cmd.HasSubCommands() {31 return nil32 }3334 // root command with subcommands, do subcommand checking.35 if !cmd.HasParent() && len(args) > 0 {36 return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))37 }38 return nil39}4041// NoArgs returns an error if any args are included.42func NoArgs(cmd *Command, args []string) error {43 if len(args) > 0 {44 return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())45 }46 return nil47}48Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 49–50why, one step back49// OnlyValidArgs returns an error if there are any positional args that are not in50// the `ValidArgs` field of `Command`Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 51–54why, one step back51func OnlyValidArgs(cmd *Command, args []string) error {52 if len(cmd.ValidArgs) > 0 {53 // Remove any description that may be included in ValidArgs.54 // A description is following a tab character.Micro-optimizations (#1957)Ville Skyttäbefore adoption3d8ac43
line 55why, one step back55 validArgs := make([]string, 0, len(cmd.ValidArgs))Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
line 56why, one step back56 for _, v := range cmd.ValidArgs {Micro-optimizations (#1957)Ville Skyttäbefore adoption3d8ac43
line 57why, one step back57 validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0])Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 58–67why, one step back58 }59 for _, v := range args {60 if !stringInSlice(v, validArgs) {61 return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))62 }63 }64 }65 return nil66}67Add cobra unique args validator (#2397)takanuva15before adoptionad460ea
lines 68–80why, one step back68// NoDuplicateArgs returns an error if there are any duplicate positional args.69func NoDuplicateArgs(cmd *Command, args []string) error {70 seen := make(map[string]struct{}, len(args))71 for _, arg := range args {72 if _, ok := seen[arg]; ok {73 return fmt.Errorf("duplicate argument %q for %q", arg, cmd.CommandPath())74 }75 seen[arg] = struct{}{}76 }7778 return nil79}80Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 81–135why, one step back81// ArbitraryArgs never returns an error.82func ArbitraryArgs(cmd *Command, args []string) error {83 return nil84}8586// MinimumNArgs returns an error if there is not at least N args.87func MinimumNArgs(n int) PositionalArgs {88 return func(cmd *Command, args []string) error {89 if len(args) < n {90 return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))91 }92 return nil93 }94}9596// MaximumNArgs returns an error if there are more than N args.97func MaximumNArgs(n int) PositionalArgs {98 return func(cmd *Command, args []string) error {99 if len(args) > n {100 return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))101 }102 return nil103 }104}105106// ExactArgs returns an error if there are not exactly n args.107func ExactArgs(n int) PositionalArgs {108 return func(cmd *Command, args []string) error {109 if len(args) != n {110 return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))111 }112 return nil113 }114}115116// RangeArgs returns an error if the number of args is not within the expected range.117func RangeArgs(min int, max int) PositionalArgs {118 return func(cmd *Command, args []string) error {119 if len(args) < min || len(args) > max {120 return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))121 }122 return nil123 }124}125126// MatchAll allows combining several PositionalArgs to work in concert.127func MatchAll(pargs ...PositionalArgs) PositionalArgs {128 return func(cmd *Command, args []string) error {129 for _, parg := range pargs {130 if err := parg(cmd, args); err != nil {131 return err132 }133 }134 return nil135 }Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 136–143why, one step back136}137138// ExactValidArgs returns an error if there are not exactly N positional args OR139// there are any positional args that are not in the `ValidArgs` field of `Command`140//141// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead142func ExactValidArgs(n int) PositionalArgs {143 return MatchAll(ExactArgs(n), OnlyValidArgs)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
line 144why, one step back144}