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

fix: show 2 dashes on help for single digit option key or alias #1493

Merged
merged 6 commits into from Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion lib/usage.js
Expand Up @@ -272,7 +272,7 @@ module.exports = function usage (yargs, y18n) {
// for the special positional group don't
// add '--' or '-' prefix.
if (groupName === self.getPositionalGroupName()) return sw
else return (sw.length > 1 ? '--' : '-') + sw
else return (/^[^0-9]$/.test(sw) ? '-' : '--') + sw
})
.join(', ')

Expand Down
45 changes: 45 additions & 0 deletions test/usage.js
Expand Up @@ -1121,6 +1121,51 @@ describe('usage tests', () => {
])
})

it('should use 2 dashes for 1-digit key usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('1', {
type: 'boolean',
description: 'Negative one'
})
.wrap(null)
.parse()
)
r.should.have.property('result')
r.result.should.have.property('_').with.length(0)
r.should.have.property('exit').and.equal(true)
r.should.have.property('errors').with.length(0)
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --1 Negative one [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})

it('should use 2 dashes for 1-digit alias usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('negativeone', {
alias: '1',
type: 'boolean',
description: 'Negative one'
})
.wrap(null)
.parse()
)
r.should.have.property('result')
r.result.should.have.property('_').with.length(0)
r.should.have.property('exit').and.equal(true)
r.should.have.property('errors').with.length(0)
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --negativeone, --1 Negative one [boolean]'
])
})

describe('when exitProcess is false', () => {
it('should not validate arguments (required argument)', () => {
const r = checkUsage(() => yargs(['--help'])
Expand Down