[Pkg-javascript-commits] [node-dargs] 40/54: Close #30 PR: Add useEquals options to swap "=" for " ". Fixes #25
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 1832ae450e2e384cccb7f2503b558ab48cd8c961
Author: Max Shenfield <mshenfield at eventbrite.com>
Date: Mon Jan 4 20:06:12 2016 +0100
Close #30 PR: Add useEquals options to swap "=" for " ". Fixes #25
---
index.js | 12 +++++++-----
readme.md | 16 ++++++++++++++++
test.js | 16 ++++++++++++++++
3 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/index.js b/index.js
index f337322..68ecd95 100644
--- a/index.js
+++ b/index.js
@@ -1,9 +1,9 @@
'use strict';
var numberIsNan = require('number-is-nan');
-function createArg(key, val) {
+function createArg(key, val, separator) {
key = key.replace(/[A-Z]/g, '-$&').toLowerCase();
- return '--' + key + (val ? '=' + val : '');
+ return '--' + key + (val ? separator + val : '');
}
function match(arr, val) {
@@ -21,6 +21,8 @@ module.exports = function (input, opts) {
opts = opts || {};
+ var separator = opts.useEquals === false ? ' ' : '=';
+
Object.keys(input).forEach(function (key) {
var val = input[key];
var argFn = createArg;
@@ -47,16 +49,16 @@ module.exports = function (input, opts) {
}
if (typeof val === 'string') {
- args.push(argFn(key, val));
+ args.push(argFn(key, val, separator));
}
if (typeof val === 'number' && !numberIsNan(val)) {
- args.push(argFn(key, String(val)));
+ args.push(argFn(key, String(val), separator));
}
if (Array.isArray(val)) {
val.forEach(function (arrVal) {
- args.push(argFn(key, arrVal));
+ args.push(argFn(key, arrVal, separator));
});
}
});
diff --git a/readme.md b/readme.md
index ef65fc0..50eb69c 100644
--- a/readme.md
+++ b/readme.md
@@ -117,6 +117,22 @@ Type: `object`
Maps keys in `input` to an aliased name. Matching keys are converted to options with a single dash ("-") in front of the aliased name and a space separating the aliased name from the value. Keys are still affected by `includes` and `excludes`.
+##### useEquals
+
+Type: `boolean`
+Default: `true`
+
+Setting to `false` switches the separator in generated commands from an equals sign ("=") to a single space (" "). For example:
+
+```js
+console.log(dargs({foo: 'bar'}, {useEquals: false}));
+/*
+[
+ '--foo bar'
+]
+*/
+```
+
##### ignoreFalse
Type: `boolean`
diff --git a/test.js b/test.js
index c3c3470..fbd4ae4 100644
--- a/test.js
+++ b/test.js
@@ -28,6 +28,22 @@ test('convert options to cli flags', t => {
]);
});
+test('useEquals options', t => {
+ t.same(fn(fixture, {
+ useEquals: false
+ }), [
+ '--a foo',
+ '--b',
+ '--no-c',
+ '--d 5',
+ '--e foo',
+ '--e bar',
+ '--h with a space',
+ '--i let\'s try quotes',
+ '--camel-case-camel'
+ ]);
+});
+
test('exclude options', t => {
t.same(fn(fixture, {excludes: ['b', /^e$/, 'h', 'i']}), [
'--a=foo',
--
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