Skip to content

Commit 5819ed9

Browse files
committed
fix: validating args (#25)
* fix espace args * update * prettier * update * prettier * throw error * update
1 parent c3f3b13 commit 5819ed9

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/cac.ts

Lines changed: 4 additions & 2 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, assertDoubleDashes } from './shared';
88

99
const execPath = process.execPath;
1010
const processArgs = process.argv.slice(1);
@@ -85,7 +85,9 @@ export default async function tab(
8585
break;
8686
}
8787
default: {
88-
const args: string[] = extra['--'];
88+
assertDoubleDashes(instance.name);
89+
90+
const args: string[] = extra['--'] || [];
8991
instance.showHelpOnExit = false;
9092

9193
// Parse current command context

src/citty.ts

Lines changed: 4 additions & 2 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, assertDoubleDashes } from './shared';
1515

1616
function quoteIfNeeded(path: string) {
1717
return path.includes(' ') ? `'${path}'` : path;
@@ -155,7 +155,6 @@ export default async function tab<TArgs extends ArgsDef>(
155155
},
156156
async run(ctx) {
157157
let shell: string | undefined = ctx.rawArgs[0];
158-
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);
159158

160159
if (shell === '--') {
161160
shell = undefined;
@@ -188,6 +187,9 @@ export default async function tab<TArgs extends ArgsDef>(
188187
break;
189188
}
190189
default: {
190+
assertDoubleDashes(name);
191+
192+
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);
191193
// const args = (await resolve(instance.args))!;
192194
// const parsed = parseArgs(extra, args);
193195
// TODO: this is not ideal at all

src/commander.ts

Lines changed: 3 additions & 0 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 { assertDoubleDashes } from './shared';
78

89
const execPath = process.execPath;
910
const processArgs = process.argv.slice(1);
@@ -79,6 +80,8 @@ export default function tab(instance: CommanderCommand): Completion {
7980
break;
8081
}
8182
default: {
83+
assertDoubleDashes(programName);
84+
8285
// Parse current command context for autocompletion
8386
return completion.parse(extra);
8487
}

src/shared.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ export interface CompletionConfig {
1616
}
1717
>;
1818
}
19+
20+
export function assertDoubleDashes(programName: string = 'cli'): void {
21+
const dashDashIndex = process.argv.indexOf('--');
22+
23+
if (dashDashIndex === -1) {
24+
const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- <args>`;
25+
console.error(errorMessage);
26+
process.exit(1);
27+
}
28+
}

0 commit comments

Comments
 (0)