Skip to content
Myr

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

2 files+122 added−2 removed

modified:command_test.go+115 added
919 unmodified lines
920921 checkStringContains(t, output, "root version 1.0.0")922}923924func TestVersionFlagExecutedWithNoName(t *testing.T) {925 rootCmd := &Command{Version: "1.0.0", Run: emptyRun}926927 output, err := executeCommand(rootCmd, "--version", "arg1")928 if err != nil {929 t.Errorf("Unexpected error: %v", err)930 }931932 checkStringContains(t, output, "version 1.0.0")933}934935func TestShortAndLongVersionFlagInHelp(t *testing.T) {936 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}937938 output, err := executeCommand(rootCmd, "--help")939 if err != nil {940 t.Errorf("Unexpected error: %v", err)941 }942943 checkStringContains(t, output, "-v, --version")944}945946func 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")949950 output, err := executeCommand(rootCmd, "--help")951 if err != nil {952 t.Errorf("Unexpected error: %v", err)953 }954955 checkStringOmits(t, output, "-v, --version")956 checkStringContains(t, output, "--version")957}958959func TestShorthandVersionFlagExecuted(t *testing.T) {960 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}961962 output, err := executeCommand(rootCmd, "-v", "arg1")963 if err != nil {964 t.Errorf("Unexpected error: %v", err)965 }966967 checkStringContains(t, output, "root version 1.0.0")968}969970func TestVersionTemplate(t *testing.T) {971 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}972 rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)973
4 unmodified lines
978979 checkStringContains(t, output, "customized version: 1.0.0")980}981982func TestShorthandVersionTemplate(t *testing.T) {983 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}984 rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)985986 output, err := executeCommand(rootCmd, "-v", "arg1")987 if err != nil {988 t.Errorf("Unexpected error: %v", err)989 }990991 checkStringContains(t, output, "customized version: 1.0.0")992}993994func TestVersionFlagExecutedOnSubcommand(t *testing.T) {995 rootCmd := &Command{Use: "root", Version: "1.0.0"}996 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})997
4 unmodified lines
10021003 checkStringContains(t, output, "root version 1.0.0")1004}10051006func TestShorthandVersionFlagExecutedOnSubcommand(t *testing.T) {1007 rootCmd := &Command{Use: "root", Version: "1.0.0"}1008 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})10091010 output, err := executeCommand(rootCmd, "-v", "sub")1011 if err != nil {1012 t.Errorf("Unexpected error: %v", err)1013 }10141015 checkStringContains(t, output, "root version 1.0.0")1016}10171018func 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 lines
10261027 checkStringContains(t, err.Error(), "unknown flag: --version")1028}10291030func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) {1031 rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}1032 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})10331034 _, err := executeCommand(rootCmd, "sub", "-v")1035 if err == nil {1036 t.Errorf("Expected error")1037 }10381039 checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")1040}10411042func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {1043 rootCmd := &Command{Use: "root", Run: emptyRun}10441045 _, err := executeCommand(rootCmd, "--version")
2 unmodified lines
1048 }1049 checkStringContains(t, err.Error(), "unknown flag: --version")1050}10511052func TestShorthandVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {1053 rootCmd := &Command{Use: "root", Run: emptyRun}10541055 _, 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}10611062func 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")10651066 _, 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}10731074func 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")10771078 _, 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}10841085func TestUsageIsNotPrintedTwice(t *testing.T) {1086 var cmd = &Command{Use: "root"}1087 var sub = &Command{Use: "sub"}1088 cmd.AddCommand(sub)
869 unmodified lines
modified:command.go+7 added-2 removed
82 unmodified lines
83 Annotations map[string]string8485 // 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 string9091 // The *Run functions are executed in the following order:92 // * PersistentPreRun()
940 unmodified lines
1033 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}10441045// InitDefaultHelpCmd adds default help command to c.
593 unmodified lines