e5c31ab592
- Added a method for looking up commands - Using some logic to make sure default flags are not added more than once
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/codegangsta/cli"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func TestApp_Run(t *testing.T) {
|
|
s := ""
|
|
|
|
app := cli.NewApp()
|
|
app.Action = func(c *cli.Context) {
|
|
s = s + c.Args()[0]
|
|
}
|
|
|
|
app.Run([]string{"command", "foo"})
|
|
app.Run([]string{"command", "bar"})
|
|
expect(t, s, "foobar")
|
|
}
|
|
|
|
var commandAppTests = []struct {
|
|
name string
|
|
expected bool
|
|
}{
|
|
{"foobar", true},
|
|
{"batbaz", true},
|
|
{"b", true},
|
|
{"f", true},
|
|
{"bat", false},
|
|
{"nothing", false},
|
|
}
|
|
|
|
func TestApp_Command(t *testing.T) {
|
|
app := cli.NewApp()
|
|
fooCommand := cli.Command{Name: "foobar", ShortName: "f"}
|
|
batCommand := cli.Command{Name: "batbaz", ShortName: "b"}
|
|
app.Commands = []cli.Command{
|
|
fooCommand,
|
|
batCommand,
|
|
}
|
|
|
|
for _, test := range commandAppTests {
|
|
expect(t, app.Command(test.name) != nil, test.expected)
|
|
}
|
|
}
|