1
0
Fork 0
mirror of https://github.com/spf13/cobra synced 2025-07-25 04:02:22 +00:00

short -v version flag (issue )

This commit is contained in:
Andrea Manzini 2018-04-15 17:00:52 +02:00
commit ce329d1cf5
3 changed files with 53 additions and 4 deletions

View file

@ -51,7 +51,7 @@ var EnableCommandSorting = true
// if the CLI is started from explorer.exe.
// To disable the mousetrap, just set this variable to blank string ("").
// Works only on Microsoft Windows.
var MousetrapHelpText string = `This is a command line tool.
var MousetrapHelpText = `This is a command line tool.
You need to open cmd.exe and run it from there.
`

View file

@ -932,7 +932,7 @@ func (c *Command) InitDefaultVersionFlag() {
} else {
usage += c.Name()
}
c.Flags().Bool("version", false, usage)
c.Flags().BoolP("version", "v", false, usage)
}
}

View file

@ -854,6 +854,16 @@ func TestVersionFlagExecuted(t *testing.T) {
checkStringContains(t, output, "root version 1.0.0")
}
func TestShortVersionFlagExecuted(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
output, err := executeCommand(rootCmd, "-v", "arg1")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
checkStringContains(t, output, "root version 1.0.0")
}
func TestVersionTemplate(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)
@ -866,6 +876,18 @@ func TestVersionTemplate(t *testing.T) {
checkStringContains(t, output, "customized version: 1.0.0")
}
func TestShortVersionTemplate(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
rootCmd.SetVersionTemplate(`customized version: {{.Version}}`)
output, err := executeCommand(rootCmd, "-v", "arg1")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
checkStringContains(t, output, "customized version: 1.0.0")
}
func TestVersionFlagExecutedOnSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0"}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
@ -886,13 +908,21 @@ func TestVersionFlagOnlyAddedToRoot(t *testing.T) {
if err == nil {
t.Errorf("Expected error")
}
checkStringContains(t, err.Error(), "unknown flag: --version")
}
func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
_, err := executeCommand(rootCmd, "sub", "-v")
if err == nil {
t.Errorf("Expected error")
}
checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")
}
func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
_, err := executeCommand(rootCmd, "--version")
if err == nil {
t.Errorf("Expected error")
@ -900,6 +930,25 @@ func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {
checkStringContains(t, err.Error(), "unknown flag: --version")
}
func TestShortVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
_, err := executeCommand(rootCmd, "-v")
if err == nil {
t.Errorf("Expected error")
}
checkStringContains(t, err.Error(), "unknown shorthand flag: 'v' in -v")
}
func TestShortVersionAndLongVersion(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
output, err := executeCommand(rootCmd, "-v", "--version")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
checkStringContains(t, output, "root version 1.0.0")
}
func TestUsageIsNotPrintedTwice(t *testing.T) {
var cmd = &Command{Use: "root"}
var sub = &Command{Use: "sub"}