From d8cf49f0dd59c09024be0020b8c183d4cf94033e Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Sat, 20 Jul 2013 08:44:09 -0700 Subject: [PATCH] Printing out version numbers --- app.go | 15 ++++++++++++--- help.go | 13 ++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index 659ef47..e203930 100644 --- a/app.go +++ b/app.go @@ -28,14 +28,23 @@ func NewApp() *App { } 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 set := flagSet(a.Name, a.Flags) set.Parse(arguments[1:]) - // append help to commands - a.Commands = append(a.Commands, helpCommand) - context := NewContext(a, set, set) + + // check version + if context.GlobalBool("version") { + showVersion(context) + return + } + args := context.Args() if len(args) > 0 { name := args[0] diff --git a/help.go b/help.go index 39f7d7f..89f8e04 100644 --- a/help.go +++ b/help.go @@ -1,8 +1,11 @@ package cli -import "os" -import "text/tabwriter" -import "text/template" +import ( + "fmt" + "os" + "text/tabwriter" + "text/template" +) var helpCommand = Command{ Name: "help", @@ -32,3 +35,7 @@ GLOBAL OPTIONS: w.Flush() }, } + +func showVersion(c *Context) { + fmt.Printf("%v version %v\n", c.App.Name, c.App.Version) +}