Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/curvy-clouds-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bomb.sh/tab': patch
---

Fix PowerShell completion argument parsing
30 changes: 23 additions & 7 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const shells = ['zsh', 'bash', 'fish', 'powershell'];

async function main() {
const args = process.argv.slice(2);
const isPowerShell = process.platform === 'win32' && process.env.PSModulePath;

if (process.env.TAB_DEBUG) {
console.error('RAW ARGS:', process.argv);
}

// <packageManager> complete -- <args>
if (args.length >= 2 && args[1] === 'complete') {
Expand All @@ -23,16 +28,27 @@ async function main() {
}

const dashIndex = process.argv.indexOf('--');
if (dashIndex !== -1) {
const completion = new PackageManagerCompletion(packageManager);
await setupCompletionForPackageManager(packageManager, completion);
const toComplete = process.argv.slice(dashIndex + 1);
await completion.parse(toComplete);
process.exit(0);
} else {
// PowerShell's argument parsing can normalize or drop a `--`
// our "<pm> complete -- <query>" POSIX-style contract doesn't work the same way.
// so we need to handle the PowerShell case separately.
// gh issue discussion: https://github.com/bombshell-dev/tab/issues/82

const completionArgs =
dashIndex !== -1 && (!isPowerShell || dashIndex < process.argv.length - 1)
? process.argv.slice(dashIndex + 1)
: isPowerShell
? args.slice(2)
: null;

if (!completionArgs) {
console.error(`Error: Expected '--' followed by command to complete`);
process.exit(1);
}

const completion = new PackageManagerCompletion(packageManager);
await setupCompletionForPackageManager(packageManager, completion);
await completion.parse(completionArgs);
process.exit(0);
}

// <packageManager> <shell>
Expand Down
Loading