Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimist version update #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ var minimist = require('minimist');
module.exports = function parse (args, opts) {
var level = 0, index;
var args_ = [];

for (var i = 0; i < args.length; i++) {
if (typeof args[i] === 'string' && /^\[/.test(args[i])) {
if (level ++ === 0) {
index = i;
}
}
if (typeof args[i] === 'string' && /\]$/.test(args[i])) {
if (typeof args[i] === 'string' && /^\[?[^\[]*\]$/.test(args[i])) {
if (-- level > 0) continue;

var sub = args.slice(index, i + 1);
if (typeof sub[0] === 'string') {
sub[0] = sub[0].replace(/^\[/, '');
Expand All @@ -24,12 +24,12 @@ module.exports = function parse (args, opts) {
sub[n] = sub[n].replace(/\]$/, '');
}
if (sub[n] === '') sub.pop();

args_.push(parse(sub));
}
else if (level === 0) args_.push(args[i]);
}

var argv = minimist(args_, opts);
return argv;
};
124 changes: 124 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@minimistjs/subarg",
"version": "1.0.0",
"version": "2.0.0",
"description": "parse arguments with recursive contexts",
"main": "index.js",
"dependencies": {
"minimist": "^1.1.0"
"minimist": "^1.2.8"
},
"devDependencies": {
"tape": "^3.0.0"
Expand Down
133 changes: 133 additions & 0 deletions test/inlineBrackets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
var subarg = require('..');
var test = require('tape');

test('inline brackets', function (t) {
t.plan(7);

t.deepEqual(
subarg('http://localhost\?q=\[1\]'.split(/\s+/)),
{
_: [ 'http://localhost?q=[1]' ]
}
)

t.deepEqual(
subarg('beep -t [ boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] -q]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: [ 'http://localhost', 'http://localhost?q=[1]' ],
q: true
}
}
)

t.deepEqual(
subarg('beep -t [boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] -q -o b.txt]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: [ 'a.txt', 'b.txt' ],
u: [ 'http://localhost', 'http://localhost?q=[1]' ],
q: true
}
}
)

t.deepEqual(
subarg('beep -t [ boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\] ]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: [ 'http://localhost', 'http://localhost?q=[1]' ]
}
}
)

t.deepEqual(
subarg('beep -t [boop -o a.txt -u http://localhost\?q=\[1\] -u http://localhost]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: [ 'http://localhost?q=[1]', 'http://localhost' ]
}
}
)

t.deepEqual(
subarg('beep -t [ boop -o a.txt -u [beep] ]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: {
_: [ 'beep' ]
}
}
}
)

t.deepEqual(
subarg('beep -t [boop] -u [ be[ep] ]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ]
},
u: {
_: [ 'be[ep]' ]
}
}
)
});

test.skip('doesnt work', function (t) {
t.plan(2);
/*
* expected:
* { _: [ 'beep' ], t: { _: [ 'boop' ], o: 'a.txt', u: [ 'http://localhost', 'http://localhost?q=[1]' ] } }
* actual:
* { _: [ 'beep' ], t: true }
*
*/
t.deepEqual(
subarg('beep -t [boop -o a.txt -u http://localhost -u http://localhost\?q=\[1\]]'.split(/\s+/)),
{
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: [ 'http://localhost', 'http://localhost?q=[1]' ]
}
}
)

t.deepEqual(
subarg('beep -t [ boop -o a.txt -u [ be[ep]] ]'.split(/\s+/)),
{
/* Same as above - note the context-closing bracket is right next to an inline closing bracket
* If you add a space, it works
* expected:
* { _: [ 'beep' ], t: { _: [ 'boop' ], o: 'a.txt', u: { _: [ 'be[ep]' ] } } }
* actual:
* { _: [ 'beep' ], t: true }
*/
_: [ 'beep'],
t: {
_: [ 'boop' ],
o: 'a.txt',
u: {
_: [ 'be[ep]' ]
}
}
}
)
});