From ed1ee94250bba1aff2a14d93056f074fd2135af5 Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Sat, 20 Jul 2013 15:50:13 -0700 Subject: [PATCH] Adding some docs --- app.go | 5 +-- app_test.go | 24 ++++++++++++++ cli.go | 20 +++++++++-- cli_test.go | 88 ++++++++++++------------------------------------- command.go | 2 +- command_test.go | 1 - helpers_test.go | 19 +++++++++++ 7 files changed, 86 insertions(+), 73 deletions(-) create mode 100644 app_test.go delete mode 100644 command_test.go create mode 100644 helpers_test.go diff --git a/app.go b/app.go index e203930..180e364 100644 --- a/app.go +++ b/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 { diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..bcabe07 --- /dev/null +++ b/app_test.go @@ -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 +} diff --git a/cli.go b/cli.go index 567e04a..b742545 100644 --- a/cli.go +++ b/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) diff --git a/cli_test.go b/cli_test.go index 1b362d3..8e535ec 100644 --- a/cli_test.go +++ b/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) } diff --git a/command.go b/command.go index 671c218..a9403eb 100644 --- a/command.go +++ b/command.go @@ -5,7 +5,7 @@ type Command struct { ShortName string Usage string Description string - Action Handler + Action func(context *Context) Flags []Flag } diff --git a/command_test.go b/command_test.go deleted file mode 100644 index 7f1e458..0000000 --- a/command_test.go +++ /dev/null @@ -1 +0,0 @@ -package cli diff --git a/helpers_test.go b/helpers_test.go new file mode 100644 index 0000000..3ce8e93 --- /dev/null +++ b/helpers_test.go @@ -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)) + } +}