Skip to content
Myr

Stop special casing runnable root commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The special case code to handle a runnable root command had some problems. It was noticed that if you created a runnable root and a subcommand. And the subcommand was then executed with both a valid and invalid flag, the error message was about the valid flag being invalid. For example ./command subcommand --goodflag=10 --badflag=10 Would fail and tell you that --goodflag was an invalid flag. Instead if we just do away with the special Command.execute() for the root command the parser for subcommand is what prints the error and it gets it right...

authored by Eric Paris · committed by Eric Paris · parent fe58172 · Browse files · 1 file+6 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+6 added−49 removed

  • other:+6 added−49 removed

1 file+6 added−49 removed

modified:command.go+6 added-49 removed
303 unmodified lines
304 case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !isBooleanShortFlag(y[1:], c.Flags()):305 inFlag = true306 case inFlag:307 inFlag = false308 case y == "":309 // strip empty commands, as the go tests expect this to be ok....310 case !strings.HasPrefix(y, "-"):311 commands = append(commands, y)312 inFlag = false313 }
62 unmodified lines
376 }377378 commandFound, a := innerfind(c, arrs)379378 // if commander returned and the first argument (if it exists) doesn't379 // match the command name, return nil & error380 if commandFound.Name() == c.Name() && len(arrs[0]) > 0 && commandFound.Name() != arrs[0] {380 // If we matched on the root, but we asked for a subcommand, return an error381 if commandFound.Name() == c.Name() && len(stripFlags(arrs, c)) > 0 && commandFound.Name() != arrs[0] {382 return nil, a, fmt.Errorf("unknown command %q", a[0])383 }384385 return commandFound, a, nil
12 unmodified lines
398399 return findRoot(c)400}401401// execute the command determined by args and the command tree402func (c *Command) findAndExecute(args []string) (err error) {403404 cmd, a, e := c.Find(args)405 if e != nil {406 return e407 }408 return cmd.execute(a)409}410402func (c *Command) execute(a []string) (err error) {403 if c == nil {404 return fmt.Errorf("Called Execute() on a nil Command")405 }
78 unmodified lines
484 } else {485 c.Help()486 }487 } else {497 err = c.findAndExecute(args)498 }499500 // Now handle the case where the root is runnable and only flags are provided501 if err != nil && c.Runnable() {502 // This is pretty much a custom version of the *Command.execute method503 // with a few differences because it's the final command (no fall back)504 e := c.ParseFlags(args)488 cmd, flags, e := c.Find(args)489 if e != nil {506 // Flags parsing had an error.507 // If an error happens here, we have to report it to the user508 c.Println(e.Error())509 // If an error happens search also for subcommand info about that510 if c.cmdErrorBuf != nil && c.cmdErrorBuf.Len() > 0 {511 c.Println(c.cmdErrorBuf.String())512 } else {513 c.Usage()514 }490 err = e516 return491 } else {518 // If help is called, regardless of other flags, we print that519 if c.helpFlagVal {520 c.Help()521 return nil522 }523524 argWoFlags := c.Flags().Args()525 if len(argWoFlags) > 0 {526 // If there are arguments (not flags) one of the earlier527 // cases should have caught it.. It means invalid usage528 // print the usage529 c.Usage()530 } else {531 // Only flags left... Call root.Run532 c.preRun()533 c.Run(c, argWoFlags)534 err = nil535 }492 err = cmd.execute(flags)493 }494 }495496 if err != nil {
419 unmodified lines