Add '--version' flag to Help output (#1707)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Francis Nickels III · committed by GitHub · parent fce8d8a · Browse files · 2 files·+89 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+89 added−1 removed
- other:+89 added−1 removed
Provenance
- Commit
7039e1f- Tree
7b449b5- Author
Francis Nickels III <fnickels@riotgames.com>- Committer
GitHub <noreply@github.com>- Parent
fce8d8a
2 files changed+89 added−1 removed
modified:command_test.go+87 added
2341 unmodified lines2342 if err != nil {2343 t.Error(err)2344 }2345 }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+}modified:command.go+2 added-1 removed
1121 unmodified lines1122 if cmd == nil || e != nil {1123 c.Printf("Unknown help topic %#q\n", args)1124 CheckErr(c.Root().Usage())1125 } else {1126- cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown1126+ cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown1127+ cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown1128 CheckErr(cmd.Help())1129 }1130 },1131 }580 unmodified lines