Skip to content
Myr

args.go

This file

Path

args.go

131 lines · 4.0 KiB · Go

Last changed by

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

History of this file

Take it

The walk stopped after 200 commits: lines older than 9ed1d71 point at it.

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

lines 1–14why, one step back
1// Copyright 2013-2022 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 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.14

bugfix/cli: Temporary fix for go get on cobra cli (#1200)Joshua Harshmanbefore adoption9ed1d71

lines 15–48why, one step back
15package cobra
1617import (18 "fmt"19 "strings"20)2122type PositionalArgs func(cmd *Command, args []string) error2324// Legacy arg validation has the following behaviour:25// - 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}48

Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6

lines 49–50why, one step back
49// OnlyValidArgs returns an error if there are any positional args that are not in
50// the `ValidArgs` field of `Command`

bugfix/cli: Temporary fix for go get on cobra cli (#1200)Joshua Harshmanbefore adoption9ed1d71

lines 51–111why, one step back
51func 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.55 var validArgs []string56 for _, v := range cmd.ValidArgs {57 validArgs = append(validArgs, strings.Split(v, "\t")[0])58 }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}6768// ArbitraryArgs never returns an error.69func ArbitraryArgs(cmd *Command, args []string) error {70 return nil71}7273// MinimumNArgs returns an error if there is not at least N args.74func MinimumNArgs(n int) PositionalArgs {75 return func(cmd *Command, args []string) error {76 if len(args) < n {77 return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))78 }79 return nil80 }81}8283// MaximumNArgs returns an error if there are more than N args.84func MaximumNArgs(n int) PositionalArgs {85 return func(cmd *Command, args []string) error {86 if len(args) > n {87 return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))88 }89 return nil90 }91}9293// ExactArgs returns an error if there are not exactly n args.94func ExactArgs(n int) PositionalArgs {95 return func(cmd *Command, args []string) error {96 if len(args) != n {97 return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))98 }99 return nil100 }101}102103// RangeArgs returns an error if the number of args is not within the expected range.104func RangeArgs(min int, max int) PositionalArgs {105 return func(cmd *Command, args []string) error {106 if len(args) < min || len(args) > max {107 return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))108 }109 return nil110 }111}

enable composing PositionalArgs (#896)Nelzbefore adoption45e521e

lines 112–122why, one step back
112
113// MatchAll allows combining several PositionalArgs to work in concert.114func MatchAll(pargs ...PositionalArgs) PositionalArgs {115 return func(cmd *Command, args []string) error {116 for _, parg := range pargs {117 if err := parg(cmd, args); err != nil {118 return err119 }120 }121 return nil122 }

Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6

lines 123–130why, one step back
123}
124125// ExactValidArgs returns an error if there are not exactly N positional args OR126// there are any positional args that are not in the `ValidArgs` field of `Command`127//128// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead129func ExactValidArgs(n int) PositionalArgs {130 return MatchAll(ExactArgs(n), OnlyValidArgs)

enable composing PositionalArgs (#896)Nelzbefore adoption45e521e

line 131why, one step back
131}