Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce single-digit boolean aliases #255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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