Shell Completion

This example demonstrates how to enable shell completion for your CLI. The .completion() method registers a completion subcommand and a hidden --get-completions flag that shells use to request dynamic suggestions.

You can customize completions at the command level or per-option. The completionHelpers.files() helper suggests filesystem paths.

All Example Files

FILE EXPLORER
shell-completion.ts
1import cliForge, { completionHelpers } from 'cli-forge';
2
3const app = cliForge('my-app')
4  .completion()
5  .option('verbose', { type: 'boolean', alias: ['v'] })
6  .command('deploy', {
7    description: 'Deploy the application',
8    builder: (args) =>
9      args
10        .option('env', {
11          type: 'string',
12          choices: ['production', 'staging', 'development'] as const,
13          description: 'Target environment',
14        })
15        .option('config', {
16          type: 'string',
17          description: 'Path to config file',
18          completion: completionHelpers.files('*.json'),
19        }),
20    handler: (args) => {
21      console.log(`Deploying to ${args.env} with config ${args.config}`);
22    },
23  })
24  .command('status', {
25    description: 'Show service status',
26    builder: (args) => args,
27    handler: () => {
28      console.log('All services running');
29    },
30  });
31
32export default app;
33
34if (require.main === module) {
35  (async () => {
36    await app.forge();
37  })();
38}
39