Skip to content

Commit

Permalink
cmd/root: Use string template instead of function for usage
Browse files Browse the repository at this point in the history
In version 1.1.2 of Cobra has been included a change[0] that changes the
behaviour of how custom usage functions are printed. The change prepends
"Error: " to the text. This is not desired since the usage text is not
an error text.

A workaround is to define a template string for the usage instead. The
template uses the templating language of Go[1]. See the default template
string in version 1.2.1[2].

This change has the positive benefit of getting more helpful usage
messages. E.g., for command 'toolbox enter' the usage text is no longer
"Run 'toolbox --help' for usage." but "Run 'toolbox enter --help' for usage.".

Upstream issue: spf13/cobra#1532

[0] spf13/cobra#1044
[1] https://pkg.go.dev/text/template
[2] https://github.com/spf13/cobra/blob/v1.2.1/command.go#L491
  • Loading branch information
HarryMichal committed Nov 11, 2021
1 parent 1b26f97 commit 6af7c77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
12 changes: 3 additions & 9 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func init() {
persistentFlags.CountVarP(&rootFlags.verbose, "verbose", "v", "Set log-level to 'debug'")

rootCmd.SetHelpFunc(rootHelp)
rootCmd.SetUsageFunc(rootUsage)

var usageTemplate = `Run '{{.CommandPath}} --help' for usage.`
rootCmd.SetUsageTemplate(usageTemplate)
}

func preRun(cmd *cobra.Command, args []string) error {
cmd.Root().SilenceUsage = true

if err := setUpLoggers(); err != nil {
return err
}
Expand Down Expand Up @@ -244,12 +244,6 @@ func rootRun(cmd *cobra.Command, args []string) error {
return nil
}

func rootUsage(cmd *cobra.Command) error {
err := fmt.Errorf("Run '%s --help' for usage.", executableBase)
fmt.Fprintf(os.Stderr, "%s", err)
return err
}

func migrate() error {
logrus.Debug("Migrating to newer Podman")

Expand Down
16 changes: 8 additions & 8 deletions test/system/002-help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -63,61 +63,61 @@ setup() {

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox create --help' for usage."
}

@test "help: Try to run 'toolbox enter' with non-existent flag (shows usage screen)" {
run $TOOLBOX enter --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox enter --help' for usage."
}

@test "help: Try to run 'toolbox help' with non-existent flag (shows usage screen)" {
run $TOOLBOX help --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox help --help' for usage."
}

@test "help: Try to run 'toolbox init-container' with non-existent flag (shows usage screen)" {
run $TOOLBOX init-container --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox init-container --help' for usage."
}

@test "help: Try to run 'toolbox list' with non-existent flag (shows usage screen)" {
run $TOOLBOX list --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox list --help' for usage."
}

@test "help: Try to run 'toolbox rm' with non-existent flag (shows usage screen)" {
run $TOOLBOX rm --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox rm --help' for usage."
}

@test "help: Try to run 'toolbox rmi' with non-existent flag (shows usage screen)" {
run $TOOLBOX rmi --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox rmi --help' for usage."
}

@test "help: Try to run 'toolbox run' with non-existent flag (shows usage screen)" {
run $TOOLBOX run --foo

assert_failure
assert_line --index 0 "Error: unknown flag: --foo"
assert_line --index 1 "Run 'toolbox --help' for usage."
assert_line --index 1 "Run 'toolbox run --help' for usage."
}

0 comments on commit 6af7c77

Please sign in to comment.