Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor annoyance of missing '--version' flag from some Help output #1707

Merged
merged 5 commits into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -37,3 +37,5 @@ bin

.idea/
*.iml

.vscode
fnickels marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion command.go
Expand Up @@ -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())
}
},
Expand Down
87 changes: 87 additions & 0 deletions command_test.go
Expand Up @@ -2161,3 +2161,90 @@ func TestSetContextPersistentPreRun(t *testing.T) {
t.Error(err)
}
}

const VERSION_FLAG = "--version"
const HELP_FLAG = "--help"
fnickels marked this conversation as resolved.
Show resolved Hide resolved

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)
}