Skip to content

Commit

Permalink
feat: add '[args]' and 'Valid Args:' to --help
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Mar 21, 2019
1 parent 2e3862d commit fec0a0b
Show file tree
Hide file tree
Showing 4 changed files with 503 additions and 153 deletions.
22 changes: 4 additions & 18 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,13 @@ import (

type PositionalArgs func(cmd *Command, args []string) error

// Legacy arg validation has the following behaviour:
// - root commands with no subcommands can take arbitrary arguments
// - root commands with subcommands will do subcommand validity checking
// - subcommands will always accept arbitrary arguments
func legacyArgs(cmd *Command, args []string) error {
// no subcommand, always take args
if !cmd.HasSubCommands() {
return nil
}

// root command with subcommands, do subcommand checking.
if !cmd.HasParent() && len(args) > 0 {
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
}
return nil
}

// NoArgs returns an error if any args are included.
func NoArgs(cmd *Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
if cmd.HasAvailableSubCommands() {
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
}
return fmt.Errorf("\"%s\" rejected; %q does not accept args", args[0], cmd.CommandPath())
}
return nil
}
Expand Down
Loading

0 comments on commit fec0a0b

Please sign in to comment.