-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
186 lines (157 loc) · 4.49 KB
/
options_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package options
import (
"fmt"
"strings"
"testing"
)
func TestNew(t *testing.T) {
_, err := Parse(`
usage: haraway <flags>... <command> <args>...
more summary
--
# Options
root= -r,--root=,HARAWAY_ROOT Path to the haraway data root
prefix= -p,--prefix,HARAWAY_PREFIX Path to the haraway install prefix.
verbose -v,--verbose Show more info
debug -d,--debug,HARAWAY_DEBUG Show debug info
--
--
# Commands
exec exec Execute a command within the haraway sanbox
shell sh,shell Open a shell within the haraway sanbox
--
`)
if err != nil {
t.Error(err)
}
}
func TestMulti(t *testing.T) {
spec, err := Parse(`
usage: multi <flags>... <command> <args>...
--
# Options
include=, -I,--include=, Add dir to include search path
exclude=, -X,--exclude= Add dir to exclude search path
--
--
--
`)
if err != nil {
t.Error(err)
}
argv := []string{"multi", "-I", "/usr/local", "--include=/usr/include", "-I=/foo"}
oo, err := spec.Interpret(argv, []string{})
if err != nil {
t.Error(err)
}
z, ok := oo.Get("include")
if !ok {
t.Error("expected to find at least one -I; found none!")
}
if z != "/usr/local" {
t.Errorf("expected to see /usr/local, saw %s", z)
}
zv := oo.GetMulti("include")
if len(zv) != 3 {
t.Errorf("expected to see 3 entries, saw %d", len(zv))
}
}
func TestParse(t *testing.T) {
spec, err := Parse(`
usage: haraway <flags>... <command> <args>...
--
root= -r,--root=,HARAWAY_ROOT Path to the haraway data root
prefix= -p,--prefix,HARAWAY_PREFIX Path to the haraway install prefix.
verbose -v,--verbose Show more info
debug -d,--debug,HARAWAY_DEBUG Show debug info
--
--
exec c,exec Execute a command within the haraway sanbox
shell sh,shell Open a shell within the haraway sanbox
--
`)
if err != nil {
t.Error(err)
}
opts, err := spec.Interpret([]string{"haraway", "-p", "/usr/local", "-r=hello", "-v", "c", "ls"}, []string{})
if err != nil {
t.Fatal(err)
}
if v, ok := opts.Get("root"); ok && v != "hello" {
t.Error("--root != hello")
}
if v, ok := opts.Get("verbose"); ok && v != "true" {
t.Error("--verbose != true")
}
if opts.GetBool("verbose") != true {
t.Error("--verbose != true (bool)")
}
if strings.Join(opts.Args, " ") != "exec ls" {
t.Errorf(".Args != [`exec`, `ls`] (was: %+v)", opts.Args)
}
}
func ExampleParse() {
spec, err := Parse(`
usage: example-tool
A short description of the command
--
flag --flag,-f,FLAG A description for this flag
option= --option=,-o=,OPTION= A description for this option
the description continues here
!required= --required,-r=,REQUIRED= A required option
--
env_var= ENV_VAR= An environment variable
--
help help,h Show this help message
run run Run some function
--
More freestyle text
`)
if err != nil {
spec.PrintUsageWithError(err)
}
opts, err := spec.Interpret([]string{"example-tool", "--required", "hello world"}, []string{})
if err != nil {
spec.PrintUsageWithError(err)
}
v, _ := opts.Get("required")
fmt.Printf("required: %s", v)
// Output:
// required: hello world
}
func TestDefaults(t *testing.T) {
spec, err := Parse(`
usage: haraway <flags>... <command> <args>...
--
root=XYZ -r,--root=,HARAWAY_ROOT Path to the haraway data root
num=2 -n= Path to the haraway install prefix.
--
--
exec c,exec Execute a command within the haraway sanbox
shell sh,shell Open a shell within the haraway sanbox
--
`)
if err != nil {
t.Error(err)
}
opts, err := spec.Interpret([]string{"haraway"}, []string{})
if err != nil {
t.Fatal(err)
}
if v, ok := opts.Get("root"); ok && v != "XYZ" {
t.Error("--root != XYZ")
}
if v, ok := opts.GetInt("num"); ok && v != 2 {
t.Errorf("--num != 2; saw %v", v)
}
opts, err = spec.Interpret([]string{"haraway", "-r", "hello", "-n=5"}, []string{})
if err != nil {
t.Fatal(err)
}
if v, ok := opts.Get("root"); ok && v != "hello" {
t.Error("--root != hello")
}
if v, ok := opts.GetInt("num"); ok && v != 5 {
t.Error("-n != 5")
}
}