зеркало из https://github.com/microsoft/docker.git
fix panic in mflag
Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Родитель
5258f833bc
Коммит
65794a2c49
|
@ -8,11 +8,12 @@ import (
|
||||||
var (
|
var (
|
||||||
i int
|
i int
|
||||||
str string
|
str string
|
||||||
b, h bool
|
b, b2, h bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.BoolVar(&b, []string{"b"}, false, "a simple bool")
|
flag.BoolVar(&b, []string{"b"}, false, "a simple bool")
|
||||||
|
flag.BoolVar(&b2, []string{"-bool"}, false, "a simple bool")
|
||||||
flag.IntVar(&i, []string{"#integer", "-integer"}, -1, "a simple integer")
|
flag.IntVar(&i, []string{"#integer", "-integer"}, -1, "a simple integer")
|
||||||
flag.StringVar(&str, []string{"s", "#hidden", "-string"}, "", "a simple string") //-s -hidden and --string will work, but -hidden won't be in the usage
|
flag.StringVar(&str, []string{"s", "#hidden", "-string"}, "", "a simple string") //-s -hidden and --string will work, but -hidden won't be in the usage
|
||||||
flag.BoolVar(&h, []string{"h", "#help", "-help"}, false, "display the help")
|
flag.BoolVar(&h, []string{"h", "#help", "-help"}, false, "display the help")
|
||||||
|
@ -22,6 +23,8 @@ func main() {
|
||||||
if h {
|
if h {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", str)
|
fmt.Printf("s/#hidden/-string: %s\n", str)
|
||||||
fmt.Printf("%s\n", flag.Lookup("s").Value.String())
|
fmt.Printf("b: %b\n", b)
|
||||||
|
fmt.Printf("-bool: %b\n", b2)
|
||||||
|
fmt.Printf("s/#hidden/-string(via lookup): %s\n", flag.Lookup("s").Value.String())
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,6 +287,11 @@ type Flag struct {
|
||||||
func sortFlags(flags map[string]*Flag) []*Flag {
|
func sortFlags(flags map[string]*Flag) []*Flag {
|
||||||
var list sort.StringSlice
|
var list sort.StringSlice
|
||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
|
if len(f.Names) == 1 {
|
||||||
|
list = append(list, f.Names[0])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
fName := strings.TrimPrefix(strings.TrimPrefix(f.Names[0], "#"), "-")
|
fName := strings.TrimPrefix(strings.TrimPrefix(f.Names[0], "#"), "-")
|
||||||
for _, name := range list {
|
for _, name := range list {
|
||||||
|
|
|
@ -228,6 +228,22 @@ func testParse(f *FlagSet, t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPanic(f *FlagSet, t *testing.T) {
|
||||||
|
f.Int([]string{"-int"}, 0, "int value")
|
||||||
|
if f.Parsed() {
|
||||||
|
t.Error("f.Parse() = true before Parse")
|
||||||
|
}
|
||||||
|
args := []string{
|
||||||
|
"-int", "21",
|
||||||
|
}
|
||||||
|
f.Parse(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsePanic(t *testing.T) {
|
||||||
|
ResetForTesting(func() {})
|
||||||
|
testPanic(CommandLine, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
ResetForTesting(func() { t.Error("bad parse") })
|
ResetForTesting(func() { t.Error("bad parse") })
|
||||||
testParse(CommandLine, t)
|
testParse(CommandLine, t)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче