Skip to content

Commit

Permalink
feat: introduce single-digit boolean aliases (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Mar 7, 2020
1 parent 87e0a21 commit 9c60265
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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('.')]
Expand Down
41 changes: 41 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 9c60265

Please sign in to comment.