Merge pull request #11 from codegangsta/JMS-3

JMS #3: Passing a context struct instead of a string
This commit is contained in:
Jeremy Saenz 2013-07-16 08:19:56 -07:00
commit ad47ead260
2 changed files with 9 additions and 5 deletions

10
cli.go
View File

@ -21,18 +21,19 @@ var Flags []Flag
var Action = ShowHelp var Action = ShowHelp
func Run(args []string) { func Run(args []string) {
context := Context{}
if len(args) > 1 { if len(args) > 1 {
name := args[1] name := args[1]
for _, c := range append(Commands, HelpCommand) { for _, c := range append(Commands, HelpCommand) {
if c.Name == name || c.ShortName == name { if c.Name == name || c.ShortName == name {
c.Action(name) c.Action(context)
return return
} }
} }
} }
// Run default Action // Run default Action
Action("") Action(context)
} }
type Command struct { type Command struct {
@ -44,4 +45,7 @@ type Command struct {
Flags flag.FlagSet Flags flag.FlagSet
} }
type Handler func(name string) type Context struct {
}
type Handler func(context Context)

View File

@ -22,7 +22,7 @@ func init() {
HelpCommand.Action = ShowHelp HelpCommand.Action = ShowHelp
} }
func ShowHelp(name string) { func ShowHelp(c Context) {
helpTemplate := `NAME: helpTemplate := `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
@ -44,7 +44,7 @@ GLOBAL OPTIONS
Usage, Usage,
Version, Version,
append(Commands, HelpCommand), append(Commands, HelpCommand),
Flags, Flags,
} }
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)