From 9c60265fd7a03cb98e6df3e32c8c5e7508d9f56f Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Sat, 7 Mar 2020 14:33:25 -0500 Subject: [PATCH] feat: introduce single-digit boolean aliases (#255) --- index.js | 7 ++++++- test/yargs-parser.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a0367bd3..5c6547d9 100644 --- a/index.js +++ b/index.js @@ -304,6 +304,12 @@ function parse (args, opts) { } } } + } else if (arg.match(/^-[0-9]$/) && + arg.match(negative) && + checkAllAliases(arg.slice(1), flags.bools)) { + // single-digit boolean alias, e.g: xargs -0 + key = arg.slice(1) + setArg(key, defaultValue(key)) } else if (arg === '--') { notFlags = args.slice(i + 1) break @@ -347,7 +353,6 @@ function parse (args, opts) { } if (configuration['strip-aliased']) { - // XXX Switch to [].concat(...Object.values(aliases)) once node.js 6 is dropped ;[].concat(...Object.keys(aliases).map(k => aliases[k])).forEach(alias => { if (configuration['camel-case-expansion']) { delete argv[alias.split('.').map(prop => camelCase(prop)).join('.')] diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 4a16cacc..3e138998 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -438,6 +438,47 @@ describe('yargs-parser', function () { argv.should.have.property('zm', 55) argv.should.have.property('f', 11) }) + + it('should set single-digit boolean alias', function () { + var argv = parser(['-f', '11', '-0'], { + alias: { + 0: 'print0' + }, + boolean: [ + 'print0' + ] + }) + argv.should.have.property('print0', true) + argv.should.have.property('f', 11) + }) + + it('should not set single-digit alias if no alias defined', function () { + var argv = parser(['-f', '11', '-0', '-1']) + argv.should.have.property('f', 11) + argv._.should.deep.equal([-0, -1]) + }) + + it('should not set single-digit boolean alias if no boolean defined', function () { + var argv = parser(['-f', '11', '-9'], { + alias: { + 0: 'print0' + } + }) + argv.should.have.property('f', 11) + argv._.should.deep.equal([-9]) + }) + + it('should be able to negate set single-digit boolean alias', function () { + var argv = parser(['--no-9'], { + alias: { + 9: 'max' + }, + boolean: [ + 'max' + ] + }) + argv.should.have.property('max', false) + }) }) it('should assign data after forward slash to the option before the slash', function () {