Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement short -v version flag (issue #654) #677

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
`
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
53 changes: 51 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`)
Expand All @@ -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})
Expand All @@ -886,20 +908,47 @@ 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")
}
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"}
Expand Down