Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/citty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,7 +155,6 @@ export default async function tab<TArgs extends ArgsDef>(
},
async run(ctx) {
let shell: string | undefined = ctx.rawArgs[0];
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);

if (shell === '--') {
shell = undefined;
Expand Down Expand Up @@ -188,6 +187,9 @@ export default async function tab<TArgs extends ArgsDef>(
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
Expand Down
3 changes: 3 additions & 0 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 -- <args>`;
console.error(errorMessage);
process.exit(1);
}
}