...

Source file src/github.com/spf13/pflag/golangflag_test.go

Documentation: github.com/spf13/pflag

     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.
     4  
     5  package pflag
     6  
     7  import (
     8  	goflag "flag"
     9  	"testing"
    10  )
    11  
    12  func TestGoflags(t *testing.T) {
    13  	goflag.String("stringFlag", "stringFlag", "stringFlag")
    14  	goflag.Bool("boolFlag", false, "boolFlag")
    15  
    16  	f := NewFlagSet("test", ContinueOnError)
    17  
    18  	f.AddGoFlagSet(goflag.CommandLine)
    19  	err := f.Parse([]string{"--stringFlag=bob", "--boolFlag"})
    20  	if err != nil {
    21  		t.Fatal("expected no error; get", err)
    22  	}
    23  
    24  	getString, err := f.GetString("stringFlag")
    25  	if err != nil {
    26  		t.Fatal("expected no error; get", err)
    27  	}
    28  	if getString != "bob" {
    29  		t.Fatalf("expected getString=bob but got getString=%s", getString)
    30  	}
    31  
    32  	getBool, err := f.GetBool("boolFlag")
    33  	if err != nil {
    34  		t.Fatal("expected no error; get", err)
    35  	}
    36  	if getBool != true {
    37  		t.Fatalf("expected getBool=true but got getBool=%v", getBool)
    38  	}
    39  	if !f.Parsed() {
    40  		t.Fatal("f.Parsed() return false after f.Parse() called")
    41  	}
    42  
    43  	// in fact it is useless. because `go test` called flag.Parse()
    44  	if !goflag.CommandLine.Parsed() {
    45  		t.Fatal("goflag.CommandLine.Parsed() return false after f.Parse() called")
    46  	}
    47  }
    48  

View as plain text