Skip to content

Commit 7039e1f

Browse files
authored
Add '--version' flag to Help output (#1707)
1 parent fce8d8a commit 7039e1f

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

command.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
11231123
c.Printf("Unknown help topic %#q\n", args)
11241124
CheckErr(c.Root().Usage())
11251125
} else {
1126-
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
1126+
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
1127+
cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown
11271128
CheckErr(cmd.Help())
11281129
}
11291130
},

command_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -2343,3 +2343,90 @@ func TestSetContextPersistentPreRun(t *testing.T) {
23432343
t.Error(err)
23442344
}
23452345
}
2346+
2347+
const VersionFlag = "--version"
2348+
const HelpFlag = "--help"
2349+
2350+
func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {
2351+
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}
2352+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2353+
2354+
output, err := executeCommand(rootCmd)
2355+
if err != nil {
2356+
t.Errorf("Unexpected error: %v", err)
2357+
}
2358+
2359+
checkStringContains(t, output, rootCmd.Long)
2360+
checkStringContains(t, output, HelpFlag)
2361+
checkStringContains(t, output, VersionFlag)
2362+
}
2363+
2364+
func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) {
2365+
rootCmd := &Command{Use: "root", Long: "Long description"}
2366+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2367+
2368+
output, err := executeCommand(rootCmd)
2369+
if err != nil {
2370+
t.Errorf("Unexpected error: %v", err)
2371+
}
2372+
2373+
checkStringContains(t, output, rootCmd.Long)
2374+
checkStringContains(t, output, HelpFlag)
2375+
checkStringOmits(t, output, VersionFlag)
2376+
}
2377+
2378+
func TestHelpCommandExecutedWithVersionSet(t *testing.T) {
2379+
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun}
2380+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2381+
2382+
output, err := executeCommand(rootCmd, "help")
2383+
if err != nil {
2384+
t.Errorf("Unexpected error: %v", err)
2385+
}
2386+
2387+
checkStringContains(t, output, rootCmd.Long)
2388+
checkStringContains(t, output, HelpFlag)
2389+
checkStringContains(t, output, VersionFlag)
2390+
}
2391+
2392+
func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) {
2393+
rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}
2394+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2395+
2396+
output, err := executeCommand(rootCmd, "help")
2397+
if err != nil {
2398+
t.Errorf("Unexpected error: %v", err)
2399+
}
2400+
2401+
checkStringContains(t, output, rootCmd.Long)
2402+
checkStringContains(t, output, HelpFlag)
2403+
checkStringOmits(t, output, VersionFlag)
2404+
}
2405+
2406+
func TestHelpflagCommandExecutedWithVersionSet(t *testing.T) {
2407+
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun}
2408+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2409+
2410+
output, err := executeCommand(rootCmd, HelpFlag)
2411+
if err != nil {
2412+
t.Errorf("Unexpected error: %v", err)
2413+
}
2414+
2415+
checkStringContains(t, output, rootCmd.Long)
2416+
checkStringContains(t, output, HelpFlag)
2417+
checkStringContains(t, output, VersionFlag)
2418+
}
2419+
2420+
func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) {
2421+
rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}
2422+
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
2423+
2424+
output, err := executeCommand(rootCmd, HelpFlag)
2425+
if err != nil {
2426+
t.Errorf("Unexpected error: %v", err)
2427+
}
2428+
2429+
checkStringContains(t, output, rootCmd.Long)
2430+
checkStringContains(t, output, HelpFlag)
2431+
checkStringOmits(t, output, VersionFlag)
2432+
}

0 commit comments

Comments
 (0)