command-line-parser
Advanced tools
Comparing version
22
index.js
@@ -1,8 +0,10 @@ | ||
// Converts an array of arguments into a key value object, | ||
// Keys arguments start with a dash (setting true to an immediately preceding key arg).. | ||
// Converts an array of arguments into a key value object. | ||
// Keys arguments start with a dash (setting true to an immediately preceding key arg). | ||
// Key and value arguments are separated by a space. | ||
function commandLineParser( args = process.argv.slice(2) ) { | ||
module.exports = function( allowMultiKey = false, args = process.argv.slice(2) ) { | ||
return args.reduce( ( argsObjInReduction, arg, index, args ) => { | ||
const isArgKey = arg[0] === '-'; | ||
const isArgKey = arg.length > 0 && arg[0] === '-'; | ||
// const isArgSingleKey = !allowMultiKey || arg.length > 1 && arg[0] === '-' && arg[1] === '-'; | ||
const isArgMultiKey = allowMultiKey && arg.length > 1 && arg[0] === '-' && arg[1] !== '-'; | ||
let isArgValue = false; | ||
@@ -33,4 +35,10 @@ | ||
if( isArgKey ) { | ||
argsObjInReduction = Object.assign( {}, argsObjInReduction, { | ||
[ arg.slice(1) ] : index === args.length - 1 ? true : undefined | ||
const trimmedKey = arg.replace( /-/g, ' ' ).trim(); | ||
const keys = trimmedKey ? isArgMultiKey ? Array.from( trimmedKey ) : [ trimmedKey ] : [] ; | ||
const value = index === args.length - 1 ? true : undefined ; | ||
keys.forEach( key => { | ||
argsObjInReduction = Object.assign( {}, argsObjInReduction, { | ||
[ key ] : value | ||
}); | ||
}); | ||
@@ -42,3 +50,1 @@ } | ||
} | ||
module.exports = commandLineParser; |
{ | ||
"name": "command-line-parser", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Simple lightweight function takes an array of command-line arguments and returns a parsed object", | ||
@@ -5,0 +5,0 @@ "main": "command-line-parser.js", |
# command-line-parser | ||
This code: | ||
@@ -21,8 +20,20 @@ ``` | ||
``` | ||
This simple lightweight module exports a default function that takes an array of command-line arguments and returns a parsed object (a bit simpler than the venerable ```minimist```). | ||
This simple lightweight module exports a default function that takes an array of command-line arguments and returns a parsed object (a bit simpler than the venerable ```minimist```). | ||
* Each argument is separate - it does not support combining multiple options (or options with values) into one argument (as in ```-abc``` or ```-n5```). | ||
* Only the first character is examined for a dash to see if it should be treated as a key to an option (only the first dash of ```--debug``` would be processed). | ||
Any argument with a dash is considered one key and will be in the parsed object with the following value: | ||
Prefixing an argument with a dash indicates that the argument is one key. However, passing ```true``` into the parser ```require('command-line-parser')(true)```allows a parsing mode where dash indicates that each letter be treated like separate one-letter keys. This option would cause ```-lt``` to set an object with: | ||
``` | ||
{ | ||
l: true, | ||
t: true | ||
} | ||
``` | ||
If it's desired to have a multi-letter argument represent one key in this mode, then two dashes should be specified for that argument, for instance ``` -lt --debug``` sets: | ||
``` | ||
{ | ||
l: true, | ||
t: true, | ||
debug: true | ||
} | ||
``` | ||
A key argument will be in the parsed object with the following value: | ||
* If the key is followed by a non-dash argument, its value will be the non-dash argument (as in ```-key value```). | ||
@@ -33,10 +44,18 @@ * If the key is not followed by a non-dash argument, its value will be true (as in ```-key1 -key2```). | ||
If no argument is passed in to the default module function, it defaults to the array ```process.argv.slice(2)```. | ||
The parsing function takes these arguments with their respective default values: | ||
``` | ||
require('command-line-parser')( allowMultiKeys = false, arguments = process.argv.slice(2)); | ||
``` | ||
Install with ```npm install command-line-parser```. | ||
See ```test/assert.js``` for example usage. | ||
Test via ```npm test``` (and see test/index.js for some examples). | ||
Install with ```npm install command-line-parser```. | ||
A couple of considerations - perhaps a number in a key should indicate the value, so ```-n5``` becomes: | ||
``` | ||
{ | ||
n: 5 | ||
} | ||
``` | ||
And how best to disambiguate between whether an argument should server as a value or an extra argument. | ||
Test via ```npm test```. | ||
PS. Useful pattern using destructuring assignment, default values, and renaming ```_args```: | ||
@@ -43,0 +62,0 @@ ``` |
@@ -7,3 +7,3 @@ const should = require('chai').should(); | ||
it( 'correctly parses empty args', function() { | ||
const argsObj = commandLineParser( [] ); | ||
const argsObj = commandLineParser( false, [] ); | ||
argsObj.should.be.empty; | ||
@@ -13,2 +13,8 @@ argsObj.should.be.an('object'); | ||
it( 'correctly parses just dash args', function() { | ||
const argsObj = commandLineParser( false, [ '-', '--', '---' ] ); | ||
argsObj.should.be.empty; | ||
argsObj.should.be.an('object'); | ||
}); | ||
it( 'correctly parses full args', function() { | ||
@@ -29,2 +35,35 @@ process.argv = [ '/my/bin/node', './myscript.js', '-port', '8081', '-a', 'b', '-c', '-dee', 'e', 'extra', 'something', '-f' ]; | ||
it( 'correctly parses full args with one dash but two-dashes supported', function() { | ||
process.argv = [ '/my/bin/node', './myscript.js', '-port', '8081', '-a', 'b', '-c', '-dee', 'e', 'extra', 'something', '-f' ]; | ||
const argsObj = commandLineParser( true, process.argv.slice(2) ); | ||
const { p, o, r, t, a, c, d, e, f, _args: files } = argsObj ; | ||
p.should.equal( '8081' ); | ||
o.should.equal( '8081' ); | ||
r.should.equal( '8081' ); | ||
t.should.equal( '8081' ); | ||
a.should.equal( 'b' ); | ||
c.should.be.true; | ||
d.should.equal( 'e' ); | ||
e.should.equal( 'e' ); | ||
f.should.be.true; | ||
files.should.have.length( 2 ); | ||
files[0].should.equal( 'extra' ); | ||
files[1].should.equal( 'something' ); | ||
}); | ||
it( 'correctly parses full args with two dashes', function() { | ||
process.argv = [ '/my/bin/node', './myscript.js', '--port', '8081', '-a', 'b', '-c', '--dee', 'e', 'extra', 'something', '-f' ]; | ||
const argsObj = commandLineParser( true, process.argv.slice(2) ); | ||
const { port, a, c, dee, f, _args: files } = argsObj ; | ||
port.should.equal( '8081' ); | ||
a.should.equal( 'b' ); | ||
c.should.be.true; | ||
dee.should.equal( 'e' ); | ||
f.should.be.true; | ||
files.should.have.length( 2 ); | ||
files[0].should.equal( 'extra' ); | ||
files[1].should.equal( 'something' ); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
7677
46.73%97
73.21%64
42.22%