-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_nostructure_test.go
More file actions
57 lines (47 loc) · 1.23 KB
/
example_nostructure_test.go
File metadata and controls
57 lines (47 loc) · 1.23 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
package clic_test
import (
"context"
"fmt"
"log"
"os"
"github.com/daved/clic"
)
var args = []string{"myapp", "print", "--info=flagval", "arrrg"}
func Example_appArchitectureNoStructure() {
var (
info = "default"
value = "unset"
out = os.Stdout // emulate an interesting dependency
)
// Associate HandlerFunc with command name
print := clic.NewFromFunc(
func(ctx context.Context) error {
fmt.Fprintf(out, "info flag = %s\nvalue operand = %v\n", info, value)
return nil
},
"print",
)
// Associate "print" flag and operand variables with relevant names
print.Flag(&info, "i|info", "Set additional info.")
print.Operand(&value, true, "first_operand", "Value to be printed.")
// Associate HandlerFunc with application name, adding "print" as a subcommand
root := clic.NewFromFunc(
func(ctx context.Context) error {
fmt.Fprintln(out, "from root")
return nil
},
"myapp", print,
)
// Parse the cli command as `myapp print --info=flagval arrrg`
cmd, err := root.Parse(args[1:])
if err != nil {
log.Fatalln(err)
}
// Run the handler that Parse resolved to
if err := cmd.Handle(context.Background()); err != nil {
log.Fatalln(err)
}
// Output:
// info flag = flagval
// value operand = arrrg
}