[Pkg-javascript-commits] [node-dargs] 41/54: Close #31 PR: Add support for an "_" key which appends values to the end of the args list. Fixes #19

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:05 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-dargs.

commit 77fddac63bd7d3d1a3069560844c2723d07467ff
Author: Chris Chua <chua at uber.com>
Date:   Thu Jan 7 20:00:51 2016 +0100

    Close #31 PR: Add support for an "_" key which appends values to the end of the args list. Fixes #19
---
 index.js  | 14 ++++++++++++++
 readme.md |  5 ++++-
 test.js   | 17 ++++++++++++++---
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/index.js b/index.js
index 68ecd95..5140408 100644
--- a/index.js
+++ b/index.js
@@ -18,6 +18,7 @@ function createAliasArg(key, val) {
 
 module.exports = function (input, opts) {
 	var args = [];
+	var extraArgs = [];
 
 	opts = opts || {};
 
@@ -40,6 +41,15 @@ module.exports = function (input, opts) {
 			argFn = createAliasArg;
 		}
 
+		if (key === '_') {
+			if (!Array.isArray(val)) {
+				throw new TypeError('special key \'_\' expected to be an Array, found a(n) ' + (typeof val) + '.');
+			}
+
+			extraArgs = val;
+			return;
+		}
+
 		if (val === true) {
 			args.push(argFn(key, ''));
 		}
@@ -63,5 +73,9 @@ module.exports = function (input, opts) {
 		}
 	});
 
+	extraArgs.forEach(function (extraArgVal) {
+		args.push(String(extraArgVal));
+	});
+
 	return args;
 };
diff --git a/readme.md b/readme.md
index 50eb69c..a48521a 100644
--- a/readme.md
+++ b/readme.md
@@ -18,6 +18,7 @@ $ npm install --save dargs
 const dargs = require('dargs');
 
 const input = {
+	_: ['some', 'option'],          // each value in '_' will be appended to the end of the generated argument list, imitating minimist
 	foo: 'bar',
 	hello: true,                    // results in only the key being used
 	cake: false,                    // prepends `no-` before the key
@@ -39,7 +40,9 @@ console.log(dargs(input, {excludes: excludes}));
 	'--no-cake',
 	'--camel-case=5',
 	'--multiple=value',
-	'--multiple=value2'
+	'--multiple=value2',
+	'some',
+	'option'
 ]
 */
 
diff --git a/test.js b/test.js
index fbd4ae4..bcda7e8 100644
--- a/test.js
+++ b/test.js
@@ -2,6 +2,7 @@ import test from 'ava';
 import fn from './';
 
 const fixture = {
+	_: ['some', 'option'],
 	a: 'foo',
 	b: true,
 	c: false,
@@ -24,10 +25,16 @@ test('convert options to cli flags', t => {
 		'--e=bar',
 		'--h=with a space',
 		'--i=let\'s try quotes',
-		'--camel-case-camel'
+		'--camel-case-camel',
+		'some',
+		'option'
 	]);
 });
 
+test('raises a TypeError if  \'_\' value is not an Array', t => {
+	t.throws(fn.bind(fn, {a: 'foo', _: 'baz'}), TypeError);
+});
+
 test('useEquals options', t => {
 	t.same(fn(fixture, {
 		useEquals: false
@@ -40,7 +47,9 @@ test('useEquals options', t => {
 		'--e bar',
 		'--h with a space',
 		'--i let\'s try quotes',
-		'--camel-case-camel'
+		'--camel-case-camel',
+		'some',
+		'option'
 	]);
 });
 
@@ -49,7 +58,9 @@ test('exclude options', t => {
 		'--a=foo',
 		'--no-c',
 		'--d=5',
-		'--camel-case-camel'
+		'--camel-case-camel',
+		'some',
+		'option'
 	]);
 });
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-dargs.git



More information about the Pkg-javascript-commits mailing list