From 6af7c77be8d4e6a1856cb46dc2e819987a6d6953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Thu, 11 Nov 2021 18:18:52 +0200 Subject: [PATCH] cmd/root: Use string template instead of function for usage 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: https://github.com/spf13/cobra/issues/1532 [0] https://github.com/spf13/cobra/pull/1044 [1] https://pkg.go.dev/text/template [2] https://github.com/spf13/cobra/blob/v1.2.1/command.go#L491 --- src/cmd/root.go | 12 +++--------- test/system/002-help.bats | 16 ++++++++-------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/cmd/root.go b/src/cmd/root.go index 9c6e6151e..fc213b19e 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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 } @@ -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") diff --git a/test/system/002-help.bats b/test/system/002-help.bats index 7483bd1e0..8364198ab 100644 --- a/test/system/002-help.bats +++ b/test/system/002-help.bats @@ -63,7 +63,7 @@ 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)" { @@ -71,7 +71,7 @@ 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 enter --help' for usage." } @test "help: Try to run 'toolbox help' with non-existent flag (shows usage screen)" { @@ -79,7 +79,7 @@ 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 help --help' for usage." } @test "help: Try to run 'toolbox init-container' with non-existent flag (shows usage screen)" { @@ -87,7 +87,7 @@ 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 init-container --help' for usage." } @test "help: Try to run 'toolbox list' with non-existent flag (shows usage screen)" { @@ -95,7 +95,7 @@ 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 list --help' for usage." } @test "help: Try to run 'toolbox rm' with non-existent flag (shows usage screen)" { @@ -103,7 +103,7 @@ 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 rm --help' for usage." } @test "help: Try to run 'toolbox rmi' with non-existent flag (shows usage screen)" { @@ -111,7 +111,7 @@ 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 rmi --help' for usage." } @test "help: Try to run 'toolbox run' with non-existent flag (shows usage screen)" { @@ -119,5 +119,5 @@ 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 run --help' for usage." }