markdown-magic 4.8.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/src/argparse/README.md +3 -8
- package/src/argparse/argparse.js +7 -313
- package/src/argparse/argparse.test.js +168 -90
- package/src/argparse/index.js +2 -2
- package/src/argparse/splitOutsideQuotes.js +3 -75
- package/src/cli-run.js +97 -166
- package/src/cli-run.test.js +71 -3
- package/src/globparse.js +14 -159
- package/src/index.dependency-graph.test.js +49 -0
- package/src/index.js +41 -11
- package/src/transforms/code/index.js +19 -13
- package/src/utils/remoteRequest.js +30 -6
- package/types/src/argparse/argparse.d.ts +2 -32
- package/types/src/argparse/argparse.d.ts.map +1 -1
- package/types/src/argparse/index.d.ts +2 -2
- package/types/src/argparse/splitOutsideQuotes.d.ts +2 -1
- package/types/src/argparse/splitOutsideQuotes.d.ts.map +1 -1
- package/types/src/cli-run.d.ts +4 -2
- package/types/src/cli-run.d.ts.map +1 -1
- package/types/src/globparse.d.ts +11 -17
- package/types/src/globparse.d.ts.map +1 -1
- package/types/src/index.d.ts +36 -9
- package/types/src/index.d.ts.map +1 -1
- package/types/src/transforms/code/index.d.ts.map +1 -1
- package/types/src/utils/remoteRequest.d.ts.map +1 -1
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
const { test } = require('uvu')
|
|
3
3
|
const assert = require('uvu/assert')
|
|
4
4
|
const { deepLog } = require('../utils/logs')
|
|
5
|
-
const {
|
|
5
|
+
const { dxParse } = require('./argparse')
|
|
6
6
|
|
|
7
|
-
const DEBUG = true
|
|
7
|
+
const DEBUG = (process.argv.includes('--debug')) ? true : false
|
|
8
8
|
const logger = DEBUG ? console.log : () => {}
|
|
9
9
|
const deepLogger = DEBUG ? deepLog : () => {}
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ function logInput(rawArgs, result) {
|
|
|
13
13
|
logger('Input:')
|
|
14
14
|
logger(`CLI ${rawArgs.join(' ')}`)
|
|
15
15
|
if (result) {
|
|
16
|
-
|
|
16
|
+
deepLogger('result', result)
|
|
17
17
|
}
|
|
18
18
|
logger('───────────────────────\n')
|
|
19
19
|
}
|
|
@@ -23,47 +23,47 @@ function stringToArgs(str) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
test('Exports API', () => {
|
|
26
|
-
assert.equal(typeof
|
|
26
|
+
assert.equal(typeof dxParse, 'function', 'undefined val')
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
const cmd00 = stringToArgs('lol = true cool = false')
|
|
30
|
-
test
|
|
31
|
-
const result =
|
|
30
|
+
test(`CLI ${cmd00.join(' ')}`, () => {
|
|
31
|
+
const result = dxParse(cmd00)
|
|
32
32
|
logInput(cmd00, result)
|
|
33
33
|
assert.equal(result.mergedOptions, { lol: true, cool: false })
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
const cmd000 = stringToArgs('--lol true --cool false')
|
|
37
37
|
test(`CLI ${cmd000.join(' ')}`, () => {
|
|
38
|
-
const result =
|
|
38
|
+
const result = dxParse(cmd000)
|
|
39
39
|
logInput(cmd000, result)
|
|
40
40
|
assert.equal(result.mergedOptions, { lol: true, cool: false })
|
|
41
41
|
})
|
|
42
42
|
|
|
43
43
|
const cmd0000 = stringToArgs('-lol true -cool false')
|
|
44
44
|
test(`CLI ${cmd0000.join(' ')}`, () => {
|
|
45
|
-
const result =
|
|
45
|
+
const result = dxParse(cmd0000)
|
|
46
46
|
logInput(cmd0000, result)
|
|
47
47
|
assert.equal(result.mergedOptions, { lol: true, cool: false })
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
const cmd00000 = stringToArgs('-lol -cool ')
|
|
51
51
|
test(`CLI ${cmd00000.join(' ')}`, () => {
|
|
52
|
-
const result =
|
|
52
|
+
const result = dxParse(cmd00000)
|
|
53
53
|
logInput(cmd00000, result)
|
|
54
54
|
assert.equal(result.mergedOptions, { lol: true, cool: true })
|
|
55
55
|
})
|
|
56
56
|
|
|
57
57
|
const cmd000000 = stringToArgs('--stage prod --cool wild = yolo')
|
|
58
58
|
test(`CLI ${cmd000000.join(' ')}`, () => {
|
|
59
|
-
const result =
|
|
59
|
+
const result = dxParse(cmd000000)
|
|
60
60
|
logInput(cmd000000, result)
|
|
61
61
|
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: 'yolo' })
|
|
62
62
|
})
|
|
63
63
|
|
|
64
64
|
const cmd0 = stringToArgs('-f no word = { foo: bar }')
|
|
65
65
|
test(`Handles single dash options, CLI ${cmd0.join(' ')}`, () => {
|
|
66
|
-
const result =
|
|
66
|
+
const result = dxParse(cmd0)
|
|
67
67
|
logInput(cmd0, result)
|
|
68
68
|
assert.equal(result.mergedOptions.f, 'no')
|
|
69
69
|
assert.equal(result.mergedOptions.word, { foo: 'bar' })
|
|
@@ -72,23 +72,23 @@ test(`Handles single dash options, CLI ${cmd0.join(' ')}`, () => {
|
|
|
72
72
|
|
|
73
73
|
const cmd1 = stringToArgs('-f -x -xyz ballin -u jimmy')
|
|
74
74
|
test(`flags in a row "CLI ${cmd1.join(' ')}"`, () => {
|
|
75
|
-
const result =
|
|
76
|
-
|
|
75
|
+
const result = dxParse(cmd1)
|
|
76
|
+
deepLogger('result', result)
|
|
77
77
|
assert.equal(result.mergedOptions.xyz, 'ballin')
|
|
78
78
|
assert.equal(result.mergedOptions, { f: true, x: true, xyz: 'ballin', u: 'jimmy' })
|
|
79
79
|
})
|
|
80
80
|
|
|
81
81
|
const cmd2 = stringToArgs('-f -x -xyz ballin = yo -u jimmy')
|
|
82
82
|
test(`flags in a row two "CLI ${cmd2.join(' ')}"`, () => {
|
|
83
|
-
const result =
|
|
84
|
-
|
|
83
|
+
const result = dxParse(cmd2)
|
|
84
|
+
deepLogger('result', result)
|
|
85
85
|
assert.equal(result.mergedOptions.ballin, 'yo')
|
|
86
86
|
})
|
|
87
87
|
|
|
88
88
|
const cmd3 = stringToArgs('-f -x -xyz ballin = yo -u jimmy=timmy')
|
|
89
89
|
test(`flags in a row two "CLI ${cmd3.join(' ')}"`, () => {
|
|
90
|
-
const result =
|
|
91
|
-
|
|
90
|
+
const result = dxParse(cmd3)
|
|
91
|
+
deepLogger('result', result)
|
|
92
92
|
assert.equal(result.mergedOptions.jimmy, 'timmy')
|
|
93
93
|
})
|
|
94
94
|
|
|
@@ -96,8 +96,8 @@ const cmd4 = stringToArgs(
|
|
|
96
96
|
'whatever=[uno] -v=false nice -funky no -word { foo: bar } -w yo --what nice -f -x -xyz false -u jimmy --testx --file **.md --u word',
|
|
97
97
|
)
|
|
98
98
|
test(`Handles glob options "CLI ${cmd4.join(' ')}"`, () => {
|
|
99
|
-
const result =
|
|
100
|
-
|
|
99
|
+
const result = dxParse(cmd4, { accumulate: ['u'] })
|
|
100
|
+
deepLogger('result', result)
|
|
101
101
|
assert.equal(result.mergedOptions, {
|
|
102
102
|
whatever: ['uno'],
|
|
103
103
|
v: false,
|
|
@@ -115,52 +115,135 @@ test(`Handles glob options "CLI ${cmd4.join(' ')}"`, () => {
|
|
|
115
115
|
})
|
|
116
116
|
})
|
|
117
117
|
|
|
118
|
+
const filePatternArgs = stringToArgs('--file **/*.md')
|
|
119
|
+
test(`Groups single --file glob pattern "CLI ${filePatternArgs.join(' ')}"`, () => {
|
|
120
|
+
const result = dxParse(filePatternArgs)
|
|
121
|
+
deepLogger('result', result)
|
|
122
|
+
assert.equal(result.globGroups, [
|
|
123
|
+
{ key: 'file', rawKey: '--file', values: ['**/*.md'] }
|
|
124
|
+
])
|
|
125
|
+
assert.equal(result.mergedOptions.file, '**/*.md')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const filesPatternArgs = stringToArgs('--files README.md docs/**/*.md --stage dev')
|
|
129
|
+
test(`Groups multiple --files patterns and leaves other options "CLI ${filesPatternArgs.join(' ')}"`, () => {
|
|
130
|
+
const result = dxParse(filesPatternArgs)
|
|
131
|
+
deepLogger('result', result)
|
|
132
|
+
assert.equal(result.globGroups, [
|
|
133
|
+
{ key: 'files', rawKey: '--files', values: ['README.md', 'docs/**/*.md'] }
|
|
134
|
+
])
|
|
135
|
+
assert.equal(result.mergedOptions.stage, 'dev')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const bareFilePatternArgs = stringToArgs('README.md docs/**/*.md --stage dev')
|
|
139
|
+
test(`Groups bare positional file patterns "CLI ${bareFilePatternArgs.join(' ')}"`, () => {
|
|
140
|
+
const result = dxParse(bareFilePatternArgs)
|
|
141
|
+
deepLogger('result', result)
|
|
142
|
+
assert.equal(result.globGroups, [
|
|
143
|
+
{ key: '', rawKey: '', values: ['README.md', 'docs/**/*.md'] }
|
|
144
|
+
])
|
|
145
|
+
assert.equal(result.leadingCommands, [])
|
|
146
|
+
assert.equal(result.mergedOptions.stage, 'dev')
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
const pathPatternArgs = stringToArgs('--path docs/**/*.md --stage dev')
|
|
150
|
+
test(`Groups --path glob pattern "CLI ${pathPatternArgs.join(' ')}"`, () => {
|
|
151
|
+
const result = dxParse(pathPatternArgs)
|
|
152
|
+
deepLogger('result', result)
|
|
153
|
+
assert.equal(result.globGroups, [
|
|
154
|
+
{ key: 'path', rawKey: '--path', values: ['docs/**/*.md'] }
|
|
155
|
+
])
|
|
156
|
+
assert.equal(result.mergedOptions.stage, 'dev')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const singleDashConfigFileArgs = stringToArgs('-config md.config.js -stage dev')
|
|
160
|
+
test(`Promotes consumed single-dash file-valued options "CLI ${singleDashConfigFileArgs.join(' ')}"`, () => {
|
|
161
|
+
const result = dxParse(singleDashConfigFileArgs)
|
|
162
|
+
deepLogger('result', result)
|
|
163
|
+
assert.equal(result.globGroups, [
|
|
164
|
+
{ key: 'config', rawKey: '-config', values: ['md.config.js'] }
|
|
165
|
+
])
|
|
166
|
+
assert.equal(result.mergedOptions, {
|
|
167
|
+
config: 'md.config.js',
|
|
168
|
+
stage: 'dev'
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
const shellExpandedFileArgs = [
|
|
173
|
+
'CONTRIBUTING.md',
|
|
174
|
+
'NOTES.md',
|
|
175
|
+
'README.md',
|
|
176
|
+
'large-table.md',
|
|
177
|
+
'foo',
|
|
178
|
+
'bar',
|
|
179
|
+
'=',
|
|
180
|
+
'false',
|
|
181
|
+
'lol={x:100}'
|
|
182
|
+
]
|
|
183
|
+
test(`Does not treat shell-expanded file matches as leading commands`, () => {
|
|
184
|
+
const result = dxParse(shellExpandedFileArgs)
|
|
185
|
+
deepLogger('result', result)
|
|
186
|
+
assert.equal(result.globGroups, [
|
|
187
|
+
{
|
|
188
|
+
key: '',
|
|
189
|
+
rawKey: '',
|
|
190
|
+
values: ['CONTRIBUTING.md', 'NOTES.md', 'README.md', 'large-table.md']
|
|
191
|
+
}
|
|
192
|
+
])
|
|
193
|
+
assert.equal(result.leadingCommands, ['foo'])
|
|
194
|
+
assert.equal(result.mergedOptions, {
|
|
195
|
+
foo: true,
|
|
196
|
+
bar: false,
|
|
197
|
+
lol: { x: 100 }
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
118
201
|
const cmd5 = stringToArgs('--stage prod --cool wild=yolo')
|
|
119
202
|
test(`CLI ${cmd5.join(' ')}"`, () => {
|
|
120
|
-
const result =
|
|
121
|
-
|
|
203
|
+
const result = dxParse(cmd5)
|
|
204
|
+
deepLogger('result', result)
|
|
122
205
|
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: 'yolo' })
|
|
123
206
|
})
|
|
124
207
|
|
|
125
208
|
const cmd6 = stringToArgs('--stage prod --cool wild=yolo --stage dev')
|
|
126
|
-
test
|
|
127
|
-
const result =
|
|
128
|
-
|
|
209
|
+
test(`Takes last value for same flag "CLI ${cmd6.join(' ')}"`, () => {
|
|
210
|
+
const result = dxParse(cmd6)
|
|
211
|
+
deepLogger('result', result)
|
|
129
212
|
assert.equal(result.mergedOptions, { stage: 'dev', cool: true, wild: 'yolo' })
|
|
130
213
|
})
|
|
131
214
|
|
|
132
215
|
const cmd7 = stringToArgs('--stage prod --cool wild="yo lo" --stage dev --stage prod')
|
|
133
|
-
test(`
|
|
134
|
-
const result =
|
|
135
|
-
|
|
136
|
-
assert.equal(result.mergedOptions, { stage:
|
|
216
|
+
test(`Takes last value for duplicate long flags "CLI ${cmd7.join(' ')}"`, () => {
|
|
217
|
+
const result = dxParse(cmd7)
|
|
218
|
+
deepLogger('result', result)
|
|
219
|
+
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: 'yo lo' })
|
|
137
220
|
})
|
|
138
221
|
|
|
139
222
|
const cmd8 = stringToArgs('-stage prod -cool wild=yolo -stage dev')
|
|
140
|
-
test(`
|
|
141
|
-
const result =
|
|
142
|
-
|
|
143
|
-
assert.equal(result.mergedOptions, { stage:
|
|
223
|
+
test(`Takes last value for duplicate forgiving single-dash long flags "CLI ${cmd8.join(' ')}"`, () => {
|
|
224
|
+
const result = dxParse(cmd8)
|
|
225
|
+
deepLogger('result', result)
|
|
226
|
+
assert.equal(result.mergedOptions, { stage: 'dev', cool: true, wild: 'yolo' })
|
|
144
227
|
})
|
|
145
228
|
|
|
146
229
|
const cmd9 = stringToArgs('-stage prod -cool wild=-yolo -stage dev -stage prod')
|
|
147
230
|
test(`Handles values with dashes "CLI ${cmd9.join(' ')}"`, () => {
|
|
148
|
-
const result =
|
|
149
|
-
|
|
150
|
-
assert.equal(result.mergedOptions, { stage:
|
|
231
|
+
const result = dxParse(cmd9)
|
|
232
|
+
deepLogger('result', result)
|
|
233
|
+
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: '-yolo' })
|
|
151
234
|
})
|
|
152
235
|
|
|
153
236
|
const cmd10 = stringToArgs('-stage prod -cool wild=--yolo -stage dev -stage prod -stage dev')
|
|
154
237
|
test(`Handles values with dashes "CLI ${cmd10.join(' ')}"`, () => {
|
|
155
|
-
const result =
|
|
156
|
-
|
|
157
|
-
assert.equal(result.mergedOptions, { stage:
|
|
238
|
+
const result = dxParse(cmd10)
|
|
239
|
+
deepLogger('result', result)
|
|
240
|
+
assert.equal(result.mergedOptions, { stage: 'dev', cool: true, wild: '--yolo' })
|
|
158
241
|
})
|
|
159
242
|
|
|
160
243
|
const cmd11 = stringToArgs('-stage prod -cool wild="--yolo"')
|
|
161
244
|
test(`Handles values with dashes "CLI ${cmd11.join(' ')}"`, () => {
|
|
162
|
-
const result =
|
|
163
|
-
|
|
245
|
+
const result = dxParse(cmd11)
|
|
246
|
+
deepLogger('result', result)
|
|
164
247
|
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: '--yolo' })
|
|
165
248
|
})
|
|
166
249
|
|
|
@@ -170,16 +253,16 @@ const cmd14 = stringToArgs('-stage prod -cool wild="yo=lo"')
|
|
|
170
253
|
test(`Handles values with equals "CLI ${cmd12.join(' ')}"`, () => {
|
|
171
254
|
const array = [cmd12, cmd13, cmd14]
|
|
172
255
|
for (const cmd of array) {
|
|
173
|
-
const result =
|
|
174
|
-
|
|
256
|
+
const result = dxParse(cmd)
|
|
257
|
+
deepLogger('result', result)
|
|
175
258
|
assert.equal(result.mergedOptions, { stage: 'prod', cool: true, wild: 'yo=lo' })
|
|
176
259
|
}
|
|
177
260
|
})
|
|
178
261
|
|
|
179
262
|
const cmd15 = stringToArgs('--param=userPoolId=us-west-1_fjsPJ6Q8J --param=userPoolClientId=19vdp5je9jsjkn488ddl4jvk19')
|
|
180
|
-
test(`
|
|
181
|
-
const result =
|
|
182
|
-
|
|
263
|
+
test(`Accumulates configured array values "CLI ${cmd15.join(' ')}"`, () => {
|
|
264
|
+
const result = dxParse(cmd15, { accumulate: ['param'] })
|
|
265
|
+
deepLogger('result', result)
|
|
183
266
|
assert.equal(result.mergedOptions, {
|
|
184
267
|
param: ['userPoolId=us-west-1_fjsPJ6Q8J', 'userPoolClientId=19vdp5je9jsjkn488ddl4jvk19'],
|
|
185
268
|
})
|
|
@@ -187,8 +270,8 @@ test(`Handles array values "CLI ${cmd15.join(' ')}"`, () => {
|
|
|
187
270
|
|
|
188
271
|
const cmd16 = stringToArgs('--param="userPoolId=us-west-1_fjsPJ6Q8J" --param="userPoolClientId=19vdp5je9jsjkn488ddl4jvk19"')
|
|
189
272
|
test(`Strips surrounding quotes "CLI ${cmd16.join(' ')}"`, () => {
|
|
190
|
-
const result =
|
|
191
|
-
|
|
273
|
+
const result = dxParse(cmd16, { accumulate: ['param'] })
|
|
274
|
+
deepLogger('result', result)
|
|
192
275
|
assert.equal(result.mergedOptions, {
|
|
193
276
|
param: ['userPoolId=us-west-1_fjsPJ6Q8J', 'userPoolClientId=19vdp5je9jsjkn488ddl4jvk19'],
|
|
194
277
|
})
|
|
@@ -196,8 +279,8 @@ test(`Strips surrounding quotes "CLI ${cmd16.join(' ')}"`, () => {
|
|
|
196
279
|
|
|
197
280
|
const cmd17 = stringToArgs('--env.stage=prod --env.region=us-east-1')
|
|
198
281
|
test(`Handles dotted properties "CLI ${cmd17.join(' ')}"`, () => {
|
|
199
|
-
const result =
|
|
200
|
-
|
|
282
|
+
const result = dxParse(cmd17)
|
|
283
|
+
deepLogger('result', result)
|
|
201
284
|
assert.equal(result.mergedOptions, {
|
|
202
285
|
'env.stage': 'prod',
|
|
203
286
|
'env.region': 'us-east-1'
|
|
@@ -206,8 +289,8 @@ test(`Handles dotted properties "CLI ${cmd17.join(' ')}"`, () => {
|
|
|
206
289
|
|
|
207
290
|
const cmd18 = stringToArgs('--array=[1,2,3] --items=["a","b","c"]')
|
|
208
291
|
test(`Handles array syntax "CLI ${cmd18.join(' ')}"`, () => {
|
|
209
|
-
const result =
|
|
210
|
-
|
|
292
|
+
const result = dxParse(cmd18)
|
|
293
|
+
deepLogger('result', result)
|
|
211
294
|
assert.equal(result.mergedOptions, {
|
|
212
295
|
array: [1, 2, 3],
|
|
213
296
|
items: ['a', 'b', 'c']
|
|
@@ -216,8 +299,8 @@ test(`Handles array syntax "CLI ${cmd18.join(' ')}"`, () => {
|
|
|
216
299
|
|
|
217
300
|
const cmd19 = stringToArgs('--config.json={"foo": "bar", "baz": 123}')
|
|
218
301
|
test(`Handles JSON objects "CLI ${cmd19.join(' ')}"`, () => {
|
|
219
|
-
const result =
|
|
220
|
-
|
|
302
|
+
const result = dxParse(cmd19)
|
|
303
|
+
deepLogger('result', result)
|
|
221
304
|
assert.equal(result.mergedOptions, {
|
|
222
305
|
'config.json': { foo: 'bar', baz: 123 }
|
|
223
306
|
})
|
|
@@ -225,8 +308,8 @@ test(`Handles JSON objects "CLI ${cmd19.join(' ')}"`, () => {
|
|
|
225
308
|
|
|
226
309
|
const cmd20 = stringToArgs('--empty --blank="" --null=null --undef=undefined')
|
|
227
310
|
test(`Handles special values "CLI ${cmd20.join(' ')}"`, () => {
|
|
228
|
-
const result =
|
|
229
|
-
|
|
311
|
+
const result = dxParse(cmd20)
|
|
312
|
+
deepLogger('result', result)
|
|
230
313
|
assert.equal(result.mergedOptions, {
|
|
231
314
|
empty: true,
|
|
232
315
|
blank: '',
|
|
@@ -238,20 +321,18 @@ test(`Handles special values "CLI ${cmd20.join(' ')}"`, () => {
|
|
|
238
321
|
// @TODO invert no prefixes?
|
|
239
322
|
const cmd21 = stringToArgs('--flag --no-flag --enable-feature --no-enable-feature')
|
|
240
323
|
test(`Handles boolean flags with no- prefix "CLI ${cmd21.join(' ')}"`, () => {
|
|
241
|
-
const result =
|
|
242
|
-
|
|
324
|
+
const result = dxParse(cmd21)
|
|
325
|
+
deepLogger('result', result)
|
|
243
326
|
assert.equal(result.mergedOptions, {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
'enable-feature': true,
|
|
247
|
-
'no-enable-feature': true
|
|
327
|
+
flag: false,
|
|
328
|
+
'enable-feature': false
|
|
248
329
|
})
|
|
249
330
|
})
|
|
250
331
|
|
|
251
332
|
const cmd22 = stringToArgs(`--spaced-string="hello world" --quoted='single quotes'`)
|
|
252
333
|
test(`Handles quoted strings with spaces "CLI ${cmd22.join(' ')}"`, () => {
|
|
253
|
-
const result =
|
|
254
|
-
|
|
334
|
+
const result = dxParse(cmd22)
|
|
335
|
+
deepLogger('result', result)
|
|
255
336
|
assert.equal(result.mergedOptions, {
|
|
256
337
|
'spaced-string': 'hello world',
|
|
257
338
|
quoted: 'single quotes'
|
|
@@ -261,8 +342,8 @@ test(`Handles quoted strings with spaces "CLI ${cmd22.join(' ')}"`, () => {
|
|
|
261
342
|
// This one is wrong iirc
|
|
262
343
|
const cmd23 = stringToArgs('--numbers 1 2 3 --strings a b c')
|
|
263
344
|
test.skip(`Handles space-separated values "CLI ${cmd23.join(' ')}"`, () => {
|
|
264
|
-
const result =
|
|
265
|
-
|
|
345
|
+
const result = dxParse(cmd23)
|
|
346
|
+
deepLogger('result', result)
|
|
266
347
|
assert.equal(result.mergedOptions, {
|
|
267
348
|
numbers: ['1', '2', '3'],
|
|
268
349
|
strings: ['a', 'b', 'c']
|
|
@@ -270,9 +351,9 @@ test.skip(`Handles space-separated values "CLI ${cmd23.join(' ')}"`, () => {
|
|
|
270
351
|
})
|
|
271
352
|
|
|
272
353
|
const cmd24 = stringToArgs(`--key=value --key="other value" --key='third value'`)
|
|
273
|
-
test(`
|
|
274
|
-
const result =
|
|
275
|
-
|
|
354
|
+
test(`Accumulates configured repeated key with different quote styles "CLI ${cmd24.join(' ')}"`, () => {
|
|
355
|
+
const result = dxParse(cmd24, { accumulate: ['key'] })
|
|
356
|
+
deepLogger('result', result)
|
|
276
357
|
assert.equal(result.mergedOptions, {
|
|
277
358
|
key: ['value', 'other value', 'third value']
|
|
278
359
|
})
|
|
@@ -280,10 +361,12 @@ test(`Handles multiple values for same key with different quote styles "CLI ${cm
|
|
|
280
361
|
|
|
281
362
|
const cmd25 = stringToArgs('-abc -xyz=123 -def="value" -ghi')
|
|
282
363
|
test(`Handles single-letter flags in various formats "CLI ${cmd25.join(' ')}"`, () => {
|
|
283
|
-
const result =
|
|
284
|
-
|
|
364
|
+
const result = dxParse(cmd25, {
|
|
365
|
+
allowShortClusters: true,
|
|
366
|
+
shortFlags: ['a', 'b', 'c', 'g', 'h', 'i'],
|
|
367
|
+
stripSingleDashOptions: true
|
|
285
368
|
})
|
|
286
|
-
|
|
369
|
+
deepLogger('result', result)
|
|
287
370
|
assert.equal(result.mergedOptions, {
|
|
288
371
|
a: true,
|
|
289
372
|
b: true,
|
|
@@ -297,24 +380,19 @@ test(`Handles single-letter flags in various formats "CLI ${cmd25.join(' ')}"`,
|
|
|
297
380
|
})
|
|
298
381
|
|
|
299
382
|
const cmd26 = stringToArgs('--flag=true --flag=false --flag --no-flag')
|
|
300
|
-
test(`
|
|
301
|
-
const result =
|
|
302
|
-
|
|
383
|
+
test(`Takes last value for multiple boolean assignments "CLI ${cmd26.join(' ')}"`, () => {
|
|
384
|
+
const result = dxParse(cmd26)
|
|
385
|
+
deepLogger('result', result)
|
|
303
386
|
assert.equal(result.mergedOptions, {
|
|
304
|
-
flag:
|
|
305
|
-
true,
|
|
306
|
-
false,
|
|
307
|
-
true,
|
|
308
|
-
],
|
|
309
|
-
'no-flag': true
|
|
387
|
+
flag: false
|
|
310
388
|
})
|
|
311
389
|
})
|
|
312
390
|
|
|
313
391
|
|
|
314
392
|
const cmd27 = stringToArgs('-l -a -h')
|
|
315
393
|
test(`Short flags "CLI ${cmd27.join(' ')}"`, () => {
|
|
316
|
-
const result =
|
|
317
|
-
|
|
394
|
+
const result = dxParse(cmd27)
|
|
395
|
+
deepLogger('result', result)
|
|
318
396
|
assert.equal(result.mergedOptions, {
|
|
319
397
|
l: true,
|
|
320
398
|
a: true,
|
|
@@ -324,8 +402,8 @@ test(`Short flags "CLI ${cmd27.join(' ')}"`, () => {
|
|
|
324
402
|
|
|
325
403
|
const cmd28 = stringToArgs('-lah = yo')
|
|
326
404
|
test(`flag clustering "CLI ${cmd28.join(' ')}"`, () => {
|
|
327
|
-
const result =
|
|
328
|
-
|
|
405
|
+
const result = dxParse(cmd28, true)
|
|
406
|
+
deepLogger('result', result)
|
|
329
407
|
assert.equal(result.mergedOptions, {
|
|
330
408
|
l: true,
|
|
331
409
|
a: true,
|
|
@@ -336,8 +414,8 @@ test(`flag clustering "CLI ${cmd28.join(' ')}"`, () => {
|
|
|
336
414
|
|
|
337
415
|
const cmd29 = stringToArgs('test.js --foo 1337 -B hello --mcgee')
|
|
338
416
|
test(`Short flags "CLI ${cmd29.join(' ')}"`, () => {
|
|
339
|
-
const result =
|
|
340
|
-
|
|
417
|
+
const result = dxParse(cmd29)
|
|
418
|
+
deepLogger('result', result)
|
|
341
419
|
assert.equal(result.mergedOptions, {
|
|
342
420
|
foo: 1337,
|
|
343
421
|
B: 'hello',
|
|
@@ -348,8 +426,8 @@ test(`Short flags "CLI ${cmd29.join(' ')}"`, () => {
|
|
|
348
426
|
|
|
349
427
|
const cmd30 = stringToArgs('hey --foo=hi.hello?q=p hello')
|
|
350
428
|
test(`basic string parsing (equals long-arg-with-equals) "CLI ${cmd30.join(' ')}"`, () => {
|
|
351
|
-
const result =
|
|
352
|
-
|
|
429
|
+
const result = dxParse(cmd30)
|
|
430
|
+
deepLogger('result', result)
|
|
353
431
|
assert.equal(result.mergedOptions, {
|
|
354
432
|
// possibleLeadingCommands: ['hey'],
|
|
355
433
|
hey: true, // @TODO fix this do we want it?
|
|
@@ -360,8 +438,8 @@ test(`basic string parsing (equals long-arg-with-equals) "CLI ${cmd30.join(' ')}
|
|
|
360
438
|
|
|
361
439
|
const cmd31 = stringToArgs('int=-5')
|
|
362
440
|
test(`basic int parsing "CLI ${cmd31.join(' ')}"`, () => {
|
|
363
|
-
const result =
|
|
364
|
-
|
|
441
|
+
const result = dxParse(cmd31)
|
|
442
|
+
deepLogger('result', result)
|
|
365
443
|
assert.equal(result.mergedOptions, {
|
|
366
444
|
int: -5
|
|
367
445
|
})
|
package/src/argparse/index.js
CHANGED
|
@@ -1,78 +1,6 @@
|
|
|
1
1
|
|
|
2
|
+
const { splitOutsideQuotes } = require('@davidwells/dx-args')
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
str = str
|
|
6
|
-
.replace(/}(?=(?:(?:[^"]*(?:")){2})*[^"]*(?:")[^"]*$)/g, '∆') // For } in double " quotes
|
|
7
|
-
.replace(/}(?=(?:(?:[^']*(?:')){2})*[^']*(?:')[^']*$)/g, '∆') // For } in single ' quotes
|
|
8
|
-
.replace(/{(?=(?:(?:[^']*(?:')){2})*[^']*(?:')[^']*$)/g, '▽') // For { in single ' quotes
|
|
9
|
-
.replace(/{(?=(?:(?:[^"]*(?:")){2})*[^"]*(?:")[^"]*$)/g, '▽') // For { in double " quotes
|
|
10
|
-
.replace(/\[(?=(?:(?:[^']*(?:')){2})*[^']*(?:')[^']*$)/g, '▸') // For [ in single ' quotes
|
|
11
|
-
.replace(/\[(?=(?:(?:[^"]*(?:")){2})*[^"]*(?:")[^"]*$)/g, '▸') // For [ in double " quotes
|
|
12
|
-
.replace(/\](?=(?:(?:[^']*(?:')){2})*[^']*(?:')[^']*$)/g, '◂') // For ] in single ' quotes
|
|
13
|
-
.replace(/\](?=(?:(?:[^"]*(?:")){2})*[^"]*(?:")[^"]*$)/g, '◂') // For ] in double " quotes
|
|
14
|
-
|
|
15
|
-
let parts = []
|
|
16
|
-
let current = ''
|
|
17
|
-
let inQuotes = false
|
|
18
|
-
let quoteChar = ''
|
|
19
|
-
let braceCount = 0
|
|
20
|
-
let bracketCount = 0
|
|
21
|
-
|
|
22
|
-
for (let char of str) {
|
|
23
|
-
if ((char === '"' || char === "'") && !inQuotes) {
|
|
24
|
-
inQuotes = true
|
|
25
|
-
quoteChar = char
|
|
26
|
-
current += char
|
|
27
|
-
} else if (char === quoteChar && inQuotes) {
|
|
28
|
-
inQuotes = false
|
|
29
|
-
quoteChar = ''
|
|
30
|
-
current += char
|
|
31
|
-
} else if (char === '{') {
|
|
32
|
-
braceCount++
|
|
33
|
-
current += char
|
|
34
|
-
} else if (char === '}') {
|
|
35
|
-
braceCount--
|
|
36
|
-
current += char
|
|
37
|
-
} else if (char === '[') {
|
|
38
|
-
bracketCount++
|
|
39
|
-
current += char
|
|
40
|
-
} else if (char === ']') {
|
|
41
|
-
bracketCount--
|
|
42
|
-
current += char
|
|
43
|
-
} else if (char === ' ' && !inQuotes && !braceCount && !bracketCount) {
|
|
44
|
-
if (current) parts.push(current)
|
|
45
|
-
current = ''
|
|
46
|
-
} else {
|
|
47
|
-
current += char
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (current) parts.push(current)
|
|
52
|
-
return parts.map((x) => {
|
|
53
|
-
return x
|
|
54
|
-
.replace(/∆/g, '}')
|
|
55
|
-
.replace(/▽/g, '{')
|
|
56
|
-
.replace(/▸/g, '[')
|
|
57
|
-
.replace(/◂/g, ']')
|
|
58
|
-
})
|
|
59
|
-
/* combineEquals */
|
|
60
|
-
.reduce((acc, item, i) => {
|
|
61
|
-
if (item.startsWith('=')) {
|
|
62
|
-
acc[acc.length - 1] += item
|
|
63
|
-
return acc
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (acc.length && acc[acc.length - 1].endsWith('=') && (acc[acc.length - 1].match(/=/g) || []).length === 1) {
|
|
67
|
-
acc[acc.length - 1] += item
|
|
68
|
-
return acc
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
acc.push(item)
|
|
72
|
-
return acc
|
|
73
|
-
}, [])
|
|
4
|
+
module.exports = {
|
|
5
|
+
splitOutsideQuotes,
|
|
74
6
|
}
|
|
75
|
-
|
|
76
|
-
module.exports = {
|
|
77
|
-
splitOutsideQuotes
|
|
78
|
-
}
|