cli-argparse
Advanced tools
Comparing version
31
index.js
@@ -91,2 +91,19 @@ var short = '-', long = '--'; | ||
function breaks(arg, opts, out) { | ||
//if(arg === long) return true; | ||
var list = (arg === long) ? [long] : []; | ||
if(Array.isArray(opts.stop) && !list.length) { | ||
list = opts.stop.filter(function(ptn) { | ||
if(typeof ptn === 'string' || (ptn instanceof RegExp)) { | ||
return (ptn instanceof RegExp) ? ptn.test(arg) : arg === ptn; | ||
} | ||
return false; | ||
}) | ||
} | ||
if(list.length) { | ||
out.stop = list[0]; | ||
} | ||
return list.length > 0 ? list[0] : null; | ||
} | ||
module.exports = function parse(args, opts) { | ||
@@ -98,3 +115,3 @@ opts = opts || {}; opts.alias = opts.alias || {}; | ||
unparsed: [], strict: !!opts.strict}; | ||
var i, arg, l = args.length, skip, raw, equals, flag, opt, info; | ||
var i, arg, l = args.length, skip, raw, equals, flag, opt, info, stop; | ||
for(i = 0;i < l;i++) { | ||
@@ -119,6 +136,12 @@ if(!args[0]) break; arg = '' + args.shift(); skip = false; | ||
out.unparsed.push(arg); | ||
}else if(arg == short) { | ||
}else if(arg === short) { | ||
out.stdin = true; | ||
}else if(arg == long) { | ||
out.unparsed = out.unparsed.concat(args.slice(i)); break; | ||
}else if(stop = breaks(arg, opts, out)) { | ||
if(stop instanceof RegExp) { | ||
out.unparsed = out.unparsed.concat( | ||
[arg.replace(stop, '')].concat(args.slice(i))); | ||
}else{ | ||
out.unparsed = out.unparsed.concat(args.slice(i)); | ||
} | ||
break; | ||
}else if(opt || ~equals || lre.test(arg)) { | ||
@@ -125,0 +148,0 @@ skip = options(arg, out, args[0], opts, opt); |
{ | ||
"name": "cli-argparse", | ||
"version": "0.3.10", | ||
"version": "0.3.11", | ||
"description": "Lightweight argument parser", | ||
@@ -5,0 +5,0 @@ "author": "muji <noop@xpm.io>", |
@@ -8,2 +8,3 @@ var expect = require('chai').expect; | ||
var result = parse(args); | ||
expect(result.stop).to.eql('--'); | ||
expect(result.raw).to.eql(args); | ||
@@ -20,2 +21,53 @@ expect(result.flags.x).to.eql(true); | ||
}); | ||
it('should stop parsing on string config', function(done) { | ||
var args = ['-xvf', '#', 'server', '--port=80', '--host=localhost']; | ||
var result = parse(args, {stop: ['#']}); | ||
expect(result.stop).to.eql('#'); | ||
expect(result.raw).to.eql(args); | ||
expect(result.flags.x).to.eql(true); | ||
expect(result.flags.v).to.eql(true); | ||
expect(result.flags.f).to.eql(true); | ||
expect(result.unparsed).to.be.an('array'); | ||
expect(result.unparsed.length).to.eql(3); | ||
expect(result.unparsed[0]).to.eql(args[2]); | ||
expect(result.unparsed[1]).to.eql(args[3]); | ||
expect(result.unparsed[2]).to.eql(args[4]); | ||
done(); | ||
}); | ||
it('should stop parsing on regexp config', function(done) { | ||
var ptn = /^#/; | ||
var args = ['-xvf', '#server', '--port=80', '--host=localhost']; | ||
var result = parse(args, {stop: [ptn]}); | ||
expect(result.stop).to.eql(ptn); | ||
expect(result.raw).to.eql(args); | ||
expect(result.flags.x).to.eql(true); | ||
expect(result.flags.v).to.eql(true); | ||
expect(result.flags.f).to.eql(true); | ||
expect(result.unparsed).to.be.an('array'); | ||
expect(result.unparsed.length).to.eql(3); | ||
expect(result.unparsed[0]).to.eql(args[1].replace(ptn, '')); | ||
expect(result.unparsed[1]).to.eql(args[2]); | ||
expect(result.unparsed[2]).to.eql(args[3]); | ||
done(); | ||
}); | ||
it('should ignore invalid stop type', function(done) { | ||
var args = ['-xvf', 'false', 'server', '--port=80', '--host=localhost']; | ||
var result = parse(args, {stop: [false]}); | ||
expect(result.stop).to.eql(undefined); | ||
expect(result.raw).to.eql(args); | ||
expect(result.flags.x).to.eql(true); | ||
expect(result.flags.v).to.eql(true); | ||
expect(result.flags.f).to.eql(true); | ||
expect(result.unparsed).to.be.an('array'); | ||
expect(result.unparsed.length).to.eql(2); | ||
expect(result.unparsed[0]).to.eql(args[1]); | ||
expect(result.unparsed[1]).to.eql(args[2]); | ||
expect(result.options.port).to.eql('80'); | ||
expect(result.options.host).to.eql('localhost'); | ||
done(); | ||
}); | ||
}) |
34991
8.33%805
9.52%