Printing out version numbers

This commit is contained in:
Jeremy Saenz 2013-07-20 08:44:09 -07:00
parent 04490dabec
commit d8cf49f0dd
2 changed files with 22 additions and 6 deletions

15
app.go
View File

@ -28,14 +28,23 @@ func NewApp() *App {
} }
func (a *App) Run(arguments []string) { func (a *App) Run(arguments []string) {
// append help to commands
a.Commands = append(a.Commands, helpCommand)
// append version to flags
a.Flags = append(a.Flags, BoolFlag{"version", "print the version"})
// parse flags // parse flags
set := flagSet(a.Name, a.Flags) set := flagSet(a.Name, a.Flags)
set.Parse(arguments[1:]) set.Parse(arguments[1:])
// append help to commands
a.Commands = append(a.Commands, helpCommand)
context := NewContext(a, set, set) context := NewContext(a, set, set)
// check version
if context.GlobalBool("version") {
showVersion(context)
return
}
args := context.Args() args := context.Args()
if len(args) > 0 { if len(args) > 0 {
name := args[0] name := args[0]

13
help.go
View File

@ -1,8 +1,11 @@
package cli package cli
import "os" import (
import "text/tabwriter" "fmt"
import "text/template" "os"
"text/tabwriter"
"text/template"
)
var helpCommand = Command{ var helpCommand = Command{
Name: "help", Name: "help",
@ -32,3 +35,7 @@ GLOBAL OPTIONS:
w.Flush() w.Flush()
}, },
} }
func showVersion(c *Context) {
fmt.Printf("%v version %v\n", c.App.Name, c.App.Version)
}