From 70eacca641ef7a004a56c13e537489a79f83650a Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Wed, 24 Jul 2013 07:35:45 -0700 Subject: [PATCH] JMS #25: Added support for help flags --- app.go | 19 +++++++++++-------- context.go | 6 +++--- context_test.go | 2 +- flag.go | 15 ++++++++++++++- help.go | 34 +++++++++++++++++++++++++--------- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/app.go b/app.go index 180e364..50a13e9 100644 --- a/app.go +++ b/app.go @@ -32,19 +32,22 @@ 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"}) + a.Flags = append( + a.Flags, + BoolFlag{"version", "print the version"}, + helpFlag{"show help"}, + ) // parse flags set := flagSet(a.Name, a.Flags) - set.Parse(arguments[1:]) + err := set.Parse(arguments[1:]) + if err != nil { + os.Exit(1) + } context := NewContext(a, set, set) - - // check version - if context.GlobalBool("version") { - showVersion(context) - return - } + checkHelp(context) + checkVersion(context) args := context.Args() if len(args) > 0 { diff --git a/context.go b/context.go index 4207204..6e3867a 100644 --- a/context.go +++ b/context.go @@ -62,7 +62,7 @@ func (c *Context) lookupInt(name string, set *flag.FlagSet) int { } return val } - + return 0 } @@ -71,7 +71,7 @@ func (c *Context) lookupString(name string, set *flag.FlagSet) string { if f != nil { return f.Value.String() } - + return "" } @@ -84,6 +84,6 @@ func (c *Context) lookupBool(name string, set *flag.FlagSet) bool { } return val } - + return false } diff --git a/context_test.go b/context_test.go index 6e342fc..cb3a551 100644 --- a/context_test.go +++ b/context_test.go @@ -2,8 +2,8 @@ package cli_test import ( "flag" - "testing" "github.com/codegangsta/cli" + "testing" ) func TestNewContext(t *testing.T) { diff --git a/flag.go b/flag.go index d6ee6e6..25fe778 100644 --- a/flag.go +++ b/flag.go @@ -9,7 +9,7 @@ type Flag interface { } func flagSet(name string, flags []Flag) *flag.FlagSet { - set := flag.NewFlagSet(name, flag.ExitOnError) + set := flag.NewFlagSet(name, flag.ContinueOnError) for _, f := range flags { f.Apply(set) } @@ -56,3 +56,16 @@ func (f IntFlag) String() string { func (f IntFlag) Apply(set *flag.FlagSet) { set.Int(f.Name, f.Value, f.Usage) } + +type helpFlag struct { + Usage string +} + +func (f helpFlag) String() string { + return fmt.Sprintf("--help, -h\t%v", f.Usage) +} + +func (f helpFlag) Apply(set *flag.FlagSet) { + set.Bool("h", false, f.Usage) + set.Bool("help", false, f.Usage) +} diff --git a/help.go b/help.go index eef0d24..9605818 100644 --- a/help.go +++ b/help.go @@ -51,30 +51,36 @@ var helpCommand = Command{ Action: func(c *Context) { args := c.Args() if len(args) > 0 { - showCommandHelp(c) + ShowCommandHelp(c, args[0]) } else { - showAppHelp(c) + ShowAppHelp(c) } }, } -func showAppHelp(c *Context) { +// Prints help for the App +func ShowAppHelp(c *Context) { printHelp(AppHelpTemplate, c.App) } -func showCommandHelp(c *Context) { - name := c.Args()[0] +// Prints help for the given command +func ShowCommandHelp(c *Context, command string) { for _, c := range c.App.Commands { - if c.HasName(name) { + if c.HasName(command) { printHelp(CommandHelpTemplate, c) return } } - fmt.Printf("No help topic for '%v'\n", name) + fmt.Printf("No help topic for '%v'\n", command) os.Exit(1) } +// Prints the version number of the App +func ShowVersion(c *Context) { + fmt.Printf("%v version %v\n", c.App.Name, c.App.Version) +} + func printHelp(templ string, data interface{}) { w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) t := template.Must(template.New("help").Parse(templ)) @@ -82,6 +88,16 @@ func printHelp(templ string, data interface{}) { w.Flush() } -func showVersion(c *Context) { - fmt.Printf("%v version %v\n", c.App.Name, c.App.Version) +func checkVersion(c *Context) { + if c.GlobalBool("version") { + ShowVersion(c) + os.Exit(0) + } +} + +func checkHelp(c *Context) { + if c.GlobalBool("h") || c.GlobalBool("help") { + ShowAppHelp(c) + os.Exit(0) + } }