Skip to content

Commit

Permalink
support all-boolean mode
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper authored and James Halliday committed Jun 19, 2014
1 parent 1f97626 commit 450a97f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ module.exports = function (args, opts) {
if (!opts) opts = {};

var flags = { bools : {}, strings : {} };

[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
flags.bools[key] = true;
});

if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
flags.allBools = true;
} else {
[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
flags.bools[key] = true;
});
}

var aliases = {};
Object.keys(opts.alias || {}).forEach(function (key) {
Expand Down Expand Up @@ -68,6 +72,7 @@ module.exports = function (args, opts) {
var next = args[i + 1];
if (next !== undefined && !/^-/.test(next)
&& !flags.bools[key]
&& !flags.allBools
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, next);
i++;
Expand Down
4 changes: 3 additions & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ options can be:

* `opts.string` - a string or array of strings argument names to always treat as
strings
* `opts.boolean` - a string or array of strings to always treat as booleans
* `opts.boolean` - a boolean, string or array of strings to always treat as
booleans. if `true` will treat all double hyphenated arguments without equal signs
as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
* `opts.alias` - an object mapping string names to strings or arrays of string
argument names to use as aliases
* `opts.default` - an object mapping string argument names to default values
Expand Down
32 changes: 32 additions & 0 deletions test/all_bool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var parse = require('../');
var test = require('tape');

test('flag boolean true (default all --args to boolean)', function (t) {
var argv = parse(['moo', '--honk', 'cow'], {
boolean: true
});

t.deepEqual(argv, {
honk: true,
_: ['moo', 'cow']
});

t.deepEqual(typeof argv.honk, 'boolean');
t.end();
});

test('flag boolean true only affects double hyphen arguments without equals signs', function (t) {
var argv = parse(['moo', '--honk', 'cow', '-p', '55', '--tacos=good'], {
boolean: true
});

t.deepEqual(argv, {
honk: true,
tacos: 'good',
p: '55',
_: ['moo', 'cow']
});

t.deepEqual(typeof argv.honk, 'boolean');
t.end();
});

0 comments on commit 450a97f

Please sign in to comment.