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
22 changes: 16 additions & 6 deletions src/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as bash from './bash';
import * as fish from './fish';
import * as powershell from './powershell';
import type { CAC } from 'cac';
import { Completion } from './';
import { Completion } from './index';
import { CompletionConfig, noopHandler } from './shared';

const execPath = process.execPath;
const processArgs = process.argv.slice(1);
Expand All @@ -17,7 +18,10 @@ function quoteIfNeeded(path: string): string {
return path.includes(' ') ? `'${path}'` : path;
}

export default function tab(instance: CAC): Completion {
export default async function tab(
instance: CAC,
completionConfig?: CompletionConfig
) {
const completion = new Completion();

// Add all commands and their options
Expand All @@ -29,24 +33,30 @@ export default function tab(instance: CAC): Completion {
arg.startsWith('[')
); // true if optional (wrapped in [])

const isRootCommand = cmd.name === '@@global@@';
const commandCompletionConfig = isRootCommand
? completionConfig
: completionConfig?.subCommands?.[cmd.name];

// Add command to completion
const commandName = completion.addCommand(
cmd.name === '@@global@@' ? '' : cmd.name,
isRootCommand ? '' : cmd.name,
cmd.description || '',
args,
async () => []
commandCompletionConfig?.handler ?? noopHandler
);

// Add command options
for (const option of [...instance.globalCommand.options, ...cmd.options]) {
// Extract short flag from the name if it exists (e.g., "-c, --config" -> "c")
const shortFlag = option.name.match(/^-([a-zA-Z]), --/)?.[1];
const argName = option.name.replace(/^-[a-zA-Z], --/, '');

completion.addOption(
commandName,
`--${option.name.replace(/^-[a-zA-Z], --/, '')}`, // Remove the short flag part if it exists
`--${argName}`, // Remove the short flag part if it exists
option.description || '',
async () => [],
commandCompletionConfig?.options?.[argName]?.handler ?? noopHandler,
shortFlag
);
}
Expand Down
20 changes: 2 additions & 18 deletions src/citty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as zsh from './zsh';
import * as bash from './bash';
import * as fish from './fish';
import * as powershell from './powershell';
import { Completion, type Handler } from '.';
import { Completion } from './index';
import type {
ArgsDef,
CommandDef,
PositionalArgDef,
SubCommandsDef,
} from 'citty';
import { generateFigSpec } from './fig';
import { CompletionConfig, noopHandler } from './shared';

function quoteIfNeeded(path: string) {
return path.includes(' ') ? `'${path}'` : path;
Expand All @@ -30,23 +31,6 @@ function isConfigPositional<T extends ArgsDef>(config: CommandDef<T>) {
);
}

// TODO (43081j): use type inference some day, so we can type-check
// that the sub commands exist, the options exist, etc.
interface CompletionConfig {
handler?: Handler;
subCommands?: Record<string, CompletionConfig>;
options?: Record<
string,
{
handler: Handler;
}
>;
}

const noopHandler: Handler = () => {
return [];
};

async function handleSubCommands(
completion: Completion,
subCommands: SubCommandsDef,
Expand Down
18 changes: 18 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Handler } from './index';

export const noopHandler: Handler = () => {
return [];
};

// TODO (43081j): use type inference some day, so we can type-check
// that the sub commands exist, the options exist, etc.
export interface CompletionConfig {
handler?: Handler;
subCommands?: Record<string, CompletionConfig>;
options?: Record<
string,
{
handler: Handler;
}
>;
}