From 48197dead4acc1bcffd623b2d02c9a3d88112a9a Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 23 May 2022 16:46:34 -0700 Subject: [PATCH 1/5] add --version to the list of flags returned --- command.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index 0f4511f38..48c4f2e6b 100644 --- a/command.go +++ b/command.go @@ -1120,7 +1120,8 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`, c.Printf("Unknown help topic %#q\n", args) CheckErr(c.Root().Usage()) } else { - cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown + cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown + cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown CheckErr(cmd.Help()) } }, From 2d6d8c7aecf7b93a8b6bea9a0872064de916ffad Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 23 May 2022 17:22:58 -0700 Subject: [PATCH 2/5] add vscode to gitignore --- .gitignore | 2 ++ FJNnotes.md | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 FJNnotes.md diff --git a/.gitignore b/.gitignore index c7b459e4d..ed1f4f595 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ bin .idea/ *.iml + +.vscode \ No newline at end of file diff --git a/FJNnotes.md b/FJNnotes.md new file mode 100644 index 000000000..22cccc9c6 --- /dev/null +++ b/FJNnotes.md @@ -0,0 +1,3 @@ +Things to fix: +* default --version flag is not shown when running `tool help` but is shown when running `tool` or `tool --help` by itself +* `tool --help command` shows general help instead of `tool command --help` results From 6495e274444691f8848eb499e6bcaa1e6b7601c7 Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 23 May 2022 18:53:15 -0700 Subject: [PATCH 3/5] add test cases --- FJNnotes.md | 3 -- command_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) delete mode 100644 FJNnotes.md diff --git a/FJNnotes.md b/FJNnotes.md deleted file mode 100644 index 22cccc9c6..000000000 --- a/FJNnotes.md +++ /dev/null @@ -1,3 +0,0 @@ -Things to fix: -* default --version flag is not shown when running `tool help` but is shown when running `tool` or `tool --help` by itself -* `tool --help command` shows general help instead of `tool command --help` results diff --git a/command_test.go b/command_test.go index d48fef1a0..d8ff19ffc 100644 --- a/command_test.go +++ b/command_test.go @@ -2161,3 +2161,90 @@ func TestSetContextPersistentPreRun(t *testing.T) { t.Error(err) } } + +const VERSION_FLAG = "--version" +const HELP_FLAG = "--help" + +func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description"} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +} + +func TestHelpCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, "help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, "help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +} + +func TestHelpflagCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, HELP_FLAG) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, HELP_FLAG) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +} From 03103735ca01a7b3e625146e99e068ecc4515153 Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 19 Sep 2022 08:02:59 -0700 Subject: [PATCH 4/5] revert gitignore change --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index ed1f4f595..c7b459e4d 100644 --- a/.gitignore +++ b/.gitignore @@ -37,5 +37,3 @@ bin .idea/ *.iml - -.vscode \ No newline at end of file From 44455258be2eb95e93f3f00acc119752c541fb71 Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 19 Sep 2022 10:44:47 -0700 Subject: [PATCH 5/5] Set Camel Case on const --- command_test.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/command_test.go b/command_test.go index d8ff19ffc..1c0bfca5d 100644 --- a/command_test.go +++ b/command_test.go @@ -2162,8 +2162,8 @@ func TestSetContextPersistentPreRun(t *testing.T) { } } -const VERSION_FLAG = "--version" -const HELP_FLAG = "--help" +const VersionFlag = "--version" +const HelpFlag = "--help" func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) { rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"} @@ -2175,8 +2175,8 @@ func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) { } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringContains(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringContains(t, output, VersionFlag) } func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) { @@ -2189,8 +2189,8 @@ func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) { } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringOmits(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringOmits(t, output, VersionFlag) } func TestHelpCommandExecutedWithVersionSet(t *testing.T) { @@ -2203,8 +2203,8 @@ func TestHelpCommandExecutedWithVersionSet(t *testing.T) { } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringContains(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringContains(t, output, VersionFlag) } func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) { @@ -2217,34 +2217,34 @@ func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) { } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringOmits(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringOmits(t, output, VersionFlag) } func TestHelpflagCommandExecutedWithVersionSet(t *testing.T) { rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun} rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) - output, err := executeCommand(rootCmd, HELP_FLAG) + output, err := executeCommand(rootCmd, HelpFlag) if err != nil { t.Errorf("Unexpected error: %v", err) } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringContains(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringContains(t, output, VersionFlag) } func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) { rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) - output, err := executeCommand(rootCmd, HELP_FLAG) + output, err := executeCommand(rootCmd, HelpFlag) if err != nil { t.Errorf("Unexpected error: %v", err) } checkStringContains(t, output, rootCmd.Long) - checkStringContains(t, output, HELP_FLAG) - checkStringOmits(t, output, VERSION_FLAG) + checkStringContains(t, output, HelpFlag) + checkStringOmits(t, output, VersionFlag) }