Add helper function to validate exact args in cmdutil
This commit is contained in:
parent
cbf8a0d964
commit
56ead91702
4 changed files with 21 additions and 3 deletions
|
|
@ -49,7 +49,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
|
|||
cmd := &cobra.Command{
|
||||
Use: "edit {<id> | <url>}",
|
||||
Short: "Edit one of your gists",
|
||||
Args: cmdutil.MinimumArgs(1, "cannot edit: gist argument required"),
|
||||
Args: cmdutil.ExactArgs(1, "cannot edit: gist argument required"),
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
opts.Selector = args[0]
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
|
|||
cmd := &cobra.Command{
|
||||
Use: "view {<id> | <url>}",
|
||||
Short: "View a gist",
|
||||
Args: cmdutil.MinimumArgs(1, "cannot view: gist argument required"),
|
||||
Args: cmdutil.ExactArgs(1, "cannot view: gist argument required"),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.Selector = args[0]
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
|
|||
cmd := &cobra.Command{
|
||||
Use: "checkout {<number> | <url> | <branch>}",
|
||||
Short: "Check out a pull request in git",
|
||||
Args: cmdutil.MinimumArgs(1, "argument required"),
|
||||
Args: cmdutil.ExactArgs(1, "argument required"),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
|
|
|||
|
|
@ -21,6 +21,24 @@ func MinimumArgs(n int, msg string) cobra.PositionalArgs {
|
|||
}
|
||||
}
|
||||
|
||||
func ExactArgs(n int, msg string) cobra.PositionalArgs {
|
||||
if msg == "" {
|
||||
return cobra.MinimumNArgs(1)
|
||||
}
|
||||
|
||||
return func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) > n {
|
||||
return &FlagError{Err: errors.New("too many arguments")}
|
||||
}
|
||||
|
||||
if len(args) < n {
|
||||
return &FlagError{Err: errors.New("not enough arguments")}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue