Skip to content

Commit 8b077c8

Browse files
committed
fix: invoke by cli name
1 parent 296706f commit 8b077c8

3 files changed

Lines changed: 16 additions & 41 deletions

File tree

src/cac.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@ import t, { type RootCommand } from './t';
99

1010
const execPath = process.execPath;
1111
const processArgs = process.argv.slice(1);
12-
const quotedExecPath = quoteIfNeeded(execPath);
13-
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
14-
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
15-
16-
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`;
1712

1813
// Regex to detect if an option takes a value (has <required> or [optional] parameters)
1914
const VALUE_OPTION_RE = /<[^>]+>|\[[^\]]+\]/;
2015

21-
function quoteIfNeeded(path: string): string {
22-
return path.includes(' ') ? `'${path}'` : path;
23-
}
24-
2516
export default async function tab(
2617
instance: CAC,
2718
completionConfig?: CompletionConfig
@@ -122,24 +113,27 @@ export default async function tab(
122113
}
123114

124115
instance.command('complete [shell]').action(async (shell, extra) => {
116+
// invoke the cli by its program name so completion works regardless of how
117+
// the cli is launched (including compiled binaries). see the related issue #135
118+
const execCommand = instance.name;
125119
switch (shell) {
126120
case 'zsh': {
127-
const script = zsh.generate(instance.name, x);
121+
const script = zsh.generate(instance.name, execCommand);
128122
console.log(script);
129123
break;
130124
}
131125
case 'bash': {
132-
const script = bash.generate(instance.name, x);
126+
const script = bash.generate(instance.name, execCommand);
133127
console.log(script);
134128
break;
135129
}
136130
case 'fish': {
137-
const script = fish.generate(instance.name, x);
131+
const script = fish.generate(instance.name, execCommand);
138132
console.log(script);
139133
break;
140134
}
141135
case 'powershell': {
142-
const script = powershell.generate(instance.name, x);
136+
const script = powershell.generate(instance.name, execCommand);
143137
console.log(script);
144138
break;
145139
}

src/citty.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ import { assertDoubleDashes } from './shared';
1515
import type { CompletionConfig } from './shared';
1616
import t, { type RootCommand } from './t';
1717

18-
function quoteIfNeeded(path: string) {
19-
return path.includes(' ') ? `'${path}'` : path;
20-
}
21-
22-
const execPath = process.execPath;
23-
const processArgs = process.argv.slice(1);
24-
const quotedExecPath = quoteIfNeeded(execPath);
25-
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
26-
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
27-
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`;
28-
2918
function isConfigPositional<T extends ArgsDef>(config: CommandDef<T>) {
3019
return (
3120
config.args &&
@@ -200,24 +189,27 @@ export default async function tab<TArgs extends ArgsDef>(
200189
shell = undefined;
201190
}
202191

192+
// the cli by its program name so completion works regardless of
193+
// how the cli is launched (including compiled binaries). see the related issue #135
194+
const execCommand = name;
203195
switch (shell) {
204196
case 'zsh': {
205-
const script = zsh.generate(name, x);
197+
const script = zsh.generate(name, execCommand);
206198
console.log(script);
207199
break;
208200
}
209201
case 'bash': {
210-
const script = bash.generate(name, x);
202+
const script = bash.generate(name, execCommand);
211203
console.log(script);
212204
break;
213205
}
214206
case 'fish': {
215-
const script = fish.generate(name, x);
207+
const script = fish.generate(name, execCommand);
216208
console.log(script);
217209
break;
218210
}
219211
case 'powershell': {
220-
const script = powershell.generate(name, x);
212+
const script = powershell.generate(name, execCommand);
221213
console.log(script);
222214
break;
223215
}

src/commander.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ interface CommandWithRawArgs extends CommanderCommand {
66
rawArgs: string[];
77
}
88

9-
const execPath = process.execPath;
10-
const processArgs = process.argv.slice(1);
11-
const quotedExecPath = quoteIfNeeded(execPath);
12-
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
13-
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
14-
15-
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`;
16-
17-
function quoteIfNeeded(path: string): string {
18-
return path.includes(' ') ? `'${path}'` : path;
19-
}
20-
219
export default function tab(
2210
instance: CommanderCommand,
2311
completionConfig?: { completionCommandName?: string }
@@ -36,7 +24,8 @@ export default function tab(
3624
.choices(['zsh', 'bash', 'fish', 'powershell'])
3725
)
3826
.action((shell) => {
39-
t.setup(programName, x, shell);
27+
// invoke the cli by its program name and not the runtime launch path. see the related issue #135.
28+
t.setup(programName, programName, shell);
4029
});
4130
completionCommand.copyInheritedSettings(instance);
4231

0 commit comments

Comments
 (0)