Comparing version
@@ -1,3 +0,5 @@ | ||
var E = '', | ||
patt = /^(-[^-\s]|--[^-\s].+)$/g; // pattern to detect single or double hyphen start | ||
var undef, | ||
rep = '$2$4', | ||
patt = /(^-([^-\s]$))|(^--([^-\s][\s\S]+$))/, // pattern to detect single or double hyphen start | ||
parse; | ||
@@ -9,13 +11,20 @@ /** | ||
* @example | ||
* var args = require('./quickargs.js')(process.argv); | ||
* var args = require('miniargs').parse(process.argv); | ||
*/ | ||
module.exports = function (argv) { | ||
parse = function (argv) { | ||
var args = {}, // object to store all key-value argument extraction | ||
lastarg; // args are split by space, so we keep a track of last key detected | ||
arg; // args are split by space, so we keep a track of last key detected | ||
argv && argv.slice && argv.slice(2).forEach(function (item) { | ||
lastarg = patt.test(item) ? item.replace(patt, E) : (lastarg && (args[lastarg] = item), undefined); | ||
patt.test(item) ? | ||
((arg = item.replace(patt, rep)), args[arg] = undef) : | ||
(arg && (args[arg] = item), (arg = undef)); | ||
}); | ||
return args; | ||
}; | ||
}; | ||
// export parse function | ||
module.exports = { | ||
parse: parse | ||
}; |
{ | ||
"name": "miniargs", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"main": "lib/index.js", | ||
"description": "Lightweight command-line tool argument processing package", | ||
"scripts": { | ||
"test": "mocha test/**/*-spec.js" | ||
"test": "jscs lib test && jshint lib test && istanbul cover _mocha -- test/**/*-spec.js" | ||
}, | ||
@@ -25,4 +25,7 @@ "repository": { | ||
"expect.js": "^0.3.1", | ||
"istanbul": "^0.3.8", | ||
"jscs": "^1.11.3", | ||
"jshint": "^2.6.3", | ||
"mocha": "^2.2.1" | ||
} | ||
} |
# miniargs | ||
Lightweight command-line tool argument processing package | ||
## Usage | ||
```javascript | ||
var args = require('miniargs').parse(process.argv); | ||
``` |
@@ -0,31 +1,71 @@ | ||
/* global describe, it */ | ||
var expect = require('expect.js'), | ||
miniargs; | ||
miniargs; | ||
describe('miniargs', function () { | ||
it ('module must be require-able', function () { | ||
var error; | ||
it('module must be require-able', function () { | ||
var error; | ||
try { | ||
miniargs = require('../lib/index.js'); | ||
} | ||
catch (e) { | ||
error = e; | ||
} | ||
try { | ||
miniargs = require('../lib/index.js'); | ||
} | ||
catch (e) { | ||
error = e; | ||
} | ||
expect(error).to.not.be.ok(); | ||
expect(miniargs).to.be.ok(); | ||
expect(typeof miniargs).to.be('function'); | ||
}); | ||
expect(error).to.not.be.ok(); | ||
expect(miniargs).to.be.ok(); | ||
expect(typeof miniargs).to.be('object'); | ||
}); | ||
it ('must handle blank parameters', function () { | ||
expect(miniargs()).to.eql({}); | ||
expect(miniargs(null)).to.eql({}); | ||
expect(miniargs(false)).to.eql({}); | ||
expect(miniargs([])).to.eql({}); | ||
expect(miniargs({})).to.eql({}); | ||
}); | ||
it('.parse() function must be available', function () { | ||
expect(miniargs && (typeof miniargs.parse)).to.be('function'); | ||
}); | ||
it ('must ignore first two argument parameters', function () { | ||
expect(miniargs(['first', 'second', 'third', 'undefined'])).to.eql({third: undefined}); | ||
}); | ||
}); | ||
it('must handle blank parameters', function () { | ||
expect(miniargs.parse()).to.eql({}); | ||
expect(miniargs.parse(null)).to.eql({}); | ||
expect(miniargs.parse(false)).to.eql({}); | ||
expect(miniargs.parse([])).to.eql({}); | ||
expect(miniargs.parse({})).to.eql({}); | ||
}); | ||
it('must ignore first two argument parameters (as from process.argv)', function () { | ||
expect(miniargs.parse(['first', 'second'])).to.eql({}); | ||
expect(miniargs.parse(['first', 'second', '-t', 'works'])).to.eql({ | ||
t: 'works' | ||
}); | ||
}); | ||
it('must accept single hyphen arguments', function () { | ||
expect(miniargs.parse(['', '', '-a', 'y', '-b', 'n'])).to.eql({ | ||
a: 'y', | ||
b: 'n' | ||
}); | ||
}); | ||
it('must set undefined to value of value-less parameters', function () { | ||
expect(miniargs.parse(['', '', '-p', '--param'])).to.eql({ | ||
p: undefined, | ||
param: undefined | ||
}); | ||
}); | ||
it('must not accept single hyphen arguments having more than one character', function () { | ||
expect(miniargs.parse(['', '', '-pp', 'value'])).to.eql({}); | ||
}); | ||
it('must not accept double hyphen arguments with just one character', function () { | ||
expect(miniargs.parse(['', '', '--p', 'value'])).to.eql({}); | ||
}); | ||
it('must not accept whitespace or hyphen between hyphen and character', function () { | ||
expect(miniargs.parse(['', '', '- p', 'v', '-- param', 'value', '---param2', 'value2'])).to.eql({}); | ||
}); | ||
it('must allow hyphen between param name', function () { | ||
expect(miniargs.parse(['', '', '--param-name', 'value'])).to.eql({ | ||
'param-name': 'value' | ||
}); | ||
}); | ||
}); |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
17774
372.71%11
83.33%201
390.24%8
166.67%5
150%2
Infinity%1
Infinity%