2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package flag_test
|
|
|
|
|
|
|
|
import (
|
2012-02-09 09:19:58 +01:00
|
|
|
"bytes"
|
2010-12-03 05:34:57 +01:00
|
|
|
. "flag"
|
|
|
|
"fmt"
|
2011-01-21 19:19:03 +01:00
|
|
|
"os"
|
2011-03-25 00:46:17 +01:00
|
|
|
"sort"
|
2012-02-09 09:19:58 +01:00
|
|
|
"strings"
|
2010-12-03 05:34:57 +01:00
|
|
|
"testing"
|
2012-01-25 21:56:26 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func boolString(s string) string {
|
|
|
|
if s == "0" {
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEverything(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
ResetForTesting(nil)
|
|
|
|
Bool("test_bool", false, "bool value")
|
|
|
|
Int("test_int", 0, "int value")
|
|
|
|
Int64("test_int64", 0, "int64 value")
|
|
|
|
Uint("test_uint", 0, "uint value")
|
|
|
|
Uint64("test_uint64", 0, "uint64 value")
|
|
|
|
String("test_string", "0", "string value")
|
|
|
|
Float64("test_float64", 0, "float64 value")
|
|
|
|
Duration("test_duration", 0, "time.Duration value")
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
m := make(map[string]*Flag)
|
|
|
|
desired := "0"
|
|
|
|
visitor := func(f *Flag) {
|
|
|
|
if len(f.Name) > 5 && f.Name[0:5] == "test_" {
|
|
|
|
m[f.Name] = f
|
|
|
|
ok := false
|
|
|
|
switch {
|
|
|
|
case f.Value.String() == desired:
|
|
|
|
ok = true
|
|
|
|
case f.Name == "test_bool" && f.Value.String() == boolString(desired):
|
|
|
|
ok = true
|
2012-01-25 21:56:26 +01:00
|
|
|
case f.Name == "test_duration" && f.Value.String() == desired+"s":
|
|
|
|
ok = true
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VisitAll(visitor)
|
2012-01-25 21:56:26 +01:00
|
|
|
if len(m) != 8 {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Error("VisitAll misses some flags")
|
|
|
|
for k, v := range m {
|
|
|
|
t.Log(k, *v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = make(map[string]*Flag)
|
|
|
|
Visit(visitor)
|
|
|
|
if len(m) != 0 {
|
|
|
|
t.Errorf("Visit sees unset flags")
|
|
|
|
for k, v := range m {
|
|
|
|
t.Log(k, *v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now set all flags
|
|
|
|
Set("test_bool", "true")
|
|
|
|
Set("test_int", "1")
|
|
|
|
Set("test_int64", "1")
|
|
|
|
Set("test_uint", "1")
|
|
|
|
Set("test_uint64", "1")
|
|
|
|
Set("test_string", "1")
|
|
|
|
Set("test_float64", "1")
|
2012-01-25 21:56:26 +01:00
|
|
|
Set("test_duration", "1s")
|
2010-12-03 05:34:57 +01:00
|
|
|
desired = "1"
|
|
|
|
Visit(visitor)
|
2012-01-25 21:56:26 +01:00
|
|
|
if len(m) != 8 {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Error("Visit fails after set")
|
|
|
|
for k, v := range m {
|
|
|
|
t.Log(k, *v)
|
|
|
|
}
|
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
// Now test they're visited in sort order.
|
|
|
|
var flagNames []string
|
|
|
|
Visit(func(f *Flag) { flagNames = append(flagNames, f.Name) })
|
|
|
|
if !sort.StringsAreSorted(flagNames) {
|
|
|
|
t.Errorf("flag names not sorted: %v", flagNames)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
ResetForTesting(nil)
|
|
|
|
Bool("test_bool", true, "bool value")
|
|
|
|
Int("test_int", 1, "int value")
|
|
|
|
Int64("test_int64", 2, "int64 value")
|
|
|
|
Uint("test_uint", 3, "uint value")
|
|
|
|
Uint64("test_uint64", 4, "uint64 value")
|
|
|
|
String("test_string", "5", "string value")
|
|
|
|
Float64("test_float64", 6, "float64 value")
|
|
|
|
Duration("test_duration", 7, "time.Duration value")
|
|
|
|
|
|
|
|
visitor := func(f *Flag) {
|
|
|
|
if len(f.Name) > 5 && f.Name[0:5] == "test_" {
|
|
|
|
g, ok := f.Value.(Getter)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Visit: value does not satisfy Getter: %T", f.Value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch f.Name {
|
|
|
|
case "test_bool":
|
|
|
|
ok = g.Get() == true
|
|
|
|
case "test_int":
|
|
|
|
ok = g.Get() == int(1)
|
|
|
|
case "test_int64":
|
|
|
|
ok = g.Get() == int64(2)
|
|
|
|
case "test_uint":
|
|
|
|
ok = g.Get() == uint(3)
|
|
|
|
case "test_uint64":
|
|
|
|
ok = g.Get() == uint64(4)
|
|
|
|
case "test_string":
|
|
|
|
ok = g.Get() == "5"
|
|
|
|
case "test_float64":
|
|
|
|
ok = g.Get() == float64(6)
|
|
|
|
case "test_duration":
|
|
|
|
ok = g.Get() == time.Duration(7)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Visit: bad value %T(%v) for %s", g.Get(), g.Get(), f.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VisitAll(visitor)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func TestUsage(t *testing.T) {
|
|
|
|
called := false
|
|
|
|
ResetForTesting(func() { called = true })
|
2013-11-06 20:49:01 +01:00
|
|
|
if CommandLine.Parse([]string{"-x"}) == nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Error("parse did not fail for unknown flag")
|
|
|
|
}
|
|
|
|
if !called {
|
|
|
|
t.Error("did not call Usage for unknown flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func testParse(f *FlagSet, t *testing.T) {
|
2011-10-27 01:57:58 +02:00
|
|
|
if f.Parsed() {
|
|
|
|
t.Error("f.Parse() = true before Parse")
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
boolFlag := f.Bool("bool", false, "bool value")
|
|
|
|
bool2Flag := f.Bool("bool2", false, "bool2 value")
|
|
|
|
intFlag := f.Int("int", 0, "int value")
|
|
|
|
int64Flag := f.Int64("int64", 0, "int64 value")
|
|
|
|
uintFlag := f.Uint("uint", 0, "uint value")
|
|
|
|
uint64Flag := f.Uint64("uint64", 0, "uint64 value")
|
|
|
|
stringFlag := f.String("string", "0", "string value")
|
|
|
|
float64Flag := f.Float64("float64", 0, "float64 value")
|
2012-01-25 21:56:26 +01:00
|
|
|
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
|
2010-12-03 05:34:57 +01:00
|
|
|
extra := "one-extra-argument"
|
|
|
|
args := []string{
|
|
|
|
"-bool",
|
|
|
|
"-bool2=true",
|
|
|
|
"--int", "22",
|
2011-03-17 00:05:44 +01:00
|
|
|
"--int64", "0x23",
|
2010-12-03 05:34:57 +01:00
|
|
|
"-uint", "24",
|
|
|
|
"--uint64", "25",
|
|
|
|
"-string", "hello",
|
|
|
|
"-float64", "2718e28",
|
2012-01-25 21:56:26 +01:00
|
|
|
"-duration", "2m",
|
2010-12-03 05:34:57 +01:00
|
|
|
extra,
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
t.Fatal(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if !f.Parsed() {
|
|
|
|
t.Error("f.Parse() = false after Parse")
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
if *boolFlag != true {
|
|
|
|
t.Error("bool flag should be true, is ", *boolFlag)
|
|
|
|
}
|
|
|
|
if *bool2Flag != true {
|
|
|
|
t.Error("bool2 flag should be true, is ", *bool2Flag)
|
|
|
|
}
|
|
|
|
if *intFlag != 22 {
|
|
|
|
t.Error("int flag should be 22, is ", *intFlag)
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
if *int64Flag != 0x23 {
|
|
|
|
t.Error("int64 flag should be 0x23, is ", *int64Flag)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if *uintFlag != 24 {
|
|
|
|
t.Error("uint flag should be 24, is ", *uintFlag)
|
|
|
|
}
|
|
|
|
if *uint64Flag != 25 {
|
|
|
|
t.Error("uint64 flag should be 25, is ", *uint64Flag)
|
|
|
|
}
|
|
|
|
if *stringFlag != "hello" {
|
|
|
|
t.Error("string flag should be `hello`, is ", *stringFlag)
|
|
|
|
}
|
|
|
|
if *float64Flag != 2718e28 {
|
|
|
|
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
if *durationFlag != 2*time.Minute {
|
|
|
|
t.Error("duration flag should be 2m, is ", *durationFlag)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if len(f.Args()) != 1 {
|
|
|
|
t.Error("expected one argument, got", len(f.Args()))
|
|
|
|
} else if f.Args()[0] != extra {
|
|
|
|
t.Errorf("expected argument %q got %q", extra, f.Args()[0])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
ResetForTesting(func() { t.Error("bad parse") })
|
2013-11-06 20:49:01 +01:00
|
|
|
testParse(CommandLine, t)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagSetParse(t *testing.T) {
|
|
|
|
testParse(NewFlagSet("test", ContinueOnError), t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a user-defined flag type.
|
2010-12-03 05:34:57 +01:00
|
|
|
type flagVar []string
|
|
|
|
|
|
|
|
func (f *flagVar) String() string {
|
|
|
|
return fmt.Sprint([]string(*f))
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (f *flagVar) Set(value string) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
*f = append(*f, value)
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserDefined(t *testing.T) {
|
2011-10-27 01:57:58 +02:00
|
|
|
var flags FlagSet
|
|
|
|
flags.Init("test", ContinueOnError)
|
2010-12-03 05:34:57 +01:00
|
|
|
var v flagVar
|
2011-09-16 17:47:21 +02:00
|
|
|
flags.Var(&v, "v", "usage")
|
|
|
|
if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil {
|
|
|
|
t.Error(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if len(v) != 3 {
|
|
|
|
t.Fatal("expected 3 args; got ", len(v))
|
|
|
|
}
|
|
|
|
expect := "[1 2 3]"
|
|
|
|
if v.String() != expect {
|
|
|
|
t.Errorf("expected value %q got %q", expect, v.String())
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
func TestUserDefinedForCommandLine(t *testing.T) {
|
|
|
|
const help = "HELP"
|
|
|
|
var result string
|
|
|
|
ResetForTesting(func() { result = help })
|
|
|
|
Usage()
|
|
|
|
if result != help {
|
|
|
|
t.Fatalf("got %q; expected %q", result, help)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Declare a user-defined boolean flag type.
|
|
|
|
type boolFlagVar struct {
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolFlagVar) String() string {
|
|
|
|
return fmt.Sprintf("%d", b.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolFlagVar) Set(value string) error {
|
|
|
|
if value == "true" {
|
|
|
|
b.count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolFlagVar) IsBoolFlag() bool {
|
|
|
|
return b.count < 4
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserDefinedBool(t *testing.T) {
|
|
|
|
var flags FlagSet
|
|
|
|
flags.Init("test", ContinueOnError)
|
|
|
|
var b boolFlagVar
|
|
|
|
var err error
|
|
|
|
flags.Var(&b, "b", "usage")
|
|
|
|
if err = flags.Parse([]string{"-b", "-b", "-b", "-b=true", "-b=false", "-b", "barg", "-b"}); err != nil {
|
|
|
|
if b.count < 4 {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.count != 4 {
|
|
|
|
t.Errorf("want: %d; got: %d", 4, b.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error; got none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func TestSetOutput(t *testing.T) {
|
|
|
|
var flags FlagSet
|
|
|
|
var buf bytes.Buffer
|
|
|
|
flags.SetOutput(&buf)
|
|
|
|
flags.Init("test", ContinueOnError)
|
|
|
|
flags.Parse([]string{"-unknown"})
|
|
|
|
if out := buf.String(); !strings.Contains(out, "-unknown") {
|
|
|
|
t.Logf("expected output mentioning unknown; got %q", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// This tests that one can reset the flags. This still works but not well, and is
|
|
|
|
// superseded by FlagSet.
|
2011-01-21 19:19:03 +01:00
|
|
|
func TestChangingArgs(t *testing.T) {
|
|
|
|
ResetForTesting(func() { t.Fatal("bad parse") })
|
|
|
|
oldArgs := os.Args
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
os.Args = []string{"cmd", "-before", "subcmd", "-after", "args"}
|
|
|
|
before := Bool("before", false, "")
|
2013-11-06 20:49:01 +01:00
|
|
|
if err := CommandLine.Parse(os.Args[1:]); err != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
cmd := Arg(0)
|
|
|
|
os.Args = Args()
|
|
|
|
after := Bool("after", false, "")
|
|
|
|
Parse()
|
|
|
|
args := Args()
|
|
|
|
|
|
|
|
if !*before || cmd != "subcmd" || !*after || len(args) != 1 || args[0] != "args" {
|
|
|
|
t.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before, cmd, *after, args)
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
// Test that -help invokes the usage message and returns ErrHelp.
|
|
|
|
func TestHelp(t *testing.T) {
|
|
|
|
var helpCalled = false
|
|
|
|
fs := NewFlagSet("help test", ContinueOnError)
|
|
|
|
fs.Usage = func() { helpCalled = true }
|
|
|
|
var flag bool
|
|
|
|
fs.BoolVar(&flag, "flag", false, "regular flag")
|
|
|
|
// Regular flag invocation should work
|
|
|
|
err := fs.Parse([]string{"-flag=true"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error; got ", err)
|
|
|
|
}
|
|
|
|
if !flag {
|
|
|
|
t.Error("flag was not set by -flag")
|
|
|
|
}
|
|
|
|
if helpCalled {
|
|
|
|
t.Error("help called for regular flag")
|
|
|
|
helpCalled = false // reset for next test
|
|
|
|
}
|
|
|
|
// Help flag should work as expected.
|
|
|
|
err = fs.Parse([]string{"-help"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
|
|
|
if err != ErrHelp {
|
|
|
|
t.Fatal("expected ErrHelp; got ", err)
|
|
|
|
}
|
|
|
|
if !helpCalled {
|
|
|
|
t.Fatal("help was not called")
|
|
|
|
}
|
|
|
|
// If we define a help flag, that should override.
|
|
|
|
var help bool
|
|
|
|
fs.BoolVar(&help, "help", false, "help flag")
|
|
|
|
helpCalled = false
|
|
|
|
err = fs.Parse([]string{"-help"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error for defined -help; got ", err)
|
|
|
|
}
|
|
|
|
if helpCalled {
|
|
|
|
t.Fatal("help was called; should not have been for defined help flag")
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
const defaultOutput = ` -A for bootstrapping, allow 'any' type
|
|
|
|
-Alongflagname
|
|
|
|
disable bounds checking
|
|
|
|
-C a boolean defaulting to true (default true)
|
|
|
|
-D path
|
|
|
|
set relative path for local imports
|
|
|
|
-F number
|
|
|
|
a non-zero number (default 2.7)
|
|
|
|
-G float
|
|
|
|
a float that defaults to zero
|
|
|
|
-N int
|
|
|
|
a non-zero int (default 27)
|
|
|
|
-Z int
|
|
|
|
an int that defaults to zero
|
|
|
|
-maxT timeout
|
|
|
|
set timeout for dial
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestPrintDefaults(t *testing.T) {
|
|
|
|
fs := NewFlagSet("print defaults test", ContinueOnError)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fs.SetOutput(&buf)
|
|
|
|
fs.Bool("A", false, "for bootstrapping, allow 'any' type")
|
|
|
|
fs.Bool("Alongflagname", false, "disable bounds checking")
|
|
|
|
fs.Bool("C", true, "a boolean defaulting to true")
|
|
|
|
fs.String("D", "", "set relative `path` for local imports")
|
|
|
|
fs.Float64("F", 2.7, "a non-zero `number`")
|
|
|
|
fs.Float64("G", 0, "a float that defaults to zero")
|
|
|
|
fs.Int("N", 27, "a non-zero int")
|
|
|
|
fs.Int("Z", 0, "an int that defaults to zero")
|
|
|
|
fs.Duration("maxT", 0, "set `timeout` for dial")
|
|
|
|
fs.PrintDefaults()
|
|
|
|
got := buf.String()
|
|
|
|
if got != defaultOutput {
|
|
|
|
t.Errorf("got %q want %q\n", got, defaultOutput)
|
|
|
|
}
|
|
|
|
}
|