-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_test.go
68 lines (61 loc) · 1.56 KB
/
lib_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
package conq_test
import (
"fmt"
"github.com/patroclos/go-conq"
"github.com/patroclos/go-conq/aid"
"github.com/patroclos/go-conq/aid/cmdhelp"
"github.com/patroclos/go-conq/commander"
"github.com/patroclos/go-conq/getopt"
"github.com/posener/complete"
)
func ExampleCmd_Opts() {
cmd := makeCmd()
ctx := conq.OSContext()
ctx.Args = []string{"--depth", "500", "testerino"}
err := commander.New(getopt.New(), nil).Execute(cmd, ctx)
if err != nil {
panic(err)
}
// Output: Doing something to depth:500 in path:"default"
// Query: "testerino"
}
func ExampleDefaultHelp() {
cmd := makeCmd()
ctx := conq.OSContext()
ctx.Args = []string{"help"}
err := commander.New(getopt.New(), aid.DefaultHelp).Execute(cmd, ctx)
if err != nil {
panic(err)
}
// Output: usage: app [options] query
//
// Options:
// int depth (required)
// string path
//
// Arguments:
// string query
//
// Commands: help
}
func makeCmd() *conq.Cmd {
var OptDepth = conq.ReqOpt[int]{Name: "depth"}
var OptPath = conq.Opt[string]{Name: "path", Predict: complete.PredictAnything}
var ArgQuery = conq.ReqOpt[string]{Name: "query"}
return &conq.Cmd{
Name: "app",
Opts: []conq.Opter{OptDepth, OptPath},
Args: []conq.Opter{ArgQuery},
Commands: []*conq.Cmd{cmdhelp.New(nil)},
Run: func(c conq.Ctx) error {
depth := OptDepth.Get(c)
path, err := OptPath.Get(c)
if err != nil {
path = "default"
}
fmt.Fprintf(c.Out, "Doing something to depth:%d in path:%q\n", depth, path)
fmt.Fprintf(c.Out, "Query: %q\n", ArgQuery.Get(c))
return nil
},
}
}