Skip to content

Commit 9c60265

Browse files
authored
feat: introduce single-digit boolean aliases (#255)
1 parent 87e0a21 commit 9c60265

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ function parse (args, opts) {
304304
}
305305
}
306306
}
307+
} else if (arg.match(/^-[0-9]$/) &&
308+
arg.match(negative) &&
309+
checkAllAliases(arg.slice(1), flags.bools)) {
310+
// single-digit boolean alias, e.g: xargs -0
311+
key = arg.slice(1)
312+
setArg(key, defaultValue(key))
307313
} else if (arg === '--') {
308314
notFlags = args.slice(i + 1)
309315
break
@@ -347,7 +353,6 @@ function parse (args, opts) {
347353
}
348354

349355
if (configuration['strip-aliased']) {
350-
// XXX Switch to [].concat(...Object.values(aliases)) once node.js 6 is dropped
351356
;[].concat(...Object.keys(aliases).map(k => aliases[k])).forEach(alias => {
352357
if (configuration['camel-case-expansion']) {
353358
delete argv[alias.split('.').map(prop => camelCase(prop)).join('.')]

test/yargs-parser.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,47 @@ describe('yargs-parser', function () {
438438
argv.should.have.property('zm', 55)
439439
argv.should.have.property('f', 11)
440440
})
441+
442+
it('should set single-digit boolean alias', function () {
443+
var argv = parser(['-f', '11', '-0'], {
444+
alias: {
445+
0: 'print0'
446+
},
447+
boolean: [
448+
'print0'
449+
]
450+
})
451+
argv.should.have.property('print0', true)
452+
argv.should.have.property('f', 11)
453+
})
454+
455+
it('should not set single-digit alias if no alias defined', function () {
456+
var argv = parser(['-f', '11', '-0', '-1'])
457+
argv.should.have.property('f', 11)
458+
argv._.should.deep.equal([-0, -1])
459+
})
460+
461+
it('should not set single-digit boolean alias if no boolean defined', function () {
462+
var argv = parser(['-f', '11', '-9'], {
463+
alias: {
464+
0: 'print0'
465+
}
466+
})
467+
argv.should.have.property('f', 11)
468+
argv._.should.deep.equal([-9])
469+
})
470+
471+
it('should be able to negate set single-digit boolean alias', function () {
472+
var argv = parser(['--no-9'], {
473+
alias: {
474+
9: 'max'
475+
},
476+
boolean: [
477+
'max'
478+
]
479+
})
480+
argv.should.have.property('max', false)
481+
})
441482
})
442483

443484
it('should assign data after forward slash to the option before the slash', function () {

0 commit comments

Comments
 (0)