Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export class YargsParser {
keys: []
}
const negative = /^-([0-9]+(\.[0-9]+)?|\.[0-9]+)$/
const negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
const negatedBoolean = new RegExp(
(configuration['short-option-groups'] ? '^--' : '^--?') + configuration['negation-prefix'] + '(.+)'
)

;([] as ArrayOption[]).concat(opts.array || []).filter(Boolean).forEach(function (opt) {
const key = typeof opt === 'object' ? opt.key : opt
Expand Down
56 changes: 56 additions & 0 deletions test/yargs-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,62 @@ describe('yargs-parser', function () {
})

describe('boolean negation', function () {
it('negates single-dash long options when short option groups are disabled', function () {
const parsed = parser(['-no-window'], {
configuration: { 'short-option-groups': false }
})

expect(parsed.window).to.equal(false)
expect(parsed['no-window']).to.equal(undefined)
})

it('uses the custom negation prefix for single-dash long options', function () {
const parsed = parser(['-without-window'], {
configuration: {
'short-option-groups': false,
'negation-prefix': 'without-'
}
})

expect(parsed.window).to.equal(false)
})

it('recognizes known single-dash negated options with aliases and arrays', function () {
const parsed = parser(['-no-window', '-no-unknown'], {
boolean: ['window'],
array: ['window'],
alias: { window: 'w' },
configuration: {
'short-option-groups': false,
'unknown-options-as-args': true
}
})

expect(parsed.window).to.deep.equal([false])
expect(parsed.w).to.deep.equal([false])
expect(parsed._).to.deep.equal(['-no-unknown'])
})

it('respects disabled boolean negation for single-dash long options', function () {
const parsed = parser(['-no-window'], {
configuration: {
'short-option-groups': false,
'boolean-negation': false
}
})

expect(parsed['no-window']).to.equal(true)
expect(parsed.window).to.equal(undefined)
})

it('preserves short option groups for arguments resembling negated options', function () {
const parsed = parser(['-no-x'])

expect(parsed.n).to.equal(true)
expect(parsed.o).to.equal('-x')
expect(parsed.x).to.equal(undefined)
})

it('does not negate arguments prefixed with --no-', function () {
const parsed = parser(['--no-dice'], {
configuration: {
Expand Down