Skip to content
Myr

Ensure that '--version' flag works properly for root command (#595)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Make it so that, in the case that the root command is not runnable but has subcommands, specifying a '--version' flag will still run the "version" behavior.

authored by Nick Miyake · committed by Albert Nigmatzianov · parent c156af3 · Browse files · 2 files+6 added−2 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+6 added−2 removed

  • other:+6 added−2 removed

2 files+6 added−2 removed

modified:command_test.go+1 added-1 removed
865 unmodified lines
866 checkStringContains(t, output, "customized version: 1.0.0")867}868869func TestVersionFlagExecutedOnSubcommand(t *testing.T) {870 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}870 rootCmd := &Command{Use: "root", Version: "1.0.0"}871 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})872873 output, err := executeCommand(rootCmd, "--version", "sub")874 if err != nil {
691 unmodified lines
modified:command.go+5 added-1 removed
683 unmodified lines
684 c.Println("\"help\" flag declared as non-bool. Please correct your code")685 return err686 }687688 if helpVal || !c.Runnable() {688 if helpVal {689 return flag.ErrHelp690 }691692 // for back-compat, only add version flag behavior if version is defined
11 unmodified lines
704 return err705 }706 }707708 if !c.Runnable() {709 return flag.ErrHelp710 }711712 c.preRun()713714 argWoFlags := c.Flags().Args()715 if c.DisableFlagParsing {
766 unmodified lines