Skip to content

Commit 512d784

Browse files
committed
fix: inline closing bracket error
1 parent bdcf285 commit 512d784

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ var minimist = require('minimist');
33
module.exports = function parse (args, opts) {
44
var level = 0, index;
55
var args_ = [];
6-
6+
77
for (var i = 0; i < args.length; i++) {
88
if (typeof args[i] === 'string' && /^\[/.test(args[i])) {
99
if (level ++ === 0) {
1010
index = i;
1111
}
1212
}
13-
if (typeof args[i] === 'string' && /\]$/.test(args[i])) {
13+
if (typeof args[i] === 'string' && /^\[?[^\[]*\]$/.test(args[i])) {
1414
if (-- level > 0) continue;
15-
15+
1616
var sub = args.slice(index, i + 1);
1717
if (typeof sub[0] === 'string') {
1818
sub[0] = sub[0].replace(/^\[/, '');
@@ -24,12 +24,12 @@ module.exports = function parse (args, opts) {
2424
sub[n] = sub[n].replace(/\]$/, '');
2525
}
2626
if (sub[n] === '') sub.pop();
27-
27+
2828
args_.push(parse(sub));
2929
}
3030
else if (level === 0) args_.push(args[i]);
3131
}
32-
32+
3333
var argv = minimist(args_, opts);
3434
return argv;
3535
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@minimistjs/subarg",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "parse arguments with recursive contexts",
55
"main": "index.js",
66
"dependencies": {

test/inlineBrackets.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var subarg = require('..');
2+
var test = require('tape');
3+
4+
test('inline brackets', function (t) {
5+
t.plan(6);
6+
t.deepEqual(
7+
subarg('beep -t [ boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] -q]'.split(/\s+/)),
8+
{
9+
_: [ 'beep'],
10+
t: {
11+
_: [ 'boop' ],
12+
o: 'a.txt',
13+
u: [ 'http://localhost', 'http://localhost?q=[1]' ],
14+
q: true
15+
}
16+
}
17+
)
18+
19+
t.deepEqual(
20+
subarg('beep -t [boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] -q -o b.txt]'.split(/\s+/)),
21+
{
22+
_: [ 'beep'],
23+
t: {
24+
_: [ 'boop' ],
25+
o: [ 'a.txt', 'b.txt' ],
26+
u: [ 'http://localhost', 'http://localhost?q=[1]' ],
27+
q: true
28+
}
29+
}
30+
)
31+
32+
t.deepEqual(
33+
subarg('beep -t [ boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] ]'.split(/\s+/)),
34+
{
35+
_: [ 'beep'],
36+
t: {
37+
_: [ 'boop' ],
38+
o: 'a.txt',
39+
u: [ 'http://localhost', 'http://localhost?q=[1]' ]
40+
}
41+
}
42+
)
43+
44+
t.deepEqual(
45+
subarg('beep -t [boop -o a.txt -u http://localhost\?q=\[1\] -u http://localhost]'.split(/\s+/)),
46+
{
47+
_: [ 'beep'],
48+
t: {
49+
_: [ 'boop' ],
50+
o: 'a.txt',
51+
u: [ 'http://localhost?q=[1]', 'http://localhost' ]
52+
}
53+
}
54+
)
55+
56+
t.deepEqual(
57+
subarg('beep -t [ boop -o a.txt -u [beep] ]'.split(/\s+/)),
58+
{
59+
_: [ 'beep'],
60+
t: {
61+
_: [ 'boop' ],
62+
o: 'a.txt',
63+
u: {
64+
_: [ 'beep' ]
65+
}
66+
}
67+
}
68+
)
69+
70+
t.deepEqual(
71+
subarg('beep -t [boop] -u [ be[ep] ]'.split(/\s+/)),
72+
{
73+
_: [ 'beep'],
74+
t: {
75+
_: [ 'boop' ]
76+
},
77+
u: {
78+
_: [ 'be[ep]' ]
79+
}
80+
}
81+
)
82+
});
83+
84+
test.skip('doesnt work', function (t) {
85+
t.plan(2);
86+
/*
87+
* expected:
88+
* { _: [ 'beep' ], t: { _: [ 'boop' ], o: 'a.txt', u: [ 'http://localhost', 'http://localhost?q=[1]' ] } }
89+
* actual:
90+
* { _: [ 'beep' ], t: true }
91+
*
92+
*/
93+
t.deepEqual(
94+
subarg('beep -t [boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\]]'.split(/\s+/)),
95+
{
96+
_: [ 'beep'],
97+
t: {
98+
_: [ 'boop' ],
99+
o: 'a.txt',
100+
u: [ 'http://localhost', 'http://localhost?q=[1]' ]
101+
}
102+
}
103+
)
104+
105+
t.deepEqual(
106+
subarg('beep -t [ boop -o a.txt -u [be[ep] ]'.split(/\s+/)),
107+
{
108+
/*
109+
* expected:
110+
* { _: [ 'beep' ], t: { _: [ 'boop' ], o: 'a.txt', u: { _: [ 'beep' ] } } }
111+
* actual:
112+
* { _: [ 'beep' ], t: true }
113+
*/
114+
_: [ 'beep'],
115+
t: {
116+
_: [ 'boop' ],
117+
o: 'a.txt',
118+
u: {
119+
_: [ 'beep' ]
120+
}
121+
}
122+
}
123+
)
124+
});

0 commit comments

Comments
 (0)