-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_config_flag_test.go
More file actions
76 lines (71 loc) · 1.3 KB
/
example_config_flag_test.go
File metadata and controls
76 lines (71 loc) · 1.3 KB
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
package warg_test
import (
"fmt"
"log"
"os"
"go.bbkane.com/warg"
"go.bbkane.com/warg/config/yamlreader"
"go.bbkane.com/warg/path"
"go.bbkane.com/warg/value/scalar"
"go.bbkane.com/warg/value/slice"
)
func exampleConfigFlagTextAdd(ctx warg.CmdContext) error {
addends := ctx.Flags["--addend"].([]int)
sum := 0
for _, a := range addends {
sum += a
}
fmt.Printf("Sum: %d\n", sum)
return nil
}
func ExampleConfigFlag() {
app := warg.New(
"newAppName",
"v1.0.0",
warg.NewSection(
"do math",
warg.NewSubCmd(
string("add"),
"add integers",
exampleConfigFlagTextAdd,
warg.NewCmdFlag(
string("--addend"),
"Integer to add. Flag is repeatible",
slice.Int(),
warg.ConfigPath("add.addends"),
warg.Required(),
),
),
),
warg.ConfigFlag(
yamlreader.New,
warg.FlagMap{
"--config": warg.NewFlag(
"Path to YAML config file",
scalar.Path(
scalar.Default(path.New("~/.config/calc.yaml")),
),
warg.Alias("-c"),
),
},
),
)
err := os.WriteFile(
"testdata/ExampleConfigFlag/calc.yaml",
[]byte(`add:
addends:
- 1
- 2
- 3
`),
0644,
)
if err != nil {
log.Fatalf("write error: %e", err)
}
app.MustRunWithArgs(
[]string{"add", "-c", "testdata/ExampleConfigFlag/calc.yaml"},
)
// Output:
// Sum: 6
}