summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/pflag
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/pflag')
-rw-r--r--vendor/github.com/spf13/pflag/README.md6
-rw-r--r--vendor/github.com/spf13/pflag/flag.go83
-rw-r--r--vendor/github.com/spf13/pflag/flag_test.go44
3 files changed, 109 insertions, 24 deletions
diff --git a/vendor/github.com/spf13/pflag/README.md b/vendor/github.com/spf13/pflag/README.md
index eefb46dec..3001fcd2b 100644
--- a/vendor/github.com/spf13/pflag/README.md
+++ b/vendor/github.com/spf13/pflag/README.md
@@ -270,8 +270,8 @@ func main() {
You can see the full reference documentation of the pflag package
[at godoc.org][3], or through go's standard documentation system by
running `godoc -http=:6060` and browsing to
-[http://localhost:6060/pkg/github.com/ogier/pflag][2] after
+[http://localhost:6060/pkg/github.com/spf13/pflag][2] after
installation.
-[2]: http://localhost:6060/pkg/github.com/ogier/pflag
-[3]: http://godoc.org/github.com/ogier/pflag
+[2]: http://localhost:6060/pkg/github.com/spf13/pflag
+[3]: http://godoc.org/github.com/spf13/pflag
diff --git a/vendor/github.com/spf13/pflag/flag.go b/vendor/github.com/spf13/pflag/flag.go
index 746af6327..0730bd637 100644
--- a/vendor/github.com/spf13/pflag/flag.go
+++ b/vendor/github.com/spf13/pflag/flag.go
@@ -16,9 +16,9 @@ pflag is a drop-in replacement of Go's native flag package. If you import
pflag under the name "flag" then all code should continue to function
with no changes.
- import flag "github.com/ogier/pflag"
+ import flag "github.com/spf13/pflag"
- There is one exception to this: if you directly instantiate the Flag struct
+There is one exception to this: if you directly instantiate the Flag struct
there is one more field "Shorthand" that you will need to set.
Most code never instantiates this struct directly, and instead uses
functions such as String(), BoolVar(), and Var(), and is therefore
@@ -134,14 +134,21 @@ type FlagSet struct {
// a custom error handler.
Usage func()
+ // SortFlags is used to indicate, if user wants to have sorted flags in
+ // help/usage messages.
+ SortFlags bool
+
name string
parsed bool
actual map[NormalizedName]*Flag
+ orderedActual []*Flag
+ sortedActual []*Flag
formal map[NormalizedName]*Flag
+ orderedFormal []*Flag
+ sortedFormal []*Flag
shorthands map[byte]*Flag
args []string // arguments after flags
argsLenAtDash int // len(args) when a '--' was located when parsing, or -1 if no --
- exitOnError bool // does the program exit if there's an error?
errorHandling ErrorHandling
output io.Writer // nil means stderr; use out() accessor
interspersed bool // allow interspersed option/non-option args
@@ -156,7 +163,7 @@ type Flag struct {
Value Value // value as set
DefValue string // default value (as text); for usage message
Changed bool // If the user set the value (or if left to default)
- NoOptDefVal string //default value (as text); if the flag is on the command line without any options
+ NoOptDefVal string // default value (as text); if the flag is on the command line without any options
Deprecated string // If this flag is deprecated, this string is the new or now thing to use
Hidden bool // used by cobra.Command to allow flags to be hidden from help/usage text
ShorthandDeprecated string // If the shorthand of this flag is deprecated, this string is the new or now thing to use
@@ -194,11 +201,13 @@ func sortFlags(flags map[NormalizedName]*Flag) []*Flag {
// "--getUrl" which may also be translated to "geturl" and everything will work.
func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName) {
f.normalizeNameFunc = n
- for k, v := range f.formal {
- delete(f.formal, k)
- nname := f.normalizeFlagName(string(k))
- f.formal[nname] = v
+ f.sortedFormal = f.sortedFormal[:0]
+ for k, v := range f.orderedFormal {
+ delete(f.formal, NormalizedName(v.Name))
+ nname := f.normalizeFlagName(v.Name)
v.Name = string(nname)
+ f.formal[nname] = v
+ f.orderedFormal[k] = v
}
}
@@ -229,10 +238,25 @@ func (f *FlagSet) SetOutput(output io.Writer) {
f.output = output
}
-// VisitAll visits the flags in lexicographical order, calling fn for each.
+// VisitAll visits the flags in lexicographical order or
+// in primordial order if f.SortFlags is false, calling fn for each.
// It visits all flags, even those not set.
func (f *FlagSet) VisitAll(fn func(*Flag)) {
- for _, flag := range sortFlags(f.formal) {
+ if len(f.formal) == 0 {
+ return
+ }
+
+ var flags []*Flag
+ if f.SortFlags {
+ if len(f.formal) != len(f.sortedFormal) {
+ f.sortedFormal = sortFlags(f.formal)
+ }
+ flags = f.sortedFormal
+ } else {
+ flags = f.orderedFormal
+ }
+
+ for _, flag := range flags {
fn(flag)
}
}
@@ -253,22 +277,39 @@ func (f *FlagSet) HasAvailableFlags() bool {
return false
}
-// VisitAll visits the command-line flags in lexicographical order, calling
-// fn for each. It visits all flags, even those not set.
+// VisitAll visits the command-line flags in lexicographical order or
+// in primordial order if f.SortFlags is false, calling fn for each.
+// It visits all flags, even those not set.
func VisitAll(fn func(*Flag)) {
CommandLine.VisitAll(fn)
}
-// Visit visits the flags in lexicographical order, calling fn for each.
+// Visit visits the flags in lexicographical order or
+// in primordial order if f.SortFlags is false, calling fn for each.
// It visits only those flags that have been set.
func (f *FlagSet) Visit(fn func(*Flag)) {
- for _, flag := range sortFlags(f.actual) {
+ if len(f.actual) == 0 {
+ return
+ }
+
+ var flags []*Flag
+ if f.SortFlags {
+ if len(f.actual) != len(f.sortedActual) {
+ f.sortedActual = sortFlags(f.actual)
+ }
+ flags = f.sortedActual
+ } else {
+ flags = f.orderedActual
+ }
+
+ for _, flag := range flags {
fn(flag)
}
}
-// Visit visits the command-line flags in lexicographical order, calling fn
-// for each. It visits only those flags that have been set.
+// Visit visits the command-line flags in lexicographical order or
+// in primordial order if f.SortFlags is false, calling fn for each.
+// It visits only those flags that have been set.
func Visit(fn func(*Flag)) {
CommandLine.Visit(fn)
}
@@ -373,6 +414,7 @@ func (f *FlagSet) Set(name, value string) error {
f.actual = make(map[NormalizedName]*Flag)
}
f.actual[normalName] = flag
+ f.orderedActual = append(f.orderedActual, flag)
flag.Changed = true
if len(flag.Deprecated) > 0 {
fmt.Fprintf(os.Stderr, "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
@@ -601,7 +643,7 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
line += usage
if !flag.defaultIsZeroValue() {
if flag.Value.Type() == "string" {
- line += fmt.Sprintf(" (default \"%s\")", flag.DefValue)
+ line += fmt.Sprintf(" (default %q)", flag.DefValue)
} else {
line += fmt.Sprintf(" (default %s)", flag.DefValue)
}
@@ -729,6 +771,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
flag.Name = string(normalizedFlagName)
f.formal[normalizedFlagName] = flag
+ f.orderedFormal = append(f.orderedFormal, flag)
if len(flag.Shorthand) == 0 {
return
@@ -807,6 +850,7 @@ func (f *FlagSet) setFlag(flag *Flag, value string, origArg string) error {
f.actual = make(map[NormalizedName]*Flag)
}
f.actual[f.normalizeFlagName(flag.Name)] = flag
+ f.orderedActual = append(f.orderedActual, flag)
flag.Changed = true
if len(flag.Deprecated) > 0 {
fmt.Fprintf(os.Stderr, "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
@@ -1036,14 +1080,15 @@ func Parsed() bool {
// CommandLine is the default set of command-line flags, parsed from os.Args.
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
-// NewFlagSet returns a new, empty flag set with the specified name and
-// error handling property.
+// NewFlagSet returns a new, empty flag set with the specified name,
+// error handling property and SortFlags set to true.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
f := &FlagSet{
name: name,
errorHandling: errorHandling,
argsLenAtDash: -1,
interspersed: true,
+ SortFlags: true,
}
return f
}
diff --git a/vendor/github.com/spf13/pflag/flag_test.go b/vendor/github.com/spf13/pflag/flag_test.go
index b83a0ed6a..172d49fc2 100644
--- a/vendor/github.com/spf13/pflag/flag_test.go
+++ b/vendor/github.com/spf13/pflag/flag_test.go
@@ -335,7 +335,7 @@ func testParse(f *FlagSet, t *testing.T) {
func testParseAll(f *FlagSet, t *testing.T) {
if f.Parsed() {
- fmt.Errorf("f.Parse() = true before Parse")
+ t.Error("f.Parse() = true before Parse")
}
f.BoolP("boola", "a", false, "bool value")
f.BoolP("boolb", "b", false, "bool2 value")
@@ -374,7 +374,7 @@ func testParseAll(f *FlagSet, t *testing.T) {
return nil
}
if err := f.ParseAll(args, store); err != nil {
- t.Errorf("expected no error, got ", err)
+ t.Errorf("expected no error, got %s", err)
}
if !f.Parsed() {
t.Errorf("f.Parse() = false after Parse")
@@ -1004,3 +1004,43 @@ func TestPrintDefaults(t *testing.T) {
t.Errorf("got %q want %q\n", got, defaultOutput)
}
}
+
+func TestVisitAllFlagOrder(t *testing.T) {
+ fs := NewFlagSet("TestVisitAllFlagOrder", ContinueOnError)
+ fs.SortFlags = false
+ // https://github.com/spf13/pflag/issues/120
+ fs.SetNormalizeFunc(func(f *FlagSet, name string) NormalizedName {
+ return NormalizedName(name)
+ })
+
+ names := []string{"C", "B", "A", "D"}
+ for _, name := range names {
+ fs.Bool(name, false, "")
+ }
+
+ i := 0
+ fs.VisitAll(func(f *Flag) {
+ if names[i] != f.Name {
+ t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
+ }
+ i++
+ })
+}
+
+func TestVisitFlagOrder(t *testing.T) {
+ fs := NewFlagSet("TestVisitFlagOrder", ContinueOnError)
+ fs.SortFlags = false
+ names := []string{"C", "B", "A", "D"}
+ for _, name := range names {
+ fs.Bool(name, false, "")
+ fs.Set(name, "true")
+ }
+
+ i := 0
+ fs.Visit(func(f *Flag) {
+ if names[i] != f.Name {
+ t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
+ }
+ i++
+ })
+}