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: add usage for single-digit boolean aliases #1580

Merged
merged 2 commits into from Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion lib/usage.js
Expand Up @@ -273,7 +273,15 @@ module.exports = function usage (yargs, y18n) {
// for the special positional group don't
// add '--' or '-' prefix.
if (groupName === self.getPositionalGroupName()) return sw
else return (/^[^0-9]$/.test(sw) ? '-' : '--') + sw
else {
return (
// matches yargs-parser logic in which single-digits
// aliases declared with a boolean type are now valid
/^[0-9]$/.test(sw)
? ~options.boolean.indexOf(key) ? '-' : '--'
: sw.length > 1 ? '--' : '-'
) + sw
}
})
.join(', ')

Expand Down
58 changes: 52 additions & 6 deletions test/usage.js
Expand Up @@ -1204,7 +1204,30 @@ describe('usage tests', () => {
])
})

it('should use 2 dashes for 1-digit key usage', () => {
it('should use 2 dashes for general 1-digit usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('1', {
type: 'string',
description: 'First one',
default: 'first'
})
.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 First one [string] [default: "first"]',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})

it('should use single dashes for 1-digit boolean key usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('1', {
type: 'boolean',
Expand All @@ -1220,13 +1243,13 @@ describe('usage tests', () => {
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --1 Negative one [boolean]',
' -1 Negative one [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
})

it('should use 2 dashes for 1-digit alias usage', () => {
it('should use single dashes for 1-digit boolean alias usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('negativeone', {
alias: '1',
Expand All @@ -1243,9 +1266,32 @@ describe('usage tests', () => {
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]'
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --negativeone, -1 Negative one [boolean]'
])
})

it('should use 2 dashes for multiple-digit alias usage', () => {
const r = checkUsage(() => yargs(['--help'])
.option('onehundred', {
alias: '100',
type: 'boolean',
description: 'one hundred'
})
.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]',
' --onehundred, --100 one hundred [boolean]'
])
})

Expand Down