Skip to content

Commit

Permalink
fix: groups were not being maintained for nested commands (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
mleguen authored and bcoe committed Sep 27, 2019
1 parent 9a42b63 commit d38650e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
74 changes: 74 additions & 0 deletions test/usage.js
Expand Up @@ -1807,6 +1807,80 @@ describe('usage tests', () => {
])
})

it('should display global non empty groups for commands', () => {
const r = checkUsage(() => yargs(['upload', '-h'])
.command('upload', 'upload something', yargs => yargs
.option('q', {
type: 'boolean'
})
.wrap(null))
.option('i', {
type: 'boolean',
global: true
})
.option('j', {
type: 'boolean',
global: false // not global so not preserved, even though the group is
})
.group(['i', 'j'], 'Awesome Flags:')
.help('h')
.wrap(null)
.parse()
)

r.logs[0].split('\n').should.deep.equal([
'usage upload',
'',
'upload something',
'',
'Awesome Flags:',
' -i [boolean]',
'',
'Options:',
' --version Show version number [boolean]',
' -h Show help [boolean]',
' -q [boolean]'
])
})

it('should display global non empty groups for subcommands', () => {
const r = checkUsage(() => yargs(['do', 'upload', '-h'])
.command('do', 'do something', yargs => yargs
.command('upload', 'upload something', yargs => yargs
.option('q', {
type: 'boolean'
})
.wrap(null))
.wrap(null))
.option('i', {
type: 'boolean',
global: true
})
.option('j', {
type: 'boolean',
global: false // not global so not preserved, even though the group is
})
.group(['i', 'j'], 'Awesome Flags:')
.help('h')
.wrap(null)
.parse()
)

r.logs[0].split('\n').should.deep.equal([
'usage do upload',
'',
'upload something',
'',
'Awesome Flags:',
' -i [boolean]',
'',
'Options:',
' --version Show version number [boolean]',
' -h Show help [boolean]',
' -q [boolean]'
])
})

it('should list a module command only once', () => {
const r = checkUsage(() => yargs('--help')
.command('upload', 'upload something', {
Expand Down
19 changes: 11 additions & 8 deletions yargs.js
Expand Up @@ -94,14 +94,17 @@ function Yargs (processArgs, cwd, parentRequire) {
})
})

// preserve all groups not set to local.
preservedGroups = Object.keys(groups).reduce((acc, groupName) => {
const keys = groups[groupName].filter(key => !(key in localLookup))
if (keys.length > 0) {
acc[groupName] = keys
}
return acc
}, {})
// add all groups not set to local to preserved groups
Object.assign(
preservedGroups,
Object.keys(groups).reduce((acc, groupName) => {
const keys = groups[groupName].filter(key => !(key in localLookup))
if (keys.length > 0) {
acc[groupName] = keys
}
return acc
}, {})
)
// groups can now be reset
groups = {}

Expand Down

0 comments on commit d38650e

Please sign in to comment.