Skip to content

Commit

Permalink
Add '--version' flag to Help output (#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnickels authored Sep 30, 2022
1 parent fce8d8a commit 7039e1f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,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
Original file line number Diff line number Diff line change
Expand Up @@ -2343,3 +2343,90 @@ func TestSetContextPersistentPreRun(t *testing.T) {
t.Error(err)
}
}

const VersionFlag = "--version"
const HelpFlag = "--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, HelpFlag)
checkStringContains(t, output, VersionFlag)
}

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, HelpFlag)
checkStringOmits(t, output, VersionFlag)
}

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, HelpFlag)
checkStringContains(t, output, VersionFlag)
}

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, 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, HelpFlag)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, rootCmd.Long)
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, HelpFlag)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, rootCmd.Long)
checkStringContains(t, output, HelpFlag)
checkStringOmits(t, output, VersionFlag)
}

0 comments on commit 7039e1f

Please sign in to comment.