diff --git a/src/cac.ts b/src/cac.ts index 7c1d219..8ddc14e 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -4,7 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { CAC } from 'cac'; import { Completion } from './index'; -import { CompletionConfig, noopHandler } from './shared'; +import { CompletionConfig, noopHandler, assertDoubleDashes } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); @@ -85,7 +85,9 @@ export default async function tab( break; } default: { - const args: string[] = extra['--']; + assertDoubleDashes(instance.name); + + const args: string[] = extra['--'] || []; instance.showHelpOnExit = false; // Parse current command context diff --git a/src/citty.ts b/src/citty.ts index e786cd1..f7affe4 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -11,7 +11,7 @@ import type { SubCommandsDef, } from 'citty'; import { generateFigSpec } from './fig'; -import { CompletionConfig, noopHandler } from './shared'; +import { CompletionConfig, noopHandler, assertDoubleDashes } from './shared'; function quoteIfNeeded(path: string) { return path.includes(' ') ? `'${path}'` : path; @@ -155,7 +155,6 @@ export default async function tab( }, async run(ctx) { let shell: string | undefined = ctx.rawArgs[0]; - const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); if (shell === '--') { shell = undefined; @@ -188,6 +187,9 @@ export default async function tab( break; } default: { + assertDoubleDashes(name); + + const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); // const args = (await resolve(instance.args))!; // const parsed = parseArgs(extra, args); // TODO: this is not ideal at all diff --git a/src/commander.ts b/src/commander.ts index bcdd5d4..7a9d842 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -4,6 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { Command as CommanderCommand } from 'commander'; import { Completion } from './'; +import { assertDoubleDashes } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); @@ -79,6 +80,8 @@ export default function tab(instance: CommanderCommand): Completion { break; } default: { + assertDoubleDashes(programName); + // Parse current command context for autocompletion return completion.parse(extra); } diff --git a/src/shared.ts b/src/shared.ts index 23e4acf..798b525 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -16,3 +16,13 @@ export interface CompletionConfig { } >; } + +export function assertDoubleDashes(programName: string = 'cli'): void { + const dashDashIndex = process.argv.indexOf('--'); + + if (dashDashIndex === -1) { + const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- `; + console.error(errorMessage); + process.exit(1); + } +}