Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/public-bats-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bomb.sh/tab': patch
---

fix: handle completions after inline option values
31 changes: 26 additions & 5 deletions src/t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,21 @@ export class RootCommand extends Command {
if (arg.startsWith('-')) {
i++;

const option = this.findOptionInScope(matchedCommand ?? this, arg);
const [optionName] = arg.split('=');
const hasInlineValue = arg.includes('=');
const option = this.findOptionInScope(
matchedCommand ?? this,
optionName
);
const isBoolean = option?.isBoolean ?? false;

// skip the next argument if this is not a boolean option and the next arg doesn't start with -
if (!isBoolean && i < args.length && !args[i].startsWith('-')) {
if (
!hasInlineValue &&
!isBoolean &&
i < args.length &&
!args[i].startsWith('-')
) {
i++;
}
} else {
Expand Down Expand Up @@ -211,15 +221,17 @@ export class RootCommand extends Command {
if (arg.startsWith('-')) {
i++;

const [optionName] = arg.split('=');
const hasInlineValue = arg.includes('=');
let isBoolean = false;

const rootOption = this.findOption(this, arg);
const rootOption = this.findOption(this, optionName);
if (rootOption) {
isBoolean = rootOption.isBoolean ?? false;
} else {
// subcommand options
for (const [, command] of this.commands) {
const option = this.findOption(command, arg);
const option = this.findOption(command, optionName);
if (option) {
isBoolean = option.isBoolean ?? false;
break;
Expand All @@ -228,7 +240,12 @@ export class RootCommand extends Command {
}

// skip the next argument if this is not a boolean option and the next arg doesn't start with -
if (!isBoolean && i < args.length && !args[i].startsWith('-')) {
if (
!hasInlineValue &&
!isBoolean &&
i < args.length &&
!args[i].startsWith('-')
) {
i++;
}
} else {
Expand Down Expand Up @@ -280,6 +297,10 @@ export class RootCommand extends Command {

// previous argument was an option, check if it expects a value
if (lastPrevArg?.startsWith('-')) {
if (lastPrevArg.includes('=')) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch that need to check here too.

return false;
}

let option: Option | undefined;

if (this.optionLookupMode) {
Expand Down
21 changes: 21 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
});
});

describe('inline option value handling', () => {
test.each(['--port=3000', '-p=3000'])(
'should complete subcommands after %s',
async (option) => {
const output = await runCommand(`${commandPrefix} dev ${option} ""`);
expect(output).toContain('build');
expect(output).toContain('start');
}
);

test.each(['--port=3000', '-p=3000'])(
'should not swallow a following subcommand after %s',
async (option) => {
const output = await runCommand(
`${commandPrefix} dev ${option} build ""`
);
expect(output.trim()).toBe(':4');
}
);
});

describe('boolean option handling', () => {
it('should complete subcommands and arguments after boolean options', async () => {
const command = `${commandPrefix} dev --verbose ""`;
Expand Down
Loading