1
0

golangflag_test.go 1.2 KB

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