2015-03-11 21:52:35 +03:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-16 22:47:32 +03:00
|
|
|
// TODO Test persistent flags on subcommands. This requires testing the full execute function.
|
|
|
|
|
2015-03-11 21:52:35 +03:00
|
|
|
func TestStripFlags(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input []string
|
|
|
|
output []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"foo", "bar"},
|
|
|
|
[]string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"foo", "--bar", "-b"},
|
|
|
|
[]string{"foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-b", "foo", "--bar", "bar"},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-i10", "echo"},
|
|
|
|
[]string{"echo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-i=10", "echo"},
|
|
|
|
[]string{"echo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"--int=100", "echo"},
|
|
|
|
[]string{"echo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-ib", "echo", "-bfoo", "baz"},
|
|
|
|
[]string{"echo", "baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-i=baz", "bar", "-i", "foo", "blah"},
|
|
|
|
[]string{"bar", "blah"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"--int=baz", "-bbar", "-i", "foo", "blah"},
|
|
|
|
[]string{"blah"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"--cat", "bar", "-i", "foo", "blah"},
|
|
|
|
[]string{"bar", "blah"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-c", "bar", "-i", "foo", "blah"},
|
|
|
|
[]string{"bar", "blah"},
|
|
|
|
},
|
2015-03-16 22:47:32 +03:00
|
|
|
{
|
|
|
|
[]string{"--persist", "bar"},
|
|
|
|
[]string{"bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-p", "bar"},
|
|
|
|
[]string{"bar"},
|
|
|
|
},
|
2015-03-11 21:52:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdPrint := &Command{
|
|
|
|
Use: "print [string to print]",
|
|
|
|
Short: "Print anything to the screen",
|
|
|
|
Long: `an utterly useless command for testing.`,
|
|
|
|
Run: func(cmd *Command, args []string) {
|
|
|
|
tp = args
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var flagi int
|
|
|
|
var flagstr string
|
|
|
|
var flagbool bool
|
2015-03-16 22:47:32 +03:00
|
|
|
cmdPrint.PersistentFlags().BoolVarP(&flagbool, "persist", "p", false, "help for persistent one")
|
2015-03-11 21:52:35 +03:00
|
|
|
cmdPrint.Flags().IntVarP(&flagi, "int", "i", 345, "help message for flag int")
|
|
|
|
cmdPrint.Flags().StringVarP(&flagstr, "bar", "b", "bar", "help message for flag string")
|
|
|
|
cmdPrint.Flags().BoolVarP(&flagbool, "cat", "c", false, "help message for flag bool")
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
output := stripFlags(test.input, cmdPrint)
|
|
|
|
if !reflect.DeepEqual(test.output, output) {
|
|
|
|
t.Errorf("expected: %v, got: %v", test.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|