Merge branch 'main' into duration-default-regression

This commit is contained in:
Dan Buch 2022-04-25 22:20:14 -04:00 committed by GitHub
commit 8fe4d792d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 30 deletions

22
flag.go
View File

@ -5,7 +5,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"reflect"
"regexp" "regexp"
"runtime" "runtime"
"strconv" "strconv"
@ -244,7 +243,7 @@ func prefixedNames(names []string, placeholder string) string {
func withEnvHint(envVars []string, str string) string { func withEnvHint(envVars []string, str string) string {
envText := "" envText := ""
if envVars != nil && len(envVars) > 0 { if len(envVars) > 0 {
prefix := "$" prefix := "$"
suffix := "" suffix := ""
sep := ", $" sep := ", $"
@ -273,17 +272,6 @@ func flagNames(name string, aliases []string) []string {
return ret return ret
} }
func flagStringSliceField(f Flag, name string) []string {
fv := flagValue(f)
field := fv.FieldByName(name)
if field.IsValid() {
return field.Interface().([]string)
}
return []string{}
}
func withFileHint(filePath, str string) string { func withFileHint(filePath, str string) string {
fileText := "" fileText := ""
if filePath != "" { if filePath != "" {
@ -292,14 +280,6 @@ func withFileHint(filePath, str string) string {
return str + fileText return str + fileText
} }
func flagValue(f Flag) reflect.Value {
fv := reflect.ValueOf(f)
for fv.Kind() == reflect.Ptr {
fv = reflect.Indirect(fv)
}
return fv
}
func formatDefault(format string) string { func formatDefault(format string) string {
return " (default: " + format + ")" return " (default: " + format + ")"
} }

View File

@ -400,7 +400,7 @@ func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
var prefixStringFlagTests = []struct { var _ = []struct {
name string name string
aliases []string aliases []string
usage string usage string
@ -490,7 +490,7 @@ func TestPathFlagApply_SetsAllNames(t *testing.T) {
expect(t, v, "/path/to/file/PATH") expect(t, v, "/path/to/file/PATH")
} }
var envHintFlagTests = []struct { var _ = []struct {
name string name string
env string env string
hinter FlagEnvHintFunc hinter FlagEnvHintFunc
@ -2174,43 +2174,43 @@ type flagDefaultTestCase struct {
func TestFlagDefaultValue(t *testing.T) { func TestFlagDefaultValue(t *testing.T) {
cases := []*flagDefaultTestCase{ cases := []*flagDefaultTestCase{
&flagDefaultTestCase{ {
name: "stringSclice", name: "stringSclice",
flag: &StringSliceFlag{Name: "flag", Value: NewStringSlice("default1", "default2")}, flag: &StringSliceFlag{Name: "flag", Value: NewStringSlice("default1", "default2")},
toParse: []string{"--flag", "parsed"}, toParse: []string{"--flag", "parsed"},
expect: `--flag value (default: "default1", "default2") (accepts multiple inputs)`, expect: `--flag value (default: "default1", "default2") (accepts multiple inputs)`,
}, },
&flagDefaultTestCase{ {
name: "float64Sclice", name: "float64Sclice",
flag: &Float64SliceFlag{Name: "flag", Value: NewFloat64Slice(1.1, 2.2)}, flag: &Float64SliceFlag{Name: "flag", Value: NewFloat64Slice(1.1, 2.2)},
toParse: []string{"--flag", "13.3"}, toParse: []string{"--flag", "13.3"},
expect: `--flag value (default: 1.1, 2.2) (accepts multiple inputs)`, expect: `--flag value (default: 1.1, 2.2) (accepts multiple inputs)`,
}, },
&flagDefaultTestCase{ {
name: "int64Sclice", name: "int64Sclice",
flag: &Int64SliceFlag{Name: "flag", Value: NewInt64Slice(1, 2)}, flag: &Int64SliceFlag{Name: "flag", Value: NewInt64Slice(1, 2)},
toParse: []string{"--flag", "13"}, toParse: []string{"--flag", "13"},
expect: `--flag value (default: 1, 2) (accepts multiple inputs)`, expect: `--flag value (default: 1, 2) (accepts multiple inputs)`,
}, },
&flagDefaultTestCase{ {
name: "intSclice", name: "intSclice",
flag: &IntSliceFlag{Name: "flag", Value: NewIntSlice(1, 2)}, flag: &IntSliceFlag{Name: "flag", Value: NewIntSlice(1, 2)},
toParse: []string{"--flag", "13"}, toParse: []string{"--flag", "13"},
expect: `--flag value (default: 1, 2) (accepts multiple inputs)`, expect: `--flag value (default: 1, 2) (accepts multiple inputs)`,
}, },
&flagDefaultTestCase{ {
name: "string", name: "string",
flag: &StringFlag{Name: "flag", Value: "default"}, flag: &StringFlag{Name: "flag", Value: "default"},
toParse: []string{"--flag", "parsed"}, toParse: []string{"--flag", "parsed"},
expect: `--flag value (default: "default")`, expect: `--flag value (default: "default")`,
}, },
&flagDefaultTestCase{ {
name: "bool", name: "bool",
flag: &BoolFlag{Name: "flag", Value: true}, flag: &BoolFlag{Name: "flag", Value: true},
toParse: []string{"--flag", "false"}, toParse: []string{"--flag", "false"},
expect: `--flag (default: true)`, expect: `--flag (default: true)`,
}, },
&flagDefaultTestCase{ {
name: "uint64", name: "uint64",
flag: &Uint64Flag{Name: "flag", Value: 1}, flag: &Uint64Flag{Name: "flag", Value: 1},
toParse: []string{"--flag", "13"}, toParse: []string{"--flag", "13"},