Skip to content
Myr

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

2 files+89 added−1 removed

modified:command_test.go+87 added
2341 unmodified lines
2342 if err != nil {2343 t.Error(err)2344 }2345}23462347const VersionFlag = "--version"2348const HelpFlag = "--help"23492350func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {2351 rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}2352 rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})23532354 output, err := executeCommand(rootCmd)2355 if err != nil {2356 t.Errorf("Unexpected error: %v", err)2357 }23582359 checkStringContains(t, output, rootCmd.Long)2360 checkStringContains(t, output, HelpFlag)2361 checkStringContains(t, output, VersionFlag)2362}23632364func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) {2365 rootCmd := &Command{Use: "root", Long: "Long description"}2366 rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})23672368 output, err := executeCommand(rootCmd)2369 if err != nil {2370 t.Errorf("Unexpected error: %v", err)2371 }23722373 checkStringContains(t, output, rootCmd.Long)2374 checkStringContains(t, output, HelpFlag)2375 checkStringOmits(t, output, VersionFlag)2376}23772378func 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})23812382 output, err := executeCommand(rootCmd, "help")2383 if err != nil {2384 t.Errorf("Unexpected error: %v", err)2385 }23862387 checkStringContains(t, output, rootCmd.Long)2388 checkStringContains(t, output, HelpFlag)2389 checkStringContains(t, output, VersionFlag)2390}23912392func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) {2393 rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}2394 rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})23952396 output, err := executeCommand(rootCmd, "help")2397 if err != nil {2398 t.Errorf("Unexpected error: %v", err)2399 }24002401 checkStringContains(t, output, rootCmd.Long)2402 checkStringContains(t, output, HelpFlag)2403 checkStringOmits(t, output, VersionFlag)2404}24052406func 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})24092410 output, err := executeCommand(rootCmd, HelpFlag)2411 if err != nil {2412 t.Errorf("Unexpected error: %v", err)2413 }24142415 checkStringContains(t, output, rootCmd.Long)2416 checkStringContains(t, output, HelpFlag)2417 checkStringContains(t, output, VersionFlag)2418}24192420func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) {2421 rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}2422 rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})24232424 output, err := executeCommand(rootCmd, HelpFlag)2425 if err != nil {2426 t.Errorf("Unexpected error: %v", err)2427 }24282429 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 lines
1122 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