-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_flag_value_options_test.go
97 lines (90 loc) · 2.22 KB
/
example_flag_value_options_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package warg_test
import (
"fmt"
"log"
"os"
"go.bbkane.com/warg"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/config/yamlreader"
"go.bbkane.com/warg/flag"
"go.bbkane.com/warg/section"
"go.bbkane.com/warg/value/scalar"
"go.bbkane.com/warg/value/slice"
)
// ExampleApp_Parse_flag_value_options shows a couple combinations of flag/value options.
// It's also possible to use '--help detailed' to see the current value of a flag and what set it.
func ExampleApp_Parse_flag_value_options() {
action := func(ctx command.Context) error {
// flag marked as Required(), so no need to check for existance
scalarVal := ctx.Flags["--scalar-flag"].(string)
// flag might not exist in config, so check for existance
// TODO: does this panic on nil?
sliceVal, sliceValExists := ctx.Flags["--slice-flag"].([]int)
fmt.Printf("--scalar-flag: %#v\n", scalarVal)
if sliceValExists {
fmt.Printf("--slice-flag: %#v\n", sliceVal)
} else {
fmt.Printf("--slice-flag value not filled!\n")
}
return nil
}
app := warg.New(
"flag-overrides",
section.New(
"demo flag overrides",
section.Command(
command.Name("show"),
"Show final flag values",
action,
command.Flag(
"--scalar-flag",
"Demo scalar flag",
scalar.String(
scalar.Choices("a", "b"),
scalar.Default("a"),
),
flag.ConfigPath("args.scalar-flag"),
flag.Required(),
),
command.Flag(
"--slice-flag",
"Demo slice flag",
slice.Int(
slice.Choices(1, 2, 3),
),
flag.Alias("-slice"),
flag.ConfigPath("args.slice-flag"),
flag.EnvVars("SLICE", "SLICE_ARG"),
),
),
),
warg.ConfigFlag(
"--config",
[]scalar.ScalarOpt[string]{
scalar.Default("~/.config/flag-overrides.yaml"),
},
yamlreader.New,
"path to YAML config file",
flag.Alias("-c"),
),
)
err := os.WriteFile(
"testdata/ExampleFlagValueOptions/config.yaml",
[]byte(`args:
slice-flag:
- 1
- 2
- 3
`),
0644,
)
if err != nil {
log.Fatalf("write error: %e", err)
}
app.MustRun(
warg.OverrideArgs([]string{"calc", "-c", "testdata/ExampleFlagValueOptions/config.yaml", "show", "--scalar-flag", "b"}),
)
// Output:
// --scalar-flag: "b"
// --slice-flag: []int{1, 2, 3}
}