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 Sep 5, 2019
1 parent 14e872f commit f87f62a
Show file tree
Hide file tree
Showing 4 changed files with 492 additions and 133 deletions.
22 changes: 4 additions & 18 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,13 @@ func validateArgs(cmd *Command, args []string) error {
return nil
}

// 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 f87f62a

Please sign in to comment.