MCPcopy
hub / github.com/spf13/pflag / TestGoflags

Function TestGoflags

golangflag_test.go:13–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestGoflags(t *testing.T) {
14 goflag.String("stringFlag", "stringFlag", "stringFlag")
15 goflag.Bool("boolFlag", false, "boolFlag")
16 var testxxxValue string
17 goflag.StringVar(&testxxxValue, "test.xxx", "test.xxx", "it is a test flag")
18
19 f := NewFlagSet("test", ContinueOnError)
20
21 f.AddGoFlagSet(goflag.CommandLine)
22 args := []string{"--stringFlag=bob", "--boolFlag", "-test.xxx=testvalue"}
23 err := f.Parse(args)
24 if err != nil {
25 t.Fatal("expected no error; get", err)
26 }
27
28 getString, err := f.GetString("stringFlag")
29 if err != nil {
30 t.Fatal("expected no error; get", err)
31 }
32 if getString != "bob" {
33 t.Fatalf("expected getString=bob but got getString=%s", getString)
34 }
35
36 getBool, err := f.GetBool("boolFlag")
37 if err != nil {
38 t.Fatal("expected no error; get", err)
39 }
40 if getBool != true {
41 t.Fatalf("expected getBool=true but got getBool=%v", getBool)
42 }
43 if !f.Parsed() {
44 t.Fatal("f.Parsed() return false after f.Parse() called")
45 }
46
47 if testxxxValue != "test.xxx" {
48 t.Fatalf("expected testxxxValue to be test.xxx but got %v", testxxxValue)
49 }
50 err = ParseSkippedFlags(args, goflag.CommandLine)
51 if err != nil {
52 t.Fatal("expected no error; ParseSkippedFlags", err)
53 }
54 if testxxxValue != "testvalue" {
55 t.Fatalf("expected testxxxValue to be testvalue but got %v", testxxxValue)
56 }
57
58 // in fact it is useless. because `go test` called flag.Parse()
59 if !goflag.CommandLine.Parsed() {
60 t.Fatal("goflag.CommandLine.Parsed() return false after f.Parse() called")
61 }
62}
63
64func TestToGoflags(t *testing.T) {
65 pfs := FlagSet{}

Callers

nothing calls this directly

Calls 10

AddGoFlagSetMethod · 0.95
ParseMethod · 0.95
GetStringMethod · 0.95
GetBoolMethod · 0.95
ParsedMethod · 0.95
NewFlagSetFunction · 0.85
ParseSkippedFlagsFunction · 0.85
BoolMethod · 0.80
StringVarMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected