Skip to content

Commit 6916ce9

Browse files
trevorlintonbcoe
authored andcommittedFeb 1, 2019
feat: adds config option for sorting command output (#1256)
1 parent 7b200d2 commit 6916ce9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
 

‎docs/api.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,13 @@ for details of this object
11511151

11521152
<a name="parserConfiguration"></a>.parserConfiguration(obj)
11531153
------------
1154-
`parserConfiguration()` allows you to configure yargs. See [yargs-parser's configuration](https://github.com/yargs/yargs-parser#configuration) for
1155-
settings specific to `yargs-parser`.
1154+
`parserConfiguration()` allows you to configure advanced yargs features.
1155+
1156+
`obj` accepts the following configuration options:
1157+
1158+
* `sort-commands` when set to `true` (boolean) will sort the commands added, the default is `false`.
1159+
1160+
For additional configuration options, see [yargs-parser's configuration](https://github.com/yargs/yargs-parser#configuration).
11561161

11571162
<a name="pkg-conf"></a>
11581163
.pkgConf(key, [cwd])

‎lib/usage.js

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ module.exports = function usage (yargs, y18n) {
204204
const context = yargs.getContext()
205205
const parentCommands = context.commands.length ? `${context.commands.join(' ')} ` : ''
206206

207+
if (yargs.getParserConfiguration()['sort-commands'] === true) {
208+
commands = commands.sort((a, b) => a[0].localeCompare(b[0]))
209+
}
210+
207211
commands.forEach((command) => {
208212
const commandString = `${base$0} ${parentCommands}${command[0].replace(/^\$0 ?/, '')}` // drop $0 from default commands.
209213
ui.span(

‎test/usage.js

+26
Original file line numberDiff line numberDiff line change
@@ -2599,6 +2599,32 @@ describe('usage tests', () => {
25992599
])
26002600
})
26012601

2602+
it('should display top-level help with sorting with no command given if sorting enabled', () => {
2603+
const r = checkUsage(() => yargs('--help')
2604+
.command(['list [pattern]', 'ls', '*'], 'List key-value pairs for pattern', {}, noop)
2605+
.command('get <key>', 'Get value for key', {}, noop)
2606+
.command('set <key> [value]', 'Set value for key', {}, noop)
2607+
.parserConfiguration({'sort-commands': true})
2608+
.parse()
2609+
)
2610+
2611+
r.logs[0].split('\n').should.deep.equal([
2612+
'usage [pattern]',
2613+
'',
2614+
'List key-value pairs for pattern',
2615+
'',
2616+
'Commands:',
2617+
' usage get <key> Get value for key',
2618+
' usage list [pattern] List key-value pairs for pattern',
2619+
' [default] [aliases: ls]',
2620+
' usage set <key> [value] Set value for key',
2621+
'',
2622+
'Options:',
2623+
' --help Show help [boolean]',
2624+
' --version Show version number [boolean]'
2625+
])
2626+
})
2627+
26022628
it('should display default command as ./$0 if it has no aliases', () => {
26032629
const r = checkUsage(() => yargs('--help')
26042630
.command('* [pattern]', 'List key-value pairs for pattern', {}, noop)

0 commit comments

Comments
 (0)
Please sign in to comment.