-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_lostructure_test.go
More file actions
75 lines (60 loc) · 1.55 KB
/
example_lostructure_test.go
File metadata and controls
75 lines (60 loc) · 1.55 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
package clic_test
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/daved/clic"
)
type HandleRoot struct {
out io.Writer
}
func NewHandleRoot(out io.Writer) *HandleRoot {
return &HandleRoot{
out: out,
}
}
func (s *HandleRoot) HandleCommand(ctx context.Context) error {
fmt.Fprintln(s.out, "from root")
return nil
}
type HandlePrint struct {
out io.Writer
info string
value string
}
func NewHandlePrint(out io.Writer) *HandlePrint {
return &HandlePrint{
out: out,
info: "default",
value: "unset",
}
}
func (s *HandlePrint) HandleCommand(ctx context.Context) error {
fmt.Fprintf(s.out, "info flag = %s\nvalue operand = %v\n", s.info, s.value)
return nil
}
func Example_appArchitectureLoStructure() {
out := os.Stdout // emulate an interesting dependency
// Associate Handler with command name
handlePrint := NewHandlePrint(out)
print := clic.New(handlePrint, "print")
// Associate "print" flag and operand variables with relevant names
print.Flag(&handlePrint.info, "i|info", "Set additional info.")
print.Operand(&handlePrint.value, true, "first_operand", "Value to be printed.")
// Associate Handler with application name, adding "print" as a subcommand
root := clic.New(NewHandleRoot(out), "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
}