Ensure README examples are runnable

This commit is contained in:
Dan Buch 2016-04-26 07:05:50 -04:00
parent a17c8cf1d8
commit b40b62794d
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

View File

@ -50,7 +50,9 @@ This app will run and show help text, but is not very useful. Let's give an acti
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
@ -58,8 +60,9 @@ func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "boom" app.Name = "boom"
app.Usage = "make an explosive entrance" app.Usage = "make an explosive entrance"
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) int {
println("boom! I say!") fmt.Println("boom! I say!")
return 0
} }
app.Run(os.Args) app.Run(os.Args)
@ -78,7 +81,9 @@ Start by creating a directory named `greet`, and within it, add a file, `greet.g
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
@ -86,8 +91,9 @@ func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "greet" app.Name = "greet"
app.Usage = "fight the loneliness!" app.Usage = "fight the loneliness!"
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) int {
println("Hello friend!") fmt.Println("Hello friend!")
return 0
} }
app.Run(os.Args) app.Run(os.Args)
@ -370,8 +376,9 @@ COMMANDS:
### Exit code ### Exit code
It is your responsibility to call `os.Exit` with the exit code returned by Calling `App.Run` will not automatically call `os.Exit`, which means that by
`app.Run`, e.g.: default the exit code will "fall through" to being `0`. Proper exit code
propagation is the responsibility of the code that calls `App.Run`, e.g.:
```go ```go
package main package main
@ -382,10 +389,7 @@ import (
) )
func main() { func main() {
exitCode, err := cli.NewApp().Run(os.Args) exitCode, _ := cli.NewApp().Run(os.Args)
if err != nil {
log.Println(err)
}
os.Exit(exitCode) os.Exit(exitCode)
} }
``` ```