Skip to content

Commit

Permalink
fix: handle invalid sort order options
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Dec 15, 2023
1 parent 858d97c commit 0e8f00c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import {HelpFormatter, HelpSection, HelpSectionRenderer} from './formatter'
// split on any platform, not just the os specific EOL at runtime.
const POSSIBLE_LINE_FEED = /\r\n|\n/

/**
* Determines the sort order of flags. Will default to alphabetical if not set or set to an invalid value.
*/
function determineSortOrder(
flagSortOrder: HelpFormatter['opts']['flagSortOrder'],
): NonNullable<HelpFormatter['opts']['flagSortOrder']> {
if (flagSortOrder === 'alphabetical') return 'alphabetical'
if (flagSortOrder === 'none') return 'none'
return 'alphabetical'
}

export class CommandHelp extends HelpFormatter {
constructor(
public command: Command.Loadable,
Expand Down Expand Up @@ -226,7 +237,7 @@ export class CommandHelp extends HelpFormatter {
})

const flags =
this.opts.flagSortOrder === 'alphabetical' || !this.opts.flagSortOrder
determineSortOrder(this.opts.flagSortOrder) === 'alphabetical'
? sortBy(unsortedFlags, (f) => [!f.char, f.char, f.name])
: unsortedFlags

Expand Down

0 comments on commit 0e8f00c

Please sign in to comment.