diff --git a/command.go b/command.go index c42a169..ad6a47c 100644 --- a/command.go +++ b/command.go @@ -9,6 +9,7 @@ import ( type Command struct { Name string + version string ShortDescription string LongDescription string Aliases []string @@ -20,8 +21,8 @@ type Command struct { isRoot bool } -func NewRoot(name string, options ...Option) *Command { - cmd := &Command{Name: name, isRoot: true} +func NewRoot(name string, version string, options ...Option) *Command { + cmd := &Command{Name: name, version: version, isRoot: true} cmd.ApplyOptions(options...) return cmd } @@ -91,6 +92,12 @@ func (c *Command) Execute(args []string) { os.Exit(1) } + versionOpt, ok := opt.Globals().GetBool("version") + if ok && versionOpt.Value() { + c.ShowVersion() + return + } + helpOpt, ok := opt.Globals().GetBool("help") if ok && helpOpt.Value() { c.ShowHelp() @@ -104,3 +111,8 @@ func (c *Command) Execute(args []string) { c.ShowHelp() } + +func (c *Command) ShowVersion() { + rootName := c.Root().Name + fmt.Printf("%s %s\n", rootName, c.version) +}