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(completion): takes negated flags into account when boolean-negation is set #1509

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
33 changes: 21 additions & 12 deletions lib/completion.js
Expand Up @@ -76,21 +76,30 @@ module.exports = function completion (yargs, usage, command) {

if (current.match(/^-/) || (current === '' && completions.length === 0)) {
const descs = usage.getDescriptions()
Object.keys(yargs.getOptions().key).forEach((key) => {
const options = yargs.getOptions()
Object.keys(options.key).forEach((key) => {
const negable = !!options.configuration['boolean-negation'] && options.boolean.includes(key)
// If the key and its aliases aren't in 'args', add the key to 'completions'
const keyAndAliases = [key].concat(aliases[key] || [])
const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
if (notInArgs) {
const startsByTwoDashes = s => /^--/.test(s)
const isShortOption = s => /^[^0-9]$/.test(s)
const dashes = !startsByTwoDashes(current) && isShortOption(key) ? '-' : '--'
if (!zshShell) {
completions.push(dashes + key)
} else {
const desc = descs[key] || ''
completions.push(dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
let keyAndAliases = [key].concat(aliases[key] || [])
if (negable) keyAndAliases = keyAndAliases.concat(keyAndAliases.map(key => `no-${key}`))

function completeOptionKey (key) {
const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
if (notInArgs) {
const startsByTwoDashes = s => /^--/.test(s)
const isShortOption = s => /^[^0-9]$/.test(s)
const dashes = !startsByTwoDashes(current) && isShortOption(key) ? '-' : '--'
if (!zshShell) {
completions.push(dashes + key)
} else {
const desc = descs[key] || ''
completions.push(dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
}
}
}

completeOptionKey(key)
if (negable && !!options.default[key]) completeOptionKey(`no-${key}`)
})
}

Expand Down
49 changes: 49 additions & 0 deletions test/completion.js
Expand Up @@ -94,6 +94,55 @@ describe('Completion', () => {
r.logs.should.not.include('-1')
})

it('completes with no- prefix flags defaulting to true when boolean-negation is set', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', ''])
.options({
foo: { describe: 'foo flag', type: 'boolean', default: true },
bar: { describe: 'bar flag', type: 'boolean' }
})
.parserConfiguration({ 'boolean-negation': true })
.argv
)

r.logs.should.include('--no-foo')
r.logs.should.include('--foo')
r.logs.should.not.include('--no-bar')
r.logs.should.include('--bar')
})

it('avoids repeating flags whose negated counterparts are already included', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', '--no-foo', '--no-bar', ''])
.options({
foo: { describe: 'foo flag', type: 'boolean', default: true },
bar: { describe: 'bar flag', type: 'boolean' },
baz: { describe: 'bar flag', type: 'boolean' }
})
.parserConfiguration({ 'boolean-negation': true })
.argv
)

r.logs.should.not.include('--no-foo')
r.logs.should.not.include('--foo')
r.logs.should.not.include('--no-bar')
r.logs.should.not.include('--bar')
r.logs.should.include('--baz')
})

it('ignores no- prefix flags when boolean-negation is not set', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', '--no-bar', ''])
.options({
foo: { describe: 'foo flag', type: 'boolean', default: true },
bar: { describe: 'bar flag', type: 'boolean' }
})
.argv
)

r.logs.should.not.include('--no-foo')
r.logs.should.include('--foo')
r.logs.should.not.include('--no-bar')
r.logs.should.include('--bar')
})

it('completes options for the correct command', () => {
process.env.SHELL = '/bin/bash'
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o'])
Expand Down