Merge pull request #26 from codegangsta/JMS-25

JMS #25: Added support for help flags
main
Jeremy Saenz 11 years ago
commit 61d5778536

@ -32,19 +32,22 @@ func (a *App) Run(arguments []string) {
// append help to commands // append help to commands
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
// append version to flags // 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 // parse flags
set := flagSet(a.Name, a.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) context := NewContext(a, set, set)
checkHelp(context)
// check version checkVersion(context)
if context.GlobalBool("version") {
showVersion(context)
return
}
args := context.Args() args := context.Args()
if len(args) > 0 { if len(args) > 0 {

@ -2,8 +2,8 @@ package cli_test
import ( import (
"flag" "flag"
"testing"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"testing"
) )
func TestNewContext(t *testing.T) { func TestNewContext(t *testing.T) {

@ -9,7 +9,7 @@ type Flag interface {
} }
func flagSet(name string, flags []Flag) *flag.FlagSet { func flagSet(name string, flags []Flag) *flag.FlagSet {
set := flag.NewFlagSet(name, flag.ExitOnError) set := flag.NewFlagSet(name, flag.ContinueOnError)
for _, f := range flags { for _, f := range flags {
f.Apply(set) f.Apply(set)
} }
@ -56,3 +56,16 @@ func (f IntFlag) String() string {
func (f IntFlag) Apply(set *flag.FlagSet) { func (f IntFlag) Apply(set *flag.FlagSet) {
set.Int(f.Name, f.Value, f.Usage) 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)
}

@ -51,30 +51,36 @@ var helpCommand = Command{
Action: func(c *Context) { Action: func(c *Context) {
args := c.Args() args := c.Args()
if len(args) > 0 { if len(args) > 0 {
showCommandHelp(c) ShowCommandHelp(c, args[0])
} else { } else {
showAppHelp(c) ShowAppHelp(c)
} }
}, },
} }
func showAppHelp(c *Context) { // Prints help for the App
func ShowAppHelp(c *Context) {
printHelp(AppHelpTemplate, c.App) printHelp(AppHelpTemplate, c.App)
} }
func showCommandHelp(c *Context) { // Prints help for the given command
name := c.Args()[0] func ShowCommandHelp(c *Context, command string) {
for _, c := range c.App.Commands { for _, c := range c.App.Commands {
if c.HasName(name) { if c.HasName(command) {
printHelp(CommandHelpTemplate, c) printHelp(CommandHelpTemplate, c)
return return
} }
} }
fmt.Printf("No help topic for '%v'\n", name) fmt.Printf("No help topic for '%v'\n", command)
os.Exit(1) 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{}) { func printHelp(templ string, data interface{}) {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Parse(templ)) t := template.Must(template.New("help").Parse(templ))
@ -82,6 +88,16 @@ func printHelp(templ string, data interface{}) {
w.Flush() w.Flush()
} }
func showVersion(c *Context) { func checkVersion(c *Context) {
fmt.Printf("%v version %v\n", c.App.Name, c.App.Version) 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)
}
} }

Loading…
Cancel
Save