Adding some docs
This commit is contained in:
parent
518348eb46
commit
ed1ee94250
5
app.go
5
app.go
@ -13,9 +13,10 @@ type App struct {
|
||||
Version string
|
||||
// List of commands to execute
|
||||
Commands []Command
|
||||
Flags []Flag
|
||||
// List of flags to parse
|
||||
Flags []Flag
|
||||
// The action to execute when no subcommands are specified
|
||||
Action Handler
|
||||
Action func(context *Context)
|
||||
}
|
||||
|
||||
func NewApp() *App {
|
||||
|
24
app_test.go
Normal file
24
app_test.go
Normal file
@ -0,0 +1,24 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ExampleApp() {
|
||||
// set args for examples sake
|
||||
os.Args = []string{"greet", "--name", "Jeremy"}
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "greet"
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{"name", "bob", "a name to say"},
|
||||
}
|
||||
app.Action = func(c *cli.Context) {
|
||||
fmt.Printf("Hello %v\n", c.String("name"))
|
||||
}
|
||||
app.Run(os.Args)
|
||||
// Output:
|
||||
// Hello Jeremy
|
||||
}
|
20
cli.go
20
cli.go
@ -1,3 +1,19 @@
|
||||
// Package cli provides a minimal framework for creating and organizing command line
|
||||
// Go applications. cli is designed to be easy to understand and write, the most simple
|
||||
// cli application can be written as follows:
|
||||
// func main() {
|
||||
// cli.NewApp().Run(os.Args)
|
||||
// }
|
||||
//
|
||||
// Of course this application does not do much, so let's make this an actual application:
|
||||
// func main() {
|
||||
// app := cli.NewApp()
|
||||
// app.Name = "greet"
|
||||
// app.Usage = "say a greeting"
|
||||
// app.Action = func(c *cli.Context) {
|
||||
// println("Greetings")
|
||||
// }
|
||||
//
|
||||
// app.Run(os.Args)
|
||||
// }
|
||||
package cli
|
||||
|
||||
type Handler func(context *Context)
|
||||
|
88
cli_test.go
88
cli_test.go
@ -1,78 +1,32 @@
|
||||
package cli
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"github.com/codegangsta/cli"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Test_SettingFlags(t *testing.T) {
|
||||
msg := ""
|
||||
app := NewApp()
|
||||
app.Flags = []Flag{
|
||||
StringFlag{"foo", "default", "a string flag"},
|
||||
IntFlag{"bar", 42, "an int flag"},
|
||||
BoolFlag{"bat", "a bool flag"},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
expect(t, c.String("foo"), "hello world")
|
||||
expect(t, c.Int("bar"), 245)
|
||||
expect(t, c.Bool("bat"), true)
|
||||
msg = "foobar"
|
||||
}
|
||||
app.Run([]string{"command", "--foo", "hello world", "--bar", "245", "--bat"})
|
||||
expect(t, msg, "foobar")
|
||||
}
|
||||
|
||||
func Test_FlagDefaults(t *testing.T) {
|
||||
msg := ""
|
||||
app := NewApp()
|
||||
app.Flags = []Flag{
|
||||
StringFlag{"foo", "default", "a string flag"},
|
||||
IntFlag{"bar", 42, "an int flag"},
|
||||
BoolFlag{"bat", "a bool flag"},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
expect(t, c.String("foo"), "default")
|
||||
expect(t, c.Int("bar"), 42)
|
||||
expect(t, c.Bool("bat"), false)
|
||||
msg = "foobar"
|
||||
}
|
||||
app.Run([]string{"command"})
|
||||
expect(t, msg, "foobar")
|
||||
}
|
||||
|
||||
func TestCommands(t *testing.T) {
|
||||
app := NewApp()
|
||||
app.Flags = []Flag{
|
||||
StringFlag{"name", "jeremy", "a name to print"},
|
||||
}
|
||||
app.Commands = []Command{
|
||||
func Example() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "todo"
|
||||
app.Usage = "task list on the command line"
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "print",
|
||||
Flags: []Flag{
|
||||
IntFlag{"age", 50, "the age of the person"},
|
||||
Name: "add",
|
||||
ShortName: "a",
|
||||
Usage: "add a task to the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("added task: ", c.Args()[0])
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
expect(t, c.GlobalString("name"), "jordie")
|
||||
expect(t, c.Int("age"), 21)
|
||||
},
|
||||
{
|
||||
Name: "complete",
|
||||
ShortName: "c",
|
||||
Usage: "complete a task on the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("completed task: ", c.Args()[0])
|
||||
},
|
||||
},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
t.Error("default action should not be called")
|
||||
}
|
||||
app.Run([]string{"command", "--name", "jordie", "print", "--age", "21"})
|
||||
}
|
||||
|
||||
/* Test Helpers */
|
||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if a != b {
|
||||
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
||||
|
||||
func refute(t *testing.T, a interface{}, b interface{}) {
|
||||
if a == b {
|
||||
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ type Command struct {
|
||||
ShortName string
|
||||
Usage string
|
||||
Description string
|
||||
Action Handler
|
||||
Action func(context *Context)
|
||||
Flags []Flag
|
||||
}
|
||||
|
||||
|
@ -1 +0,0 @@
|
||||
package cli
|
19
helpers_test.go
Normal file
19
helpers_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Test Helpers */
|
||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if a != b {
|
||||
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
||||
|
||||
func refute(t *testing.T, a interface{}, b interface{}) {
|
||||
if a == b {
|
||||
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user