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: groups were not being maintained for nested commands #1430

Merged
merged 2 commits into from Sep 27, 2019
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
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