Skip to content

Commit

Permalink
Add support for --version flag (spf13#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmiyake authored and n10v committed Dec 1, 2017
1 parent 1be1d28 commit b1ec2ce
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
65 changes: 64 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ type Command struct {
// group commands.
Annotations map[string]string

// Version defines the version for this command. If this value is non-empty and the command does not
// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
// will print content of the "Version" variable.
Version string

// The *Run functions are executed in the following order:
// * PersistentPreRun()
// * PreRun()
Expand Down Expand Up @@ -177,6 +182,8 @@ type Command struct {
// helpCommand is command with usage 'help'. If it's not defined by user,
// cobra uses default help command.
helpCommand *Command
// versionTemplate is the version template defined by user.
versionTemplate string
}

// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
Expand Down Expand Up @@ -222,6 +229,11 @@ func (c *Command) SetHelpTemplate(s string) {
c.helpTemplate = s
}

// SetVersionTemplate sets version template to be used. Application can use it to set custom template.
func (c *Command) SetVersionTemplate(s string) {
c.versionTemplate = s
}

// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
// The user should not have a cyclic dependency on commands.
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
Expand Down Expand Up @@ -411,6 +423,19 @@ func (c *Command) HelpTemplate() string {
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
}

// VersionTemplate return version template for the command.
func (c *Command) VersionTemplate() string {
if c.versionTemplate != "" {
return c.versionTemplate
}

if c.HasParent() {
return c.parent.VersionTemplate()
}
return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
`
}

func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
flag := fs.Lookup(name)
if flag == nil {
Expand Down Expand Up @@ -640,9 +665,10 @@ func (c *Command) execute(a []string) (err error) {
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
}

// initialize help flag as the last point possible to allow for user
// initialize help and version flag at the last point possible to allow for user
// overriding
c.InitDefaultHelpFlag()
c.InitDefaultVersionFlag()

err = c.ParseFlags(a)
if err != nil {
Expand All @@ -663,6 +689,22 @@ func (c *Command) execute(a []string) (err error) {
return flag.ErrHelp
}

// for back-compat, only add version flag behavior if version is defined
if c.Version != "" {
versionVal, err := c.Flags().GetBool("version")
if err != nil {
c.Println("\"version\" flag declared as non-bool. Please correct your code")
return err
}
if versionVal {
err := tmpl(c.OutOrStdout(), c.VersionTemplate(), c)
if err != nil {
c.Println(err)
}
return err
}
}

c.preRun()

argWoFlags := c.Flags().Args()
Expand Down Expand Up @@ -848,6 +890,27 @@ func (c *Command) InitDefaultHelpFlag() {
}
}

// InitDefaultVersionFlag adds default version flag to c.
// It is called automatically by executing the c.
// If c already has a version flag, it will do nothing.
// If c.Version is empty, it will do nothing.
func (c *Command) InitDefaultVersionFlag() {
if c.Version == "" {
return
}

c.mergePersistentFlags()
if c.Flags().Lookup("version") == nil {
usage := "version for "
if c.Name() == "" {
usage += "this command"
} else {
usage += c.Name()
}
c.Flags().Bool("version", false, usage)
}
}

// InitDefaultHelpCmd adds default help command to c.
// It is called automatically by executing the c or by calling help and usage.
// If c already has help command or c has no subcommands, it will do nothing.
Expand Down
57 changes: 57 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,63 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
checkStringContains(t, output, childCmd.Long)
}

func TestVersionFlagExecuted(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}

output, err := executeCommand(rootCmd, "--version", "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}}`)

output, err := executeCommand(rootCmd, "--version", "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", Run: emptyRun}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})

output, err := executeCommand(rootCmd, "--version", "sub")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, "root version 1.0.0")
}

func TestVersionFlagOnlyAddedToRoot(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})

_, err := executeCommand(rootCmd, "sub", "--version")
if err == nil {
t.Errorf("Expected error")
}

checkStringContains(t, err.Error(), "unknown flag: --version")
}

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 TestUsageIsNotPrintedTwice(t *testing.T) {
var cmd = &Command{Use: "root"}
var sub = &Command{Use: "sub"}
Expand Down

0 comments on commit b1ec2ce

Please sign in to comment.