diff --git a/test/usage.js b/test/usage.js index 37052714d..ea252403c 100644 --- a/test/usage.js +++ b/test/usage.js @@ -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', { diff --git a/yargs.js b/yargs.js index a4ed28e5a..8280ae5aa 100644 --- a/yargs.js +++ b/yargs.js @@ -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 = {}