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
|
Version string
|
||||||
// List of commands to execute
|
// List of commands to execute
|
||||||
Commands []Command
|
Commands []Command
|
||||||
Flags []Flag
|
// List of flags to parse
|
||||||
|
Flags []Flag
|
||||||
// The action to execute when no subcommands are specified
|
// The action to execute when no subcommands are specified
|
||||||
Action Handler
|
Action func(context *Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp() *App {
|
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
|
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 (
|
import (
|
||||||
"reflect"
|
"github.com/codegangsta/cli"
|
||||||
"testing"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_SettingFlags(t *testing.T) {
|
func Example() {
|
||||||
msg := ""
|
app := cli.NewApp()
|
||||||
app := NewApp()
|
app.Name = "todo"
|
||||||
app.Flags = []Flag{
|
app.Usage = "task list on the command line"
|
||||||
StringFlag{"foo", "default", "a string flag"},
|
app.Commands = []cli.Command{
|
||||||
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{
|
|
||||||
{
|
{
|
||||||
Name: "print",
|
Name: "add",
|
||||||
Flags: []Flag{
|
ShortName: "a",
|
||||||
IntFlag{"age", 50, "the age of the person"},
|
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 */
|
app.Run(os.Args)
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ type Command struct {
|
|||||||
ShortName string
|
ShortName string
|
||||||
Usage string
|
Usage string
|
||||||
Description string
|
Description string
|
||||||
Action Handler
|
Action func(context *Context)
|
||||||
Flags []Flag
|
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