-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathargs_test.go
186 lines (140 loc) · 4.04 KB
/
args_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 args
import (
"fmt"
"testing"
)
const (
TEST_STRING = `the quick "brown 'fox'" jumps 'o v e r' \"the\"\ lazy dog`
PARSE_STRING = "-l --number=42 -where=here --where=there -- -not-an-option- one two three # a comment \n next line"
TEST_BRACKETS = `some stuff in "quotes" and {"brackets":[1, 'help', (2+3)]} {{"a":1,"b":2},{"c":3}} x={"value":"with brakets", a=[1, "2", 3.14, {"another": "field"}]}`
TEST_INFIELD = `a=1 b=2 c={"one": 2.0} d=[1, 2, 3], e=["x", "y"], q="quoted" qe="\"quoted and escaped\"" qe='"quote in quotes"'` + " `\"raw string \\'quoted\\'\"` qr=`\"raw string \\'quoted\\'\"`" + ` {"q":"\"quoted value\""}`
)
func TestScanner(test *testing.T) {
scanner := NewScannerString(TEST_STRING)
for {
token, delim, err := scanner.NextToken()
if err != nil {
test.Log(err)
break
}
test.Logf("%q %q", delim, token)
}
}
func TestScannerInfieldBrackets(test *testing.T) {
scanner := NewScannerString(TEST_INFIELD)
scanner.InfieldBrackets = true
for {
token, delim, err := scanner.NextToken()
if err != nil {
test.Log(err)
break
}
test.Logf("%q %q", delim, token)
}
}
func TestScannerDots(test *testing.T) {
scanner := NewScannerString(`a."b".'c'.[some]`)
res := []string{}
for {
token, delim, err := scanner.NextToken()
if err != nil {
test.Log(err)
break
}
res = append(res, token)
if delim != '.' {
test.Logf("delimiter: %q", delim)
break
}
}
var rest [64]byte
n, _ := scanner.in.Read(rest[:])
test.Log("tokens:", res, "remain:", string(rest[:n]))
}
func TestGetArgs(test *testing.T) {
test.Logf("%q", GetArgs(TEST_STRING))
}
func TestGetArgsN(test *testing.T) {
args := GetArgsN(TEST_STRING, 1)
test.Logf("%d, %q", len(args), args)
args = GetArgsN(TEST_STRING, 2)
test.Logf("%d, %q", len(args), args)
args = GetArgsN(TEST_STRING, 3)
test.Logf("%d, %q", len(args), args)
}
func TestGetArgsN2(test *testing.T) {
args := GetArgsN("x", 1)
test.Logf("asked 1 expected 1 got %q", args)
args = GetArgsN("x", 2)
test.Logf("asked 2 expected 1 got %q", args)
args = GetArgsN("x y", 2)
test.Logf("asked 2 expected 2 got %q", args)
args = GetArgsN("x y z", 2)
test.Logf("asked 2 expected 2 got %q", args)
}
func TestGetOptions(test *testing.T) {
options, rest := GetOptions(PARSE_STRING)
test.Logf("%q %q", options, rest)
}
func TestParseArgs(test *testing.T) {
test.Logf("%q", ParseArgs(PARSE_STRING))
}
func TestBrackets(test *testing.T) {
for i, a := range GetArgs(TEST_BRACKETS) {
fmt.Println(i, a)
}
}
func TestBracketsInfield(test *testing.T) {
for i, a := range GetArgs(TEST_INFIELD, InfieldBrackets()) {
fmt.Println(i, a)
}
}
func TestUserTokens(test *testing.T) {
for i, a := range GetArgs(TEST_INFIELD+" X = Y", UserTokens("=")) {
fmt.Println(i, a)
}
}
func ExampleGetArgs() {
s := `one two three "double quotes" 'single quotes' arg\ with\ spaces "\"quotes\" in 'quotes'" '"quotes" in \'quotes'"`
for i, arg := range GetArgs(s) {
fmt.Println(i, arg)
}
// Output:
// 0 one
// 1 two
// 2 three
// 3 double quotes
// 4 single quotes
// 5 arg with spaces
// 6 "quotes" in 'quotes'
// 7 "quotes" in 'quotes
}
func ExampleParseArgs() {
arguments := "-l --number=42 -where=here -- -not-an-option- one two three |pipers piping"
parsed := ParseArgs(arguments)
fmt.Println("options:", parsed.Options)
fmt.Println("arguments:", parsed.Arguments)
// Output:
// options: map[l: number:42 where:here]
// arguments: [-not-an-option- one two three |pipers piping]
}
func ExampleParseFlags() {
arguments := "-l --number=42 -where=here -- -not-an-option- one two three"
flags := NewFlags("args")
list := flags.Bool("l", false, "list something")
num := flags.Int("number", 0, "a number option")
where := flags.String("where", "", "a string option")
if err := ParseFlags(flags, arguments); err != nil {
fmt.Println(err)
} else {
fmt.Println("list:", *list)
fmt.Println("num:", *num)
fmt.Println("where:", *where)
fmt.Println("args:", flags.Args())
}
// Output:
// list: true
// num: 42
// where: here
// args: [-not-an-option- one two three]
}