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

fix: improve algo for collating command id #415

Merged
merged 4 commits into from May 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/help/util.ts
Expand Up @@ -39,28 +39,27 @@ export function template(context: any): (t: string) => string {
function collateSpacedCmdIDFromArgs(argv: string[], config: IConfig): string[] {
if (argv.length === 1) return argv

const ids = collectUsableIds(config.commandIDs)
const findId = (argv: string[]): string | undefined => {
const ids = collectUsableIds(config.commandIDs)

const final: string[] = []
const idPresent = (id: string) => ids.includes(id)
const isFlag = (s: string) => s.startsWith('-')
const isArgWithValue = (s: string) => s.includes('=')
const finalizeId = (s?: string) => s ? [...final, s].join(':') : final.join(':')

const hasSubCommandsWithArgs = () => {
const hasArgs = () => {
const id = finalizeId()
if (!id) return false
// Get a list of sub commands for the current command id. A command is returned as a subcommand if the `id` starts with the current command id.
// e.g. `foo:bar` is a subcommand of `foo`
const subCommands = config.commands.filter(c => (c.id).startsWith(id))
return Boolean(subCommands.find(cmd => cmd.strict === false || cmd.args?.length > 0))
const cmd = config.findCommand(id)
return Boolean(cmd && (cmd.strict === false || cmd.args?.length > 0))
}

for (const arg of argv) {
if (idPresent(finalizeId(arg))) final.push(arg)
// If the parent topic has a command that expects positional arguments, then we cannot
// assume that any subsequent string could be part of the command name
else if (isArgWithValue(arg) || isFlag(arg) || hasSubCommandsWithArgs()) break
else if (isArgWithValue(arg) || isFlag(arg) || hasArgs()) break
else final.push(arg)
}

Expand Down
13 changes: 13 additions & 0 deletions test/help/util.test.ts
Expand Up @@ -65,6 +65,19 @@ describe('util', () => {
expect(actual).to.deep.equal(['foo:ba', '--baz'])
})

test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has args and command is misspelled', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
args: [{name: 'first'}],
})
const actual = standardizeIDFromArgv(['foo', 'ba', 'baz'], config)
expect(actual).to.deep.equal(['foo:ba:baz'])
})

test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has args', () => {
Expand Down