Skip to content

Commit

Permalink
fix: show 2 dashes on help for single digit option key or alias (#1493)
Browse files Browse the repository at this point in the history
To be coherent with what yargs/parser considers as a single dash key
  • Loading branch information
mleguen committed Dec 2, 2019
1 parent 490f601 commit 63b3dd3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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

0 comments on commit 63b3dd3

Please sign in to comment.