WYSIWYG sort (#1400)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Use simpler more WYSIWYG sort of options * Do a WYSIWYG style sort
authored by John Gee · committed by GitHub · parent 689d7a0 · Browse files · 2 files·+29 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+29 added−18 removed
- tests:+24 added−13 removed
- other:+5 added−5 removed
Provenance
- Commit
9b087c8- Tree
076ec0c- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
689d7a0
2 files changed+29 added−18 removed
modified:tests/help.sortOptions.test.js+24 added-13 removed
25 unmodified lines26272829303031323334353637383939404142424344454647484946474849505251525355565455565758596061626364656667686970716162727374756576777825 unmodified lines const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'help', 'zzz']); });
test('when short and long flags then sort on long flag (name)', () => { test('when both short and long flags then sort on short flag', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('-m,--zzz', 'desc') .option('-n,--aaa', 'desc') .option('-o,--bbb', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'help', 'zzz']); expect(visibleOptionNames).toEqual(['help', 'zzz', 'aaa', 'bbb']); });
test('when negated option with positive then sort together with negative after positive', () => { test('when lone short and long flags then sort on lone flag', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('--bbb', 'desc') .option('--ccc', 'desc') .option('--no-bbb', 'desc') .option('--aaa', 'desc'); .option('--zzz', 'desc') .option('--aaa', 'desc') .option('-b', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'no-bbb', 'ccc', 'help']); expect(visibleOptionNames).toEqual(['aaa', 'b', 'help', 'zzz']); });
test('when negated option without positive then still sorts using attribute name', () => { // Sorting '--no-foo' as 'foo' (mainly for when also 'foo' so sort together)! test('when mixed case flags then sort is case insensitive', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('-B', 'desc') .option('-a', 'desc') .option('-c', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['a', 'B', 'c', 'help']); });
test('when negated option then sort negated option separately', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('--bbb', 'desc') .option('--ccc', 'desc') .option('--aaa', 'desc') .option('--no-bbb', 'desc'); .option('--no-bbb', 'desc') .option('--aaa', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'no-bbb', 'ccc', 'help']); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'ccc', 'help', 'no-bbb']); });});modified:index.js+5 added-5 removed
65 unmodified lines66 }67 visibleOptions.push(helpOption);68 }69 if (this.sortOptions) {70+ const getSortKey = (option) => {71+ // WYSIWYG for order displayed in help with short before long, no special handling for negated.72+ return option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, '');73+ };74 visibleOptions.sort((a, b) => {71- const compare = a.attributeName().localeCompare(b.attributeName());72- if (compare === 0) {73- return (a.negate) ? +1 : -1;74- }75- return compare;75+ return getSortKey(a).localeCompare(getSortKey(b));76 });77 }78 return visibleOptions;79 }2093 unmodified lines