-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliflags.go
87 lines (72 loc) · 1.84 KB
/
cliflags.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
package main
import (
"fmt"
"os"
"golang.org/x/exp/slices"
"github.com/marco-m/otium"
)
func main() {
if err := run(); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
}
func run() error {
pcd := otium.NewProcedure(otium.ProcedureOpts{
Title: "Example showing command-line flags",
Desc: `
Sometimes you know beforehand some of the variables that the procedure steps
will ask. In those cases, it can be simpler to pass them as command-line flags,
instead of waiting to be prompted for them.
`})
pcd.AddStep(&otium.Step{
Title: "Introduction",
Desc: `
- To see all the variables directly settable from the command-line, re-invoke
this program as:
cliflags -h
- To see at any point in time the contents of the _bag_ (that is, all the
variables available to this program, type this command from the (top) REPL:
variables
`,
})
pcd.AddStep(&otium.Step{
Title: "Two variables",
Desc: `
- This step asks for two variables: 'fruit' and 'amount'.
To set them as command-line flags, just invoke the program as:
cliflags --fruit banana --amount 42
- The variable 'fruit' has a validator, that limits the acceptable inputs.
Try it.
`,
Vars: []otium.Variable{
{
Name: "fruit",
Desc: "Fruit for breakfast",
Fn: func(val string) error {
basket := []string{"banana", "mango"}
if !slices.Contains(basket, val) {
return fmt.Errorf("we only have %s", basket)
}
return nil
},
},
{Name: "amount", Desc: "How many pieces of fruit"},
},
Run: func(bag otium.Bag, uctx any) error {
fruit, err := bag.Get("fruit")
if err != nil {
return err
}
amount, err := bag.Get("amount")
if err != nil {
return err
}
fmt.Println("The variables are:")
fmt.Println("fruit", fruit)
fmt.Println("amount", amount)
return nil
},
})
return pcd.Execute(os.Args)
}