args.go
This file
Path
args.go
121 lines · 3.4 KiB · Go
Last changed by
enable composing PositionalArgs (#896)Nelzbefore adoption45e521e
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ea93dd point at it.
Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 1–4why, one step back1package cobra23import (4 "fmt"Fish completion using Go completion (#1048)Marc Khouzambefore adoptiona684a6d
line 5why, one step back5 "strings"Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 6–37why, one step back6)78type PositionalArgs func(cmd *Command, args []string) error910// Legacy arg validation has the following behaviour:11// - root commands with no subcommands can take arbitrary arguments12// - root commands with subcommands will do subcommand validity checking13// - subcommands will always accept arbitrary arguments14func legacyArgs(cmd *Command, args []string) error {15 // no subcommand, always take args16 if !cmd.HasSubCommands() {17 return nil18 }1920 // root command with subcommands, do subcommand checking.21 if !cmd.HasParent() && len(args) > 0 {22 return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))23 }24 return nil25}2627// NoArgs returns an error if any args are included.28func NoArgs(cmd *Command, args []string) error {29 if len(args) > 0 {30 return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())31 }32 return nil33}3435// OnlyValidArgs returns an error if any args are not in the list of ValidArgs.36func OnlyValidArgs(cmd *Command, args []string) error {37 if len(cmd.ValidArgs) > 0 {Fish completion using Go completion (#1048)Marc Khouzambefore adoptiona684a6d
lines 38–44why, one step back38 // Remove any description that may be included in ValidArgs.39 // A description is following a tab character.40 var validArgs []string41 for _, v := range cmd.ValidArgs {42 validArgs = append(validArgs, strings.Split(v, "\t")[0])43 }44Fixed linter issuesJuan Lenibefore adoption0ea93dd
line 45why, one step back45 for _, v := range args {Fish completion using Go completion (#1048)Marc Khouzambefore adoptiona684a6d
line 46why, one step back46 if !stringInSlice(v, validArgs) {Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 47–109why, one step back47 return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))48 }49 }50 }51 return nil52}5354// ArbitraryArgs never returns an error.55func ArbitraryArgs(cmd *Command, args []string) error {56 return nil57}5859// MinimumNArgs returns an error if there is not at least N args.60func MinimumNArgs(n int) PositionalArgs {61 return func(cmd *Command, args []string) error {62 if len(args) < n {63 return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))64 }65 return nil66 }67}6869// MaximumNArgs returns an error if there are more than N args.70func MaximumNArgs(n int) PositionalArgs {71 return func(cmd *Command, args []string) error {72 if len(args) > n {73 return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))74 }75 return nil76 }77}7879// ExactArgs returns an error if there are not exactly n args.80func ExactArgs(n int) PositionalArgs {81 return func(cmd *Command, args []string) error {82 if len(args) != n {83 return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))84 }85 return nil86 }87}8889// ExactValidArgs returns an error if90// there are not exactly N positional args OR91// there are any positional args that are not in the `ValidArgs` field of `Command`92func ExactValidArgs(n int) PositionalArgs {93 return func(cmd *Command, args []string) error {94 if err := ExactArgs(n)(cmd, args); err != nil {95 return err96 }97 return OnlyValidArgs(cmd, args)98 }99}100101// RangeArgs returns an error if the number of args is not within the expected range.102func RangeArgs(min int, max int) PositionalArgs {103 return func(cmd *Command, args []string) error {104 if len(args) < min || len(args) > max {105 return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))106 }107 return nil108 }109}enable composing PositionalArgs (#896)Nelzbefore adoption45e521e
lines 110–121why, one step back110111// MatchAll allows combining several PositionalArgs to work in concert.112func MatchAll(pargs ...PositionalArgs) PositionalArgs {113 return func(cmd *Command, args []string) error {114 for _, parg := range pargs {115 if err := parg(cmd, args); err != nil {116 return err117 }118 }119 return nil120 }121}