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: complete short options with a single dash #1507

Merged
merged 2 commits into from Jan 2, 2020
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: 5 additions & 2 deletions lib/completion.js
Expand Up @@ -81,11 +81,14 @@ module.exports = function completion (yargs, usage, command) {
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(`--${key}`)
completions.push(dashes + key)
} else {
const desc = descs[key] || ''
completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
completions.push(dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
}
}
})
Expand Down
38 changes: 37 additions & 1 deletion test/completion.js
Expand Up @@ -58,6 +58,42 @@ describe('Completion', () => {
r.logs.should.not.include('--foo')
})

it('completes short options with a single dash when the user did not already enter two dashes', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', ''])
.options({
f: { describe: 'f option', alias: 'foo' }
})
.argv
)

r.logs.should.include('-f')
r.logs.should.not.include('--f')
})

it('completes short options with two dashes when the user already entered two dashes', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', '--'])
.options({
f: { describe: 'f option', alias: 'foo' }
})
.argv
)

r.logs.should.include('--f')
r.logs.should.not.include('-f')
})

it('completes single digit options with two dashes', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', './completion', ''])
.options({
1: { describe: '1 option', alias: 'one' }
})
.argv
)

r.logs.should.include('--1')
r.logs.should.not.include('-1')
})

it('completes options for the correct command', () => {
process.env.SHELL = '/bin/bash'
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o'])
Expand Down Expand Up @@ -98,7 +134,7 @@ describe('Completion', () => {
r.logs.should.include('cmd1')
})

it('does not include possitional arguments', function () {
it('does not include positional arguments', function () {
process.env.SHELL = '/bin/bash'
var r = checkUsage(function () {
return yargs(['./completion', '--get-yargs-completions', 'cmd'])
Expand Down