Skip to content

Commit

Permalink
fix: command name parsing when flag=value present (#231)
Browse files Browse the repository at this point in the history
* fix: command name parsing when flag=value present

* fix: improve solution and add tests
  • Loading branch information
mdonnalley committed Aug 18, 2021
1 parent 7e38b93 commit 6497514
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/help/util.ts
Expand Up @@ -44,18 +44,19 @@ function collateSpacedCmdIDFromArgs(argv: string[], config: IConfig): string[] {
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 subCommands = config.commands.filter(c => (c.id).startsWith(finalizeId()))
return Boolean(subCommands.find(cmd => cmd.strict === false || cmd.args.length > 0))
return Boolean(subCommands.find(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 (isFlag(arg) || hasSubCommandsWithArgs()) break
else if (isArgWithValue(arg) || isFlag(arg) || hasSubCommandsWithArgs()) break
else final.push(arg)
}

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

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has variable arguments and flags', () => {
config.topicSeparator = ' '
config.commands.push({
id: 'foo:bar',
strict: false,
} as any)
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz', '--hello'], config)
expect(actual).to.deep.equal(['foo:bar', 'baz', '--hello'])
})

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return full id when topic separator is a space and does not have arguments', () => {
Expand All @@ -102,5 +114,45 @@ describe('util', () => {
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
expect(actual).to.deep.equal(['foo:bar:baz'])
})

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has arg with value', () => {
config.topicSeparator = ' '
config.commands.push({id: 'foo:bar'} as any)
const actual = standardizeIDFromArgv(['foo', 'bar', 'hello=world'], config)
expect(actual).to.deep.equal(['foo:bar', 'hello=world'])
})

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has variable args with value', () => {
config.topicSeparator = ' '
config.commands.push({id: 'foo:bar', strict: false} as any)
const actual = standardizeIDFromArgv(['foo', 'bar', 'hello=world', 'my-arg=value'], config)
expect(actual).to.deep.equal(['foo:bar', 'hello=world', 'my-arg=value'])
})

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has flags', () => {
config.topicSeparator = ' '
config.commands.push({id: 'foo:bar'} as any)
const actual = standardizeIDFromArgv(['foo', 'bar', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', '--baz'])
})

test
.stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has flags, arg, and arg with value', () => {
config.topicSeparator = ' '
config.commands.push({
id: 'foo:bar',
args: [{name: 'my-arg'}],
strict: true,
} as any)
const actual = standardizeIDFromArgv(['foo', 'bar', 'my-arg', 'hello=world', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', 'my-arg', 'hello=world', '--baz'])
})
})
})

0 comments on commit 6497514

Please sign in to comment.