Skip to content

Commit 5a56b57

Browse files
committed
Make unknown subcommand a sentinel error
Will help consumers avoid duplicating the error output. Also, why the hell did I write the code this way in the first place?
1 parent 539d63d commit 5a56b57

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

help.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ func (lm *newlineLimiter) Write(p []byte) (int, error) {
322322

323323
var usageWantsArgRe = regexp.MustCompile(`<.*>`)
324324

325+
type UnknownSubcommandError struct {
326+
Args []string
327+
}
328+
329+
func (e *UnknownSubcommandError) Error() string {
330+
return fmt.Sprintf("unknown subcommand %q", strings.Join(e.Args, " "))
331+
}
332+
325333
// DefaultHelpFn returns a function that generates usage (help)
326334
// output for a given command.
327335
func DefaultHelpFn() HandlerFunc {
@@ -352,7 +360,7 @@ func DefaultHelpFn() HandlerFunc {
352360
if len(inv.Args) > 0 {
353361
// Return an error so that exit status is non-zero when
354362
// a subcommand is not found.
355-
return fmt.Errorf("error: unknown subcommand %q", strings.Join(inv.Args, " "))
363+
return &UnknownSubcommandError{Args: inv.Args}
356364
}
357365
return nil
358366
}

0 commit comments

Comments
 (0)