forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
518348e
commit ed1ee94
Showing
7 changed files
with
86 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |