Skip to content

Commit

Permalink
Like what is already possible for 'Before' actions, get 'Actions' to …
Browse files Browse the repository at this point in the history
…return on

exit, so that app.Run() gets to catch  all errors, if any, underneath it.
This commit induces a minor API break. [ for the sake of perfection, the tests
would need to be extended a bit for full coverage. ]
  • Loading branch information
AntonioMeireles committed Feb 11, 2014
1 parent 563b43c commit 52496bf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
31 changes: 23 additions & 8 deletions 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.

Expand Down Expand Up @@ -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)
}
```

Expand Down Expand Up @@ -90,8 +96,10 @@ func main() {
app.License = "ASL-2 (Apache Software License version 2.0) <http://www.apache.org/licenses/LICENSE-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)
}
Expand Down Expand Up @@ -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
}
...
```
Expand All @@ -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]
Expand All @@ -170,6 +180,7 @@ app.Action = func(c *cli.Context) {
}
}
...
return err
```

#### Alternate Names
Expand All @@ -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
}

```
Expand All @@ -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
},
},
}
Expand Down
5 changes: 2 additions & 3 deletions app.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions app_test.go
Expand Up @@ -2,9 +2,11 @@ package cli_test

import (
"fmt"
"github.com/codegangsta/cli"
"os"
"testing"

"."
//"github.com/codegangsta/cli"
)

func ExampleApp() {
Expand All @@ -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:
Expand All @@ -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"})
Expand Down Expand Up @@ -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}
Expand All @@ -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"})
Expand All @@ -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}
Expand Down Expand Up @@ -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
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion cli.go
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions command.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion help.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit 52496bf

Please sign in to comment.