1 // Copyright 2017 Frank Schroeder. 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.
13 // TestFlag verifies Properties.MustFlag without flag.FlagSet.Parse
14 func TestFlag(t *testing.T) {
15 f := flag.NewFlagSet("src", flag.PanicOnError)
16 gotS := f.String("s", "?", "string flag")
17 gotI := f.Int("i", -1, "int flag")
24 if want := "t"; *gotS != want {
25 t.Errorf("Got string s=%q, want %q", *gotS, want)
27 if want := 9; *gotI != want {
28 t.Errorf("Got int i=%d, want %d", *gotI, want)
32 // TestFlagOverride verifies Properties.MustFlag with flag.FlagSet.Parse.
33 func TestFlagOverride(t *testing.T) {
34 f := flag.NewFlagSet("src", flag.PanicOnError)
35 gotA := f.Int("a", 1, "remain default")
36 gotB := f.Int("b", 2, "customized")
37 gotC := f.Int("c", 3, "overridden")
39 if err := f.Parse([]string{"-c", "4"}); err != nil {
48 if want := 1; *gotA != want {
49 t.Errorf("Got remain default a=%d, want %d", *gotA, want)
51 if want := 5; *gotB != want {
52 t.Errorf("Got customized b=%d, want %d", *gotB, want)
54 if want := 4; *gotC != want {
55 t.Errorf("Got overriden c=%d, want %d", *gotC, want)
59 func ExampleProperties_MustFlag() {
60 x := flag.Int("x", 0, "demo customize")
61 y := flag.Int("y", 0, "demo override")
63 // Demo alternative for flag.Parse():
64 flag.CommandLine.Parse([]string{"-y", "10"})
65 fmt.Printf("flagged as x=%d, y=%d\n", *x, *y)
69 p.MustSet("y", "42") // note discard
70 p.MustFlag(flag.CommandLine)
71 fmt.Printf("configured to x=%d, y=%d\n", *x, *y)
74 // flagged as x=0, y=10
75 // configured to x=7, y=10