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

feat: add flagSortOrder to help options #896

Merged
merged 5 commits into from
Dec 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions 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 @@ -218,15 +229,17 @@ export class CommandHelp extends HelpFormatter {

generate(): string {
const cmd = this.command
const flags = sortBy(
Object.entries(cmd.flags || {})
.filter(([, v]) => !v.hidden)
.map(([k, v]) => {
v.name = k
return v
}),
(f) => [!f.char, f.char, f.name],
)
const unsortedFlags = Object.entries(cmd.flags || {})
.filter(([, v]) => !v.hidden)
.map(([k, v]) => {
v.name = k
return v
})

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

const args = Object.values(ensureArgObject(cmd.args)).filter((a) => !a.hidden)
const output = compact(
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export interface HelpOptions {
* Use docopts as the usage. Defaults to true.
*/
docopts?: boolean
/**
* Order in which to sort flags in help output. Defaults to `alphabetical`.
*
* `alphabetical`: Sort flags alphabetically. All flags with short characters will come first.
* `none`: Do not sort flags. They will appear in the order in which they were defined on the command.
*/
flagSortOrder?: 'alphabetical' | 'none'
/**
* If true, hide command aliases from the root help output. Defaults to false.
*/
Expand Down
78 changes: 78 additions & 0 deletions test/help/format-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,81 @@ EXAMPLES
})
})
})

describe('formatCommand with flagSortOrder', () => {
let config: Config

before(async () => {
config = await Config.load(process.cwd())
})

it('should not sort flags when set to "none"', async () => {
const help = new TestHelp(config, {flagSortOrder: 'none'})
const cmd = await makeLoadable(
makeCommandClass({
id: 'apps:create',
flags: {
cFlag: flags.string({char: 'c'}),
aFlag: flags.string({char: 'a'}),
bFlag: flags.string({char: 'b'}),
},
}),
)

const output = help.formatCommand(cmd)
expect(output).to.equal(`USAGE
$ oclif apps:create [-c <value>] [-a <value>] [-b <value>]

FLAGS
-c, --cFlag=<value>
-a, --aFlag=<value>
-b, --bFlag=<value>`)
})

it('should sort flags alphabetically when flagSortOrder is not set', async () => {
const help = new TestHelp(config)
const cmd = await makeLoadable(
makeCommandClass({
id: 'apps:create',
flags: {
cFlag: flags.string({char: 'c'}),
aFlag: flags.string({char: 'a'}),
bFlag: flags.string({char: 'b'}),
},
}),
)

const output = help.formatCommand(cmd)
expect(output).to.equal(`USAGE
$ oclif apps:create [-c <value>] [-a <value>] [-b <value>]

FLAGS
-a, --aFlag=<value>
-b, --bFlag=<value>
-c, --cFlag=<value>`)
})

it('should sort flags alphabetically when flagSortOrder is invalid', async () => {
// @ts-expect-error because we're testing an invalid flagSortOrder
const help = new TestHelp(config, {flagSortOrder: 'INVALID'})
const cmd = await makeLoadable(
makeCommandClass({
id: 'apps:create',
flags: {
cFlag: flags.string({char: 'c'}),
aFlag: flags.string({char: 'a'}),
bFlag: flags.string({char: 'b'}),
},
}),
)

const output = help.formatCommand(cmd)
expect(output).to.equal(`USAGE
$ oclif apps:create [-c <value>] [-a <value>] [-b <value>]

FLAGS
-a, --aFlag=<value>
-b, --bFlag=<value>
-c, --cFlag=<value>`)
})
})
Loading