Skip to content

Commit 8a16758

Browse files
committed
update
1 parent b213b69 commit 8a16758

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

src/cac.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Completion } from './index';
77
import {
88
CompletionConfig,
99
noopHandler,
10-
requireDashDashSeparator,
10+
assertDoubleDashes,
1111
} from './shared';
1212

1313
const execPath = process.execPath;
@@ -89,23 +89,25 @@ export default async function tab(
8989
break;
9090
}
9191
default: {
92-
if (!requireDashDashSeparator(instance.name)) {
93-
return;
94-
}
92+
try {
93+
assertDoubleDashes(instance.name);
9594

96-
const args: string[] = extra['--'] || [];
97-
instance.showHelpOnExit = false;
95+
const args: string[] = extra['--'] || [];
96+
instance.showHelpOnExit = false;
9897

99-
// Parse current command context
100-
instance.unsetMatchedCommand();
101-
instance.parse([execPath, processArgs[0], ...args], {
102-
run: false,
103-
});
98+
// Parse current command context
99+
instance.unsetMatchedCommand();
100+
instance.parse([execPath, processArgs[0], ...args], {
101+
run: false,
102+
});
104103

105-
// const matchedCommand = instance.matchedCommand?.name || '';
106-
// const potentialCommand = args.join(' ')
107-
// console.log(potentialCommand)
108-
return completion.parse(args);
104+
// const matchedCommand = instance.matchedCommand?.name || '';
105+
// const potentialCommand = args.join(' ')
106+
// console.log(potentialCommand)
107+
return completion.parse(args);
108+
} catch (error) {
109+
return;
110+
}
109111
}
110112
}
111113
});

src/citty.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { generateFigSpec } from './fig';
1414
import {
1515
CompletionConfig,
1616
noopHandler,
17-
requireDashDashSeparator,
17+
assertDoubleDashes,
1818
} from './shared';
1919

2020
function quoteIfNeeded(path: string) {
@@ -191,19 +191,21 @@ export default async function tab<TArgs extends ArgsDef>(
191191
break;
192192
}
193193
default: {
194-
if (!requireDashDashSeparator(name)) {
194+
try {
195+
assertDoubleDashes(name);
196+
197+
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);
198+
// const args = (await resolve(instance.args))!;
199+
// const parsed = parseArgs(extra, args);
200+
// TODO: this is not ideal at all
201+
// const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170
202+
// TODO: `command lint i` does not work because `lint` and `i` are potential commands
203+
// instead the potential command should only be `lint`
204+
// and `i` is the to be completed part
205+
return completion.parse(extra);
206+
} catch (error) {
195207
return;
196208
}
197-
198-
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);
199-
// const args = (await resolve(instance.args))!;
200-
// const parsed = parseArgs(extra, args);
201-
// TODO: this is not ideal at all
202-
// const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170
203-
// TODO: `command lint i` does not work because `lint` and `i` are potential commands
204-
// instead the potential command should only be `lint`
205-
// and `i` is the to be completed part
206-
return completion.parse(extra);
207209
}
208210
}
209211
},

src/commander.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fish from './fish';
44
import * as powershell from './powershell';
55
import type { Command as CommanderCommand } from 'commander';
66
import { Completion } from './';
7-
import { requireDashDashSeparator } from './shared';
7+
import { assertDoubleDashes } from './shared';
88

99
const execPath = process.execPath;
1010
const processArgs = process.argv.slice(1);
@@ -80,12 +80,14 @@ export default function tab(instance: CommanderCommand): Completion {
8080
break;
8181
}
8282
default: {
83-
if (!requireDashDashSeparator(programName)) {
83+
try {
84+
assertDoubleDashes(programName);
85+
86+
// Parse current command context for autocompletion
87+
return completion.parse(extra);
88+
} catch (error) {
8489
return;
8590
}
86-
87-
// Parse current command context for autocompletion
88-
return completion.parse(extra);
8991
}
9092
}
9193
});

src/shared.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ export interface CompletionConfig {
1717
>;
1818
}
1919

20-
export function requireDashDashSeparator(programName: string): boolean {
20+
export function assertDoubleDashes(programName: string = 'cli'): void {
2121
const dashDashIndex = process.argv.indexOf('--');
22-
const wasDashDashProvided = dashDashIndex !== -1;
2322

24-
if (!wasDashDashProvided) {
25-
console.error('Error: You need to use -- to separate completion arguments');
23+
if (dashDashIndex === -1) {
24+
const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- <args>`;
25+
console.error(errorMessage);
2626
}
27-
28-
return wasDashDashProvided;
2927
}

0 commit comments

Comments
 (0)