Update cobra und spf13

This commit is contained in:
Alexander Neumann
2017-04-21 19:19:14 +02:00
parent aed0126102
commit ab870dd691
23 changed files with 1679 additions and 1492 deletions

View File

@@ -3,7 +3,6 @@ package cobra
import (
"bytes"
"fmt"
"os"
"reflect"
"runtime"
"strings"
@@ -13,9 +12,6 @@ import (
"github.com/spf13/pflag"
)
var _ = fmt.Println
var _ = os.Stderr
var tp, te, tt, t1, tr []string
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
var flagb1, flagb2, flagb3, flagbr, flagbp bool
@@ -166,20 +162,22 @@ func flagInit() {
cmdRootWithRun.ResetFlags()
cmdSubNoRun.ResetFlags()
cmdCustomFlags.ResetFlags()
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
cmdCustomFlags.Flags().IntVar(&flagi4, "intfour", 456, "help message for flag intfour")
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")
cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
cmdVersion1.ResetFlags()
cmdVersion2.ResetFlags()
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
cmdCustomFlags.Flags().IntVar(&flagi4, "intfour", 456, "help message for flag intfour")
cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
cmdTimes.Flags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
}
func commandInit() {
@@ -571,7 +569,7 @@ func TestChildCommandFlags(t *testing.T) {
if r.Error == nil {
t.Errorf("invalid input should generate error")
}
if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") {
if !strings.Contains(r.Error.Error(), "invalid syntax") {
t.Errorf("Wrong error message displayed, \n %s", r.Error)
}
}
@@ -677,7 +675,7 @@ func TestPersistentFlags(t *testing.T) {
fullSetupTest("echo times -s again -c -p test here")
if strings.Join(tt, " ") != "test here" {
t.Errorf("flags didn't leave proper args remaining..%s given", tt)
t.Errorf("flags didn't leave proper args remaining. %s given", tt)
}
if flags1 != "again" {
@@ -858,7 +856,6 @@ func TestFlagAccess(t *testing.T) {
}
if inherited.Lookup("strtwo") != nil {
t.Errorf("InheritedFlags shouldn not contain overwritten flag strtwo")
}
}
@@ -990,7 +987,7 @@ func TestFlagsBeforeCommand(t *testing.T) {
// With parsing error properly reported
x = fullSetupTest("-i10E echo")
if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") {
if !strings.Contains(x.Error.Error(), "invalid syntax") {
t.Errorf("Wrong error message displayed, \n %s", x.Error)
}
@@ -1185,3 +1182,23 @@ func TestUsageIsNotPrintedTwice(t *testing.T) {
t.Error("Usage output is not printed exactly once")
}
}
func BenchmarkInheritedFlags(b *testing.B) {
initialize()
cmdEcho.AddCommand(cmdTimes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cmdTimes.InheritedFlags()
}
}
func BenchmarkLocalFlags(b *testing.B) {
initialize()
cmdEcho.AddCommand(cmdTimes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cmdTimes.LocalFlags()
}
}