Skip to content

Commit 4226416

Browse files
committed
update
1 parent a7c51bf commit 4226416

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed

src/cac.ts

Lines changed: 3 additions & 9 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 { CAC } from 'cac';
66
import { Completion } from './index';
7-
import { CompletionConfig, noopHandler } from './shared';
7+
import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared';
88

99
const execPath = process.execPath;
1010
const processArgs = process.argv.slice(1);
@@ -24,10 +24,6 @@ export default async function tab(
2424
) {
2525
const completion = new Completion();
2626

27-
// a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this?
28-
const dashDashIndex = process.argv.indexOf('--');
29-
const wasDashDashProvided = dashDashIndex !== -1;
30-
3127
// Add all commands and their options
3228
for (const cmd of [instance.globalCommand, ...instance.commands]) {
3329
if (cmd.name === 'complete') continue; // Skip completion command
@@ -89,12 +85,10 @@ export default async function tab(
8985
break;
9086
}
9187
default: {
92-
if (!wasDashDashProvided) {
93-
console.error(
94-
'Error: You need to use -- to separate completion arguments'
95-
);
88+
if (!requireDashDashSeparator(instance.name)) {
9689
return;
9790
}
91+
9892
const args: string[] = extra['--'] || [];
9993
instance.showHelpOnExit = false;
10094

src/citty.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
SubCommandsDef,
1212
} from 'citty';
1313
import { generateFigSpec } from './fig';
14-
import { CompletionConfig, noopHandler } from './shared';
14+
import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared';
1515

1616
function quoteIfNeeded(path: string) {
1717
return path.includes(' ') ? `'${path}'` : path;
@@ -98,10 +98,6 @@ export default async function tab<TArgs extends ArgsDef>(
9898
) {
9999
const completion = new Completion();
100100

101-
// a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this?
102-
const dashDashIndex = process.argv.indexOf('--');
103-
const wasDashDashProvided = dashDashIndex !== -1;
104-
105101
const meta = await resolve(instance.meta);
106102

107103
if (!meta) {
@@ -191,11 +187,7 @@ export default async function tab<TArgs extends ArgsDef>(
191187
break;
192188
}
193189
default: {
194-
// check if -- separator was provided
195-
if (!wasDashDashProvided) {
196-
console.error(
197-
'Error: You need to use -- to separate completion arguments'
198-
);
190+
if (!requireDashDashSeparator(name)) {
199191
return;
200192
}
201193

src/commander.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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';
78

89
const execPath = process.execPath;
910
const processArgs = process.argv.slice(1);
@@ -21,10 +22,6 @@ export default function tab(instance: CommanderCommand): Completion {
2122
const completion = new Completion();
2223
const programName = instance.name();
2324

24-
// a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this?
25-
const dashDashIndex = process.argv.indexOf('--');
26-
const wasDashDashProvided = dashDashIndex !== -1;
27-
2825
// Process the root command
2926
processRootCommand(completion, instance, programName);
3027

@@ -83,11 +80,7 @@ export default function tab(instance: CommanderCommand): Completion {
8380
break;
8481
}
8582
default: {
86-
// check if -- separator is provided
87-
if (!wasDashDashProvided) {
88-
console.error(
89-
'Error: You need to use -- to separate completion arguments'
90-
);
83+
if (!requireDashDashSeparator(programName)) {
9184
return;
9285
}
9386

src/shared.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ export interface CompletionConfig {
1616
}
1717
>;
1818
}
19+
20+
export function requireDashDashSeparator(programName: string): boolean {
21+
const dashDashIndex = process.argv.indexOf('--');
22+
const wasDashDashProvided = dashDashIndex !== -1;
23+
24+
if (!wasDashDashProvided) {
25+
console.error('Error: You need to use -- to separate completion arguments');
26+
}
27+
28+
return wasDashDashProvided;
29+
}

0 commit comments

Comments
 (0)