1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 func boolString(s
string) string {
25 func TestEverything(t
*testing
.T
) {
27 Bool("test_bool", false, "bool value")
28 Int("test_int", 0, "int value")
29 Int64("test_int64", 0, "int64 value")
30 Uint("test_uint", 0, "uint value")
31 Uint64("test_uint64", 0, "uint64 value")
32 String("test_string", "0", "string value")
33 Float64("test_float64", 0, "float64 value")
34 Duration("test_duration", 0, "time.Duration value")
36 m
:= make(map[string]*Flag
)
38 visitor
:= func(f
*Flag
) {
39 if len(f
.Name
) > 5 && f
.Name
[0:5] == "test_" {
43 case f
.Value
.String() == desired
:
45 case f
.Name
== "test_bool" && f
.Value
.String() == boolString(desired
):
47 case f
.Name
== "test_duration" && f
.Value
.String() == desired
+"s":
51 t
.Error("Visit: bad value", f
.Value
.String(), "for", f
.Name
)
57 t
.Error("VisitAll misses some flags")
62 m
= make(map[string]*Flag
)
65 t
.Errorf("Visit sees unset flags")
71 Set("test_bool", "true")
73 Set("test_int64", "1")
75 Set("test_uint64", "1")
76 Set("test_string", "1")
77 Set("test_float64", "1")
78 Set("test_duration", "1s")
82 t
.Error("Visit fails after set")
87 // Now test they're visited in sort order.
88 var flagNames
[]string
89 Visit(func(f
*Flag
) { flagNames
= append(flagNames
, f
.Name
) })
90 if !sort
.StringsAreSorted(flagNames
) {
91 t
.Errorf("flag names not sorted: %v", flagNames
)
95 func TestUsage(t
*testing
.T
) {
97 ResetForTesting(func() { called
= true })
98 if CommandLine().Parse([]string{"-x"}) == nil {
99 t
.Error("parse did not fail for unknown flag")
102 t
.Error("did not call Usage for unknown flag")
106 func testParse(f
*FlagSet
, t
*testing
.T
) {
108 t
.Error("f.Parse() = true before Parse")
110 boolFlag
:= f
.Bool("bool", false, "bool value")
111 bool2Flag
:= f
.Bool("bool2", false, "bool2 value")
112 intFlag
:= f
.Int("int", 0, "int value")
113 int64Flag
:= f
.Int64("int64", 0, "int64 value")
114 uintFlag
:= f
.Uint("uint", 0, "uint value")
115 uint64Flag
:= f
.Uint64("uint64", 0, "uint64 value")
116 stringFlag
:= f
.String("string", "0", "string value")
117 float64Flag
:= f
.Float64("float64", 0, "float64 value")
118 durationFlag
:= f
.Duration("duration", 5*time
.Second
, "time.Duration value")
119 extra
:= "one-extra-argument"
128 "-float64", "2718e28",
132 if err
:= f
.Parse(args
); err
!= nil {
136 t
.Error("f.Parse() = false after Parse")
138 if *boolFlag
!= true {
139 t
.Error("bool flag should be true, is ", *boolFlag
)
141 if *bool2Flag
!= true {
142 t
.Error("bool2 flag should be true, is ", *bool2Flag
)
145 t
.Error("int flag should be 22, is ", *intFlag
)
147 if *int64Flag
!= 0x23 {
148 t
.Error("int64 flag should be 0x23, is ", *int64Flag
)
151 t
.Error("uint flag should be 24, is ", *uintFlag
)
153 if *uint64Flag
!= 25 {
154 t
.Error("uint64 flag should be 25, is ", *uint64Flag
)
156 if *stringFlag
!= "hello" {
157 t
.Error("string flag should be `hello`, is ", *stringFlag
)
159 if *float64Flag
!= 2718e28
{
160 t
.Error("float64 flag should be 2718e28, is ", *float64Flag
)
162 if *durationFlag
!= 2*time
.Minute
{
163 t
.Error("duration flag should be 2m, is ", *durationFlag
)
165 if len(f
.Args()) != 1 {
166 t
.Error("expected one argument, got", len(f
.Args()))
167 } else if f
.Args()[0] != extra
{
168 t
.Errorf("expected argument %q got %q", extra
, f
.Args()[0])
172 func TestParse(t
*testing
.T
) {
173 ResetForTesting(func() { t
.Error("bad parse") })
174 testParse(CommandLine(), t
)
177 func TestFlagSetParse(t
*testing
.T
) {
178 testParse(NewFlagSet("test", ContinueOnError
), t
)
181 // Declare a user-defined flag type.
182 type flagVar
[]string
184 func (f
*flagVar
) String() string {
185 return fmt
.Sprint([]string(*f
))
188 func (f
*flagVar
) Set(value
string) error
{
189 *f
= append(*f
, value
)
193 func TestUserDefined(t
*testing
.T
) {
195 flags
.Init("test", ContinueOnError
)
197 flags
.Var(&v
, "v", "usage")
198 if err
:= flags
.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err
!= nil {
202 t
.Fatal("expected 3 args; got ", len(v
))
205 if v
.String() != expect
{
206 t
.Errorf("expected value %q got %q", expect
, v
.String())
210 // Declare a user-defined boolean flag type.
211 type boolFlagVar
struct {
215 func (b
*boolFlagVar
) String() string {
216 return fmt
.Sprintf("%d", b
.count
)
219 func (b
*boolFlagVar
) Set(value
string) error
{
226 func (b
*boolFlagVar
) IsBoolFlag() bool {
230 func TestUserDefinedBool(t
*testing
.T
) {
232 flags
.Init("test", ContinueOnError
)
235 flags
.Var(&b
, "b", "usage")
236 if err
= flags
.Parse([]string{"-b", "-b", "-b", "-b=true", "-b=false", "-b", "barg", "-b"}); err
!= nil {
243 t
.Errorf("want: %d; got: %d", 4, b
.count
)
247 t
.Error("expected error; got none")
251 func TestSetOutput(t
*testing
.T
) {
254 flags
.SetOutput(&buf
)
255 flags
.Init("test", ContinueOnError
)
256 flags
.Parse([]string{"-unknown"})
257 if out
:= buf
.String(); !strings
.Contains(out
, "-unknown") {
258 t
.Logf("expected output mentioning unknown; got %q", out
)
262 // This tests that one can reset the flags. This still works but not well, and is
263 // superseded by FlagSet.
264 func TestChangingArgs(t
*testing
.T
) {
265 ResetForTesting(func() { t
.Fatal("bad parse") })
267 defer func() { os
.Args
= oldArgs
}()
268 os
.Args
= []string{"cmd", "-before", "subcmd", "-after", "args"}
269 before
:= Bool("before", false, "")
270 if err
:= CommandLine().Parse(os
.Args
[1:]); err
!= nil {
275 after
:= Bool("after", false, "")
279 if !*before || cmd
!= "subcmd" ||
!*after ||
len(args
) != 1 || args
[0] != "args" {
280 t
.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before
, cmd
, *after
, args
)
284 // Test that -help invokes the usage message and returns ErrHelp.
285 func TestHelp(t
*testing
.T
) {
286 var helpCalled
= false
287 fs
:= NewFlagSet("help test", ContinueOnError
)
288 fs
.Usage
= func() { helpCalled
= true }
290 fs
.BoolVar(&flag
, "flag", false, "regular flag")
291 // Regular flag invocation should work
292 err
:= fs
.Parse([]string{"-flag=true"})
294 t
.Fatal("expected no error; got ", err
)
297 t
.Error("flag was not set by -flag")
300 t
.Error("help called for regular flag")
301 helpCalled
= false // reset for next test
303 // Help flag should work as expected.
304 err
= fs
.Parse([]string{"-help"})
306 t
.Fatal("error expected")
309 t
.Fatal("expected ErrHelp; got ", err
)
312 t
.Fatal("help was not called")
314 // If we define a help flag, that should override.
316 fs
.BoolVar(&help
, "help", false, "help flag")
318 err
= fs
.Parse([]string{"-help"})
320 t
.Fatal("expected no error for defined -help; got ", err
)
323 t
.Fatal("help was called; should not have been for defined help flag")