-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_lostructure_test.go
74 lines (59 loc) · 1.57 KB
/
example_lostructure_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
package clic_test
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/daved/clic"
)
type RootHandle struct {
out io.Writer
}
func NewRootHandle(out io.Writer) *RootHandle {
return &RootHandle{
out: out,
}
}
func (s *RootHandle) HandleCommand(ctx context.Context) error {
fmt.Fprintln(s.out, "ouch, hit root")
return nil
}
type PrintHandle struct {
out io.Writer
info string
value string
}
func NewPrintHandle(out io.Writer) *PrintHandle {
return &PrintHandle{
out: out,
info: "default",
value: "unset",
}
}
func (s *PrintHandle) HandleCommand(ctx context.Context) error {
fmt.Fprintf(s.out, "info flag = %s\nvalue arg = %v\n", s.info, s.value)
return nil
}
func Example_loStructure() {
out := os.Stdout // emulate an interesting dependency
// Associate Handler with command name "print"
printHandle := NewPrintHandle(out)
print := clic.New(printHandle, "print")
// Associate "print" flag and operand variables with relevant names
print.Flag(&printHandle.info, "i|info", "Set additional info.")
print.Operand(&printHandle.value, true, "first_operand", "Value to be printed.")
// Associate Handler with application name, adding "print" as a subcommand
rootHandle := clic.New(NewRootHandle(out), "myapp", print)
// Parse the cli command as `myapp print --info=flagval arrrg`
if err := rootHandle.Parse(args[1:]); err != nil {
log.Fatalln(err)
}
// Run the handler that Parse resolved to
if err := rootHandle.HandleResolvedCmd(context.Background()); err != nil {
log.Fatalln(err)
}
// Output:
// info flag = flagval
// value arg = arrrg
}