Add short version flag -v when not otherwise set (#996)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Dave Henderson · committed by GitHub · parent 39cf99f · Browse files · 2 files·+122 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diff stats
2 files+122 added−2 removed
- other:+122 added−2 removed
Provenance
- Commit
95f2f73- Tree
bc435d5- Author
Dave Henderson <dhenderson@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
39cf99f
2 files changed+122 added−2 removed
modified:command_test.go+115 added
919 unmodified lines920 921 checkStringContains(t, output, "root version 1.0.0")922 }923 924+func TestVersionFlagExecutedWithNoName(t *testing.T) {925+ rootCmd := &Command{Version: "1.0.0", Run: emptyRun}926+927+ output, err := executeCommand(rootCmd, "--version", "arg1")928+ if err != nil {929+ t.Errorf("Unexpected error: %v", err)930+ }931+932+ checkStringContains(t, output, "version 1.0.0")933+}934+935+func TestShortAndLongVersionFlagInHelp(t *testing.T) {936+ rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}937+938+ output, err := executeCommand(rootCmd, "--help")939+ if err != nil {940+ t.Errorf("Unexpected error: %v", err)941+ }942+943+ checkStringContains(t, output, "-v, --version")944+}945+946+func TestLongVersionFlagOnlyInHelpWhenShortPredefined(t *testing.T) {947+ rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}948+ rootCmd.Flags().StringP("foo", "v", "", "not a version flag")949+950+ output, err := executeCommand(rootCmd, "--help")951+ if err != nil {952+ t.Errorf("Unexpected error: %v", err)953+ }954+955+ checkStringOmits(t, output, "-v, --version")956+ checkStringContains(t, output, "--version")957+}958+959+func TestShorthandVersionFlagExecuted(t *testing.T) {960+ rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}961+962+ output, err := executeCommand(rootCmd, "-v", "arg1")963+ if err != nil {964+ t.Errorf("Unexpected error: %v", err)965+ }966+967+ checkStringContains(t, output, "root version 1.0.0")968+}969+970 func TestVersionTemplate(t *testing.T) {971 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}972 rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)973 4 unmodified lines978 979 checkStringContains(t, output, "customized version: 1.0.0")980 }981 982+func TestShorthandVersionTemplate(t *testing.T) {983+ rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}984+ rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)985+986+ output, err := executeCommand(rootCmd, "-v", "arg1")987+ if err != nil {988+ t.Errorf("Unexpected error: %v", err)989+ }990+991+ checkStringContains(t, output, "customized version: 1.0.0")992+}993+994 func TestVersionFlagExecutedOnSubcommand(t *testing.T) {995 rootCmd := &Command{Use: "root", Version: "1.0.0"}996 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})997 4 unmodified lines1002 1003 checkStringContains(t, output, "root version 1.0.0")1004 }1005 1006+func TestShorthandVersionFlagExecutedOnSubcommand(t *testing.T) {1007+ rootCmd := &Command{Use: "root", Version: "1.0.0"}1008+ rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})1009+1010+ output, err := executeCommand(rootCmd, "-v", "sub")1011+ if err != nil {1012+ t.Errorf("Unexpected error: %v", err)1013+ }1014+1015+ checkStringContains(t, output, "root version 1.0.0")1016+}1017+1018 func TestVersionFlagOnlyAddedToRoot(t *testing.T) {1019 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}1020 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})1021 4 unmodified lines1026 1027 checkStringContains(t, err.Error(), "unknown flag: --version")1028 }1029 1030+func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) {1031+ rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}1032+ rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})1033+1034+ _, err := executeCommand(rootCmd, "sub", "-v")1035+ if err == nil {1036+ t.Errorf("Expected error")1037+ }1038+1039+ checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")1040+}1041+1042 func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {1043 rootCmd := &Command{Use: "root", Run: emptyRun}1044 1045 _, err := executeCommand(rootCmd, "--version")2 unmodified lines1048 }1049 checkStringContains(t, err.Error(), "unknown flag: --version")1050 }1051 1052+func TestShorthandVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {1053+ rootCmd := &Command{Use: "root", Run: emptyRun}1054+1055+ _, err := executeCommand(rootCmd, "-v")1056+ if err == nil {1057+ t.Errorf("Expected error")1058+ }1059+ checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")1060+}1061+1062+func TestShorthandVersionFlagOnlyAddedIfShorthandNotDefined(t *testing.T) {1063+ rootCmd := &Command{Use: "root", Run: emptyRun, Version: "1.2.3"}1064+ rootCmd.Flags().StringP("notversion", "v", "", "not a version flag")1065+1066+ _, err := executeCommand(rootCmd, "-v")1067+ if err == nil {1068+ t.Errorf("Expected error")1069+ }1070+ check(t, rootCmd.Flags().ShorthandLookup("v").Name, "notversion")1071+ checkStringContains(t, err.Error(), "flag needs an argument: 'v' in -v")1072+}1073+1074+func TestShorthandVersionFlagOnlyAddedIfVersionNotDefined(t *testing.T) {1075+ rootCmd := &Command{Use: "root", Run: emptyRun, Version: "1.2.3"}1076+ rootCmd.Flags().Bool("version", false, "a different kind of version flag")1077+1078+ _, err := executeCommand(rootCmd, "-v")1079+ if err == nil {1080+ t.Errorf("Expected error")1081+ }1082+ checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")1083+}1084+1085 func TestUsageIsNotPrintedTwice(t *testing.T) {1086 var cmd = &Command{Use: "root"}1087 var sub = &Command{Use: "sub"}1088 cmd.AddCommand(sub)869 unmodified linesmodified:command.go+7 added-2 removed
82 unmodified lines83 Annotations map[string]string84 85 // Version defines the version for this command. If this value is non-empty and the command does not86 // define a "version" flag, a "version" boolean flag will be added to the command and, if specified,87- // will print content of the "Version" variable.87+ // will print content of the "Version" variable. A shorthand "v" flag will also be added if the88+ // command does not define one.89 Version string90 91 // The *Run functions are executed in the following order:92 // * PersistentPreRun()940 unmodified lines1033 usage += "this command"1034 } else {1035 usage += c.Name()1036 }1037+ if c.Flags().ShorthandLookup("v") == nil {1038+ c.Flags().BoolP("version", "v", false, usage)1039+ } else {1036- c.Flags().Bool("version", false, usage)1040+ c.Flags().Bool("version", false, usage)1041+ }1042 }1043 }1044 1045 // InitDefaultHelpCmd adds default help command to c.593 unmodified lines