|
|
|
@ -21,17 +21,19 @@ func ExampleApp_Run() {
|
|
|
|
|
// set args for examples sake
|
|
|
|
|
os.Args = []string{"greet", "--name", "Jeremy"}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "greet"
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "greet",
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
|
|
|
|
}
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
fmt.Printf("Hello %v\n", c.String("name"))
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
UsageText: "app [first_arg] [second_arg]",
|
|
|
|
|
Authors: []*Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}},
|
|
|
|
|
}
|
|
|
|
|
app.UsageText = "app [first_arg] [second_arg]"
|
|
|
|
|
app.Authors = []*Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}}
|
|
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
|
// Output:
|
|
|
|
|
// Hello Jeremy
|
|
|
|
@ -40,9 +42,9 @@ func ExampleApp_Run() {
|
|
|
|
|
func ExampleApp_Run_subcommand() {
|
|
|
|
|
// set args for examples sake
|
|
|
|
|
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "say"
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "say",
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "hello",
|
|
|
|
|
Aliases: []string{"hi"},
|
|
|
|
@ -68,6 +70,7 @@ func ExampleApp_Run_subcommand() {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
@ -79,12 +82,12 @@ func ExampleApp_Run_help() {
|
|
|
|
|
// set args for examples sake
|
|
|
|
|
os.Args = []string{"greet", "h", "describeit"}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "greet"
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "greet",
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
|
|
|
|
}
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "describeit",
|
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
@ -95,6 +98,7 @@ func ExampleApp_Run_help() {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
|
// Output:
|
|
|
|
@ -112,10 +116,10 @@ func ExampleApp_Run_bashComplete() {
|
|
|
|
|
// set args for examples sake
|
|
|
|
|
os.Args = []string{"greet", "--generate-bash-completion"}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "greet"
|
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "greet",
|
|
|
|
|
EnableBashCompletion: true,
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "describeit",
|
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
@ -134,6 +138,7 @@ func ExampleApp_Run_bashComplete() {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
@ -148,10 +153,11 @@ func ExampleApp_Run_bashComplete() {
|
|
|
|
|
func TestApp_Run(t *testing.T) {
|
|
|
|
|
s := ""
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
app := &App{
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
s = s + c.Args().First()
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"command", "foo"})
|
|
|
|
@ -174,7 +180,7 @@ var commandAppTests = []struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Command(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
fooCommand := &Command{Name: "foobar", Aliases: []string{"f"}}
|
|
|
|
|
batCommand := &Command{Name: "batbaz", Aliases: []string{"b"}}
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
@ -190,8 +196,8 @@ func TestApp_Command(t *testing.T) {
|
|
|
|
|
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
|
|
|
|
var context *Context
|
|
|
|
|
|
|
|
|
|
a := NewApp()
|
|
|
|
|
a.Commands = []*Command{
|
|
|
|
|
a := &App{
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "foo",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -207,6 +213,7 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
|
|
|
|
},
|
|
|
|
|
Before: func(_ *Context) error { return nil },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
|
|
|
|
|
|
|
|
|
@ -218,7 +225,7 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
|
|
|
|
|
var parsedOption string
|
|
|
|
|
var args Args
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
command := &Command{
|
|
|
|
|
Name: "cmd",
|
|
|
|
|
Flags: []Flag{
|
|
|
|
@ -243,7 +250,7 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
|
|
|
|
|
func TestApp_CommandWithDash(t *testing.T) {
|
|
|
|
|
var args Args
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
command := &Command{
|
|
|
|
|
Name: "cmd",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -262,7 +269,7 @@ func TestApp_CommandWithDash(t *testing.T) {
|
|
|
|
|
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
|
|
|
|
var args Args
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
command := &Command{
|
|
|
|
|
Name: "cmd",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -280,8 +287,8 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_VisibleCommands(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app := &App{
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "frob",
|
|
|
|
|
HelpName: "foo frob",
|
|
|
|
@ -293,6 +300,7 @@ func TestApp_VisibleCommands(t *testing.T) {
|
|
|
|
|
Hidden: true,
|
|
|
|
|
Action: func(_ *Context) error { return nil },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
@ -332,13 +340,14 @@ func TestApp_VisibleCommands(t *testing.T) {
|
|
|
|
|
func TestApp_Float64Flag(t *testing.T) {
|
|
|
|
|
var meters float64
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
app := &App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
|
|
|
|
|
}
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
meters = c.Float64("height")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Run([]string{"", "--height", "1.93"})
|
|
|
|
@ -350,7 +359,7 @@ func TestApp_ParseSliceFlags(t *testing.T) {
|
|
|
|
|
var parsedIntSlice []int
|
|
|
|
|
var parsedStringSlice []string
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
command := &Command{
|
|
|
|
|
Name: "cmd",
|
|
|
|
|
Flags: []Flag{
|
|
|
|
@ -408,7 +417,7 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
|
|
|
|
|
var parsedIntSlice []int
|
|
|
|
|
var parsedStringSlice []string
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
command := &Command{
|
|
|
|
|
Name: "cmd",
|
|
|
|
|
Flags: []Flag{
|
|
|
|
@ -438,7 +447,8 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_DefaultStdout(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
app.Setup()
|
|
|
|
|
|
|
|
|
|
if app.Writer != os.Stdout {
|
|
|
|
|
t.Error("Default output writer not set.")
|
|
|
|
@ -466,9 +476,10 @@ func (fw *mockWriter) GetWritten() (b []byte) {
|
|
|
|
|
func TestApp_SetStdout(t *testing.T) {
|
|
|
|
|
w := &mockWriter{}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "test"
|
|
|
|
|
app.Writer = w
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "test",
|
|
|
|
|
Writer: w,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"help"})
|
|
|
|
|
|
|
|
|
@ -486,9 +497,8 @@ func TestApp_BeforeFunc(t *testing.T) {
|
|
|
|
|
beforeError := fmt.Errorf("fail")
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
|
|
|
|
|
app.Before = func(c *Context) error {
|
|
|
|
|
app := &App{
|
|
|
|
|
Before: func(c *Context) error {
|
|
|
|
|
counts.Total++
|
|
|
|
|
counts.Before = counts.Total
|
|
|
|
|
s := c.String("opt")
|
|
|
|
@ -497,9 +507,8 @@ func TestApp_BeforeFunc(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "sub",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -508,10 +517,10 @@ func TestApp_BeforeFunc(t *testing.T) {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
},
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&StringFlag{Name: "opt"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run with the Before() func succeeding
|
|
|
|
@ -578,9 +587,8 @@ func TestApp_AfterFunc(t *testing.T) {
|
|
|
|
|
afterError := fmt.Errorf("fail")
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
|
|
|
|
|
app.After = func(c *Context) error {
|
|
|
|
|
app := &App{
|
|
|
|
|
After: func(c *Context) error {
|
|
|
|
|
counts.Total++
|
|
|
|
|
counts.After = counts.Total
|
|
|
|
|
s := c.String("opt")
|
|
|
|
@ -589,9 +597,8 @@ func TestApp_AfterFunc(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "sub",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -600,10 +607,10 @@ func TestApp_AfterFunc(t *testing.T) {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
},
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&StringFlag{Name: "opt"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run with the After() func succeeding
|
|
|
|
@ -649,8 +656,7 @@ func TestAppNoHelpFlag(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
HelpFlag = nil
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Writer = ioutil.Discard
|
|
|
|
|
app := &App{Writer: ioutil.Discard}
|
|
|
|
|
err := app.Run([]string{"test", "-h"})
|
|
|
|
|
|
|
|
|
|
if err != flag.ErrHelp {
|
|
|
|
@ -669,7 +675,7 @@ func TestAppHelpPrinter(t *testing.T) {
|
|
|
|
|
wasCalled = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
app.Run([]string{"-h"})
|
|
|
|
|
|
|
|
|
|
if wasCalled == false {
|
|
|
|
@ -688,7 +694,7 @@ func TestApp_VersionPrinter(t *testing.T) {
|
|
|
|
|
wasCalled = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
ctx := NewContext(app, nil, nil)
|
|
|
|
|
ShowVersion(ctx)
|
|
|
|
|
|
|
|
|
@ -699,14 +705,12 @@ func TestApp_VersionPrinter(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestApp_CommandNotFound(t *testing.T) {
|
|
|
|
|
counts := &opCounts{}
|
|
|
|
|
app := NewApp()
|
|
|
|
|
|
|
|
|
|
app.CommandNotFound = func(c *Context, command string) {
|
|
|
|
|
app := &App{
|
|
|
|
|
CommandNotFound: func(c *Context, command string) {
|
|
|
|
|
counts.Total++
|
|
|
|
|
counts.CommandNotFound = counts.Total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "bar",
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
@ -715,6 +719,7 @@ func TestApp_CommandNotFound(t *testing.T) {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Run([]string{"command", "foo"})
|
|
|
|
@ -729,17 +734,17 @@ func TestApp_OrderOfOperations(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
resetCounts := func() { counts = &opCounts{} }
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
|
app.BashComplete = func(c *Context) {
|
|
|
|
|
app := &App{
|
|
|
|
|
EnableBashCompletion: true,
|
|
|
|
|
BashComplete: func(c *Context) {
|
|
|
|
|
counts.Total++
|
|
|
|
|
counts.BashComplete = counts.Total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
},
|
|
|
|
|
OnUsageError: func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
counts.Total++
|
|
|
|
|
counts.OnUsageError = counts.Total
|
|
|
|
|
return errors.New("hay OnUsageError")
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeNoError := func(c *Context) error {
|
|
|
|
@ -875,7 +880,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
|
|
|
|
|
for _, flagSet := range subcommandHelpTopics {
|
|
|
|
|
t.Logf("==> checking with flags %v", flagSet)
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
|
|
|
|
@ -920,7 +925,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Name = "command"
|
|
|
|
@ -953,7 +958,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_SubcommandHelpName(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Name = "command"
|
|
|
|
@ -988,7 +993,7 @@ func TestApp_Run_SubcommandHelpName(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_CommandHelpName(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Name = "command"
|
|
|
|
@ -1023,7 +1028,7 @@ func TestApp_Run_CommandHelpName(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app := &App{}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Name = "base"
|
|
|
|
@ -1065,13 +1070,14 @@ func TestApp_Run_Help(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
t.Logf("==> checking with arguments %v", args)
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "boom"
|
|
|
|
|
app.Usage = "make an explosive entrance"
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "boom",
|
|
|
|
|
Usage: "make an explosive entrance",
|
|
|
|
|
Writer: buf,
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
buf.WriteString("boom I say!")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run(args)
|
|
|
|
@ -1096,14 +1102,15 @@ func TestApp_Run_Version(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
t.Logf("==> checking with arguments %v", args)
|
|
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "boom"
|
|
|
|
|
app.Usage = "make an explosive entrance"
|
|
|
|
|
app.Version = "0.1.0"
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "boom",
|
|
|
|
|
Usage: "make an explosive entrance",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
Writer: buf,
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
buf.WriteString("boom I say!")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run(args)
|
|
|
|
@ -1121,9 +1128,11 @@ func TestApp_Run_Version(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_Categories(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "categories"
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "categories",
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "command1",
|
|
|
|
|
Category: "1",
|
|
|
|
@ -1136,9 +1145,9 @@ func TestApp_Run_Categories(t *testing.T) {
|
|
|
|
|
Name: "command3",
|
|
|
|
|
Category: "2",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Writer: buf,
|
|
|
|
|
}
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
app.Writer = buf
|
|
|
|
|
|
|
|
|
|
app.Run([]string{"categories"})
|
|
|
|
|
|
|
|
|
@ -1171,9 +1180,9 @@ func TestApp_Run_Categories(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Name = "visible-categories"
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "visible-categories",
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "command1",
|
|
|
|
|
Category: "1",
|
|
|
|
@ -1190,6 +1199,7 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
Category: "3",
|
|
|
|
|
HelpName: "foo command3",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := []CommandCategory{
|
|
|
|
@ -1210,9 +1220,9 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
app.Setup()
|
|
|
|
|
expect(t, expected, app.VisibleCategories())
|
|
|
|
|
|
|
|
|
|
app = NewApp()
|
|
|
|
|
app.Name = "visible-categories"
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app = &App{
|
|
|
|
|
Name: "visible-categories",
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "command1",
|
|
|
|
|
Category: "1",
|
|
|
|
@ -1230,6 +1240,7 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
Category: "3",
|
|
|
|
|
HelpName: "foo command3",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected = []CommandCategory{
|
|
|
|
@ -1244,9 +1255,9 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
app.Setup()
|
|
|
|
|
expect(t, expected, app.VisibleCategories())
|
|
|
|
|
|
|
|
|
|
app = NewApp()
|
|
|
|
|
app.Name = "visible-categories"
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app = &App{
|
|
|
|
|
Name: "visible-categories",
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "command1",
|
|
|
|
|
Category: "1",
|
|
|
|
@ -1265,6 +1276,7 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
HelpName: "foo command3",
|
|
|
|
|
Hidden: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
@ -1272,10 +1284,11 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Action = func(c *Context) error { return nil }
|
|
|
|
|
app.Before = func(c *Context) error { return fmt.Errorf("before error") }
|
|
|
|
|
app.After = func(c *Context) error { return fmt.Errorf("after error") }
|
|
|
|
|
app := &App{
|
|
|
|
|
Action: func(c *Context) error { return nil },
|
|
|
|
|
Before: func(c *Context) error { return fmt.Errorf("before error") },
|
|
|
|
|
After: func(c *Context) error { return fmt.Errorf("after error") },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo"})
|
|
|
|
|
if err == nil {
|
|
|
|
@ -1291,8 +1304,8 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
app := &App{
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Subcommands: []*Command{
|
|
|
|
|
{
|
|
|
|
@ -1303,6 +1316,7 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
|
|
|
|
Before: func(c *Context) error { return fmt.Errorf("before error") },
|
|
|
|
|
After: func(c *Context) error { return fmt.Errorf("after error") },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo", "bar"})
|
|
|
|
@ -1319,11 +1333,11 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
app := &App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&IntFlag{Name: "flag"},
|
|
|
|
|
}
|
|
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
},
|
|
|
|
|
OnUsageError: func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
if isSubcommand {
|
|
|
|
|
t.Errorf("Expect no subcommand")
|
|
|
|
|
}
|
|
|
|
@ -1331,11 +1345,12 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
|
|
|
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
|
|
|
|
}
|
|
|
|
|
return errors.New("intercepted: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "bar",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo", "--flag=wrong"})
|
|
|
|
@ -1349,11 +1364,11 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
|
|
|
|
|
app := NewApp()
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
app := &App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&IntFlag{Name: "flag"},
|
|
|
|
|
}
|
|
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
},
|
|
|
|
|
OnUsageError: func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
|
if isSubcommand {
|
|
|
|
|
t.Errorf("Expect subcommand")
|
|
|
|
|
}
|
|
|
|
@ -1361,11 +1376,12 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
|
|
|
|
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
|
|
|
|
}
|
|
|
|
|
return errors.New("intercepted: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
app.Commands = []*Command{
|
|
|
|
|
},
|
|
|
|
|
Commands: []*Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "bar",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo", "--flag=wrong", "bar"})
|
|
|
|
|