diff --git a/README.md b/README.md index 1ce79a71d4..d016e75fed 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ [![Build Status](https://travis-ci.org/codegangsta/cli.png?branch=master)](https://travis-ci.org/codegangsta/cli) +this is a patched version, with some additional features i found usefull, from [github.com/codegangsta/cli](github.com/codegangsta/cli). ideally those features will be merged upstream some day. + +--- + # cli.go cli.go is simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable command line applications in an expressive way. @@ -54,11 +58,13 @@ func main() { app := cli.NewApp() app.Name = "boom" app.Usage = "make an explosive entrance" - app.Action = func(c *cli.Context) { + app.Action = func(c *cli.Context) (err error) { println("boom! I say!") + ... + return err } - app.Run(os.Args) + err := app.Run(os.Args) } ``` @@ -90,8 +96,10 @@ func main() { app.License = "ASL-2 (Apache Software License version 2.0) " // if ommited nothig will be printed app.Reporting = "Report bugs to me, if you find any :-) ..." - app.Action = func(c *cli.Context) { + app.Action = func(c *cli.Context) (err error) { println("Hello friend!") + ... + return err } app.Run(os.Args) } @@ -145,8 +153,10 @@ You can lookup arguments by calling the `Args` function on cli.Context. ``` go ... -app.Action = func(c *cli.Context) { +app.Action = func(c *cli.Context) (err error) { println("Hello", c.Args()[0]) + ... + return err } ... ``` @@ -158,7 +168,7 @@ Setting and querying flags is simple. app.Flags = []cli.Flag { cli.StringFlag{"lang", "english", "language for the greeting"}, } -app.Action = func(c *cli.Context) { +app.Action = func(c *cli.Context) (err error){ name := "someone" if len(c.Args()) > 0 { name = c.Args()[0] @@ -170,6 +180,7 @@ app.Action = func(c *cli.Context) { } } ... +return err ``` #### Alternate Names @@ -194,11 +205,13 @@ app.Flags = []cli.Flag{ Usage: "enables debug mode", }, } -app.Action = func(c *cli.Context) { +app.Action = func(c *cli.Context) (err error) { if c.String("debug") == "true" { DEBUG = true log.Printf("DEBUG mode enabled.") } + ... + return err } ``` @@ -215,16 +228,18 @@ app.Commands = []cli.Command{ Name: "add", ShortName: "a", Usage: "add a task to the list", - Action: func(c *cli.Context) { + Action: func(c *cli.Context) (err error) { println("added task: ", c.Args().First()) + return err }, }, { Name: "complete", ShortName: "c", Usage: "complete a task on the list", - Action: func(c *cli.Context) { + Action: func(c *cli.Context) (err error) { println("completed task: ", c.Args().First()) + return err }, }, } diff --git a/app.go b/app.go index f2be834dc2..cbddf65aa0 100644 --- a/app.go +++ b/app.go @@ -25,7 +25,7 @@ type App struct { // If a non-nil error is returned, no subcommands are run Before func(context *Context) error // The action to execute when no subcommands are specified - Action func(context *Context) + Action func(context *Context) error // Compilation date Compiled time.Time // Author @@ -126,8 +126,7 @@ func (a *App) Run(arguments []string) error { } // Run default Action - a.Action(context) - return nil + return a.Action(context) } // Returns the named command on App. Returns nil if the command does not exist diff --git a/app_test.go b/app_test.go index f329b91361..2a6873a231 100644 --- a/app_test.go +++ b/app_test.go @@ -2,9 +2,11 @@ package cli_test import ( "fmt" - "github.com/codegangsta/cli" "os" "testing" + + "." + //"github.com/codegangsta/cli" ) func ExampleApp() { @@ -16,8 +18,9 @@ func ExampleApp() { app.Flags = []cli.Flag{ cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, } - app.Action = func(c *cli.Context) { + app.Action = func(c *cli.Context) (err error) { fmt.Printf("Hello %v\n", c.String("name")) + return err } app.Run(os.Args) // Output: @@ -28,8 +31,9 @@ func TestApp_Run(t *testing.T) { s := "" app := cli.NewApp() - app.Action = func(c *cli.Context) { + app.Action = func(c *cli.Context) (err error) { s = s + c.Args().First() + return nil } err := app.Run([]string{"command", "foo"}) @@ -74,9 +78,10 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) { Flags: []cli.Flag{ cli.StringFlag{Name: "option", Value: "", Usage: "some option"}, }, - Action: func(c *cli.Context) { + Action: func(c *cli.Context) (err error) { parsedOption = c.String("option") firstArg = c.Args().First() + return err }, } app.Commands = []cli.Command{command} @@ -94,8 +99,9 @@ func TestApp_Float64Flag(t *testing.T) { app.Flags = []cli.Flag{ cli.Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"}, } - app.Action = func(c *cli.Context) { + app.Action = func(c *cli.Context) (err error) { meters = c.Float64("height") + return err } app.Run([]string{"", "--height", "1.93"}) @@ -114,11 +120,12 @@ func TestApp_ParseSliceFlags(t *testing.T) { cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"}, cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"}, }, - Action: func(c *cli.Context) { + Action: func(c *cli.Context) (err error) { parsedIntSlice = c.IntSlice("p") parsedStringSlice = c.StringSlice("ip") parsedOption = c.String("option") firstArg = c.Args().First() + return err }, } app.Commands = []cli.Command{command} @@ -180,8 +187,9 @@ func TestApp_BeforeFunc(t *testing.T) { app.Commands = []cli.Command{ cli.Command{ Name: "sub", - Action: func(c *cli.Context) { + Action: func(c *cli.Context) (err error) { subcommandRun = true + return err }, }, } diff --git a/cli.go b/cli.go index b742545812..c196908048 100644 --- a/cli.go +++ b/cli.go @@ -10,8 +10,9 @@ // app := cli.NewApp() // app.Name = "greet" // app.Usage = "say a greeting" -// app.Action = func(c *cli.Context) { +// app.Action = func(c *cli.Context) (err error) { // println("Greetings") +// return err // } // // app.Run(os.Args) diff --git a/command.go b/command.go index b93f56ed73..be5be55e82 100644 --- a/command.go +++ b/command.go @@ -17,7 +17,7 @@ type Command struct { // A longer explaination of how the command works Description string // The function to call when this command is invoked - Action func(context *Context) + Action func(context *Context) error // List of flags to parse Flags []Flag // if True command won't be displayed on help @@ -72,8 +72,7 @@ func (c Command) Run(ctx *Context) error { if checkCommandHelp(context, c.Name) { return nil } - c.Action(context) - return nil + return c.Action(context) } // Returns true if Command.Name or Command.ShortName matches given name diff --git a/help.go b/help.go index d13b89b7d2..b3ea40f8a8 100644 --- a/help.go +++ b/help.go @@ -58,18 +58,20 @@ var helpCommand = Command{ Name: "help", ShortName: "h", Usage: "Shows a list of commands or help for one command", - Action: func(c *Context) { + Action: func(c *Context) (err error) { args := c.Args() if args.Present() { ShowCommandHelp(c, args.First()) } else { ShowAppHelp(c) } + return err }, } // Prints help for the App var HelpPrinter = printHelp + func ShowAppHelp(c *Context) { HelpPrinter(AppHelpTemplate, c.App) }