|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * Shell completion example |
| 4 | + * |
| 5 | + * Demonstrates how to enable shell completion for a CLI. |
| 6 | + * |
| 7 | + * To enable completions for this example: |
| 8 | + * |
| 9 | + * # Bash (add to ~/.bashrc) |
| 10 | + * |
| 11 | + * Npx tsx examples/completion.ts --completion-script bash >> ~/.bashrc source |
| 12 | + * ~/.bashrc |
| 13 | + * |
| 14 | + * # Zsh (add to ~/.zshrc) |
| 15 | + * |
| 16 | + * Npx tsx examples/completion.ts --completion-script zsh >> ~/.zshrc source |
| 17 | + * ~/.zshrc |
| 18 | + * |
| 19 | + * # Fish (save to completions directory) |
| 20 | + * |
| 21 | + * Npx tsx examples/completion.ts --completion-script fish > |
| 22 | + * ~/.config/fish/completions/completion-demo.fish |
| 23 | + * |
| 24 | + * Then try pressing TAB after typing partial commands or options. |
| 25 | + * |
| 26 | + * Usage: npx tsx examples/completion.ts build --target prod npx tsx |
| 27 | + * examples/completion.ts test --coverage npx tsx examples/completion.ts lint |
| 28 | + * --fix |
| 29 | + */ |
| 30 | +import { bargs, opt, pos } from '../src/index.js'; |
| 31 | + |
| 32 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 33 | +// GLOBAL OPTIONS |
| 34 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 35 | + |
| 36 | +const globalOptions = opt.options({ |
| 37 | + config: opt.string({ |
| 38 | + aliases: ['c'], |
| 39 | + description: 'Path to config file', |
| 40 | + }), |
| 41 | + verbose: opt.boolean({ |
| 42 | + aliases: ['v'], |
| 43 | + default: false, |
| 44 | + description: 'Enable verbose output', |
| 45 | + }), |
| 46 | +}); |
| 47 | + |
| 48 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 49 | +// BUILD COMMAND |
| 50 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 51 | + |
| 52 | +const buildParser = opt.options({ |
| 53 | + minify: opt.boolean({ |
| 54 | + aliases: ['m'], |
| 55 | + default: false, |
| 56 | + description: 'Minify output', |
| 57 | + }), |
| 58 | + // Enum option - completions will suggest these choices |
| 59 | + target: opt.enum(['dev', 'staging', 'prod'], { |
| 60 | + aliases: ['t'], |
| 61 | + default: 'dev', |
| 62 | + description: 'Build target environment', |
| 63 | + }), |
| 64 | +}); |
| 65 | + |
| 66 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 67 | +// TEST COMMAND |
| 68 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 69 | + |
| 70 | +const testParser = pos.positionals( |
| 71 | + // Enum positional - completions will suggest these choices |
| 72 | + pos.enum(['unit', 'integration', 'e2e'], { |
| 73 | + description: 'Test type to run', |
| 74 | + name: 'type', |
| 75 | + }), |
| 76 | +)( |
| 77 | + opt.options({ |
| 78 | + coverage: opt.boolean({ |
| 79 | + default: false, |
| 80 | + description: 'Collect coverage', |
| 81 | + }), |
| 82 | + watch: opt.boolean({ |
| 83 | + aliases: ['w'], |
| 84 | + default: false, |
| 85 | + description: 'Watch for changes', |
| 86 | + }), |
| 87 | + }), |
| 88 | +); |
| 89 | + |
| 90 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 91 | +// LINT COMMAND |
| 92 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 93 | + |
| 94 | +const lintParser = opt.options({ |
| 95 | + fix: opt.boolean({ |
| 96 | + default: false, |
| 97 | + description: 'Auto-fix issues', |
| 98 | + }), |
| 99 | + // Enum option - completions will suggest these choices |
| 100 | + format: opt.enum(['stylish', 'json', 'compact'], { |
| 101 | + default: 'stylish', |
| 102 | + description: 'Output format', |
| 103 | + }), |
| 104 | +}); |
| 105 | + |
| 106 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 107 | +// CLI |
| 108 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 109 | + |
| 110 | +await bargs('completion', { |
| 111 | + // Enable shell completion support! |
| 112 | + completion: true, |
| 113 | + description: 'Example CLI with shell completion support', |
| 114 | + version: '1.0.0', |
| 115 | +}) |
| 116 | + .globals(globalOptions) |
| 117 | + .command( |
| 118 | + 'build', |
| 119 | + buildParser, |
| 120 | + ({ values }) => { |
| 121 | + console.log('Building for:', values.target); |
| 122 | + console.log('Minify:', values.minify); |
| 123 | + if (values.verbose) { |
| 124 | + console.log('Config:', values.config ?? '(default)'); |
| 125 | + } |
| 126 | + }, |
| 127 | + { aliases: ['b'], description: 'Build the project' }, |
| 128 | + ) |
| 129 | + .command( |
| 130 | + 'test', |
| 131 | + testParser, |
| 132 | + ({ positionals, values }) => { |
| 133 | + console.log('Running tests:', positionals[0] ?? 'all'); |
| 134 | + console.log('Coverage:', values.coverage); |
| 135 | + console.log('Watch:', values.watch); |
| 136 | + if (values.verbose) { |
| 137 | + console.log('Config:', values.config ?? '(default)'); |
| 138 | + } |
| 139 | + }, |
| 140 | + { aliases: ['t'], description: 'Run tests' }, |
| 141 | + ) |
| 142 | + .command( |
| 143 | + 'lint', |
| 144 | + lintParser, |
| 145 | + ({ values }) => { |
| 146 | + console.log('Linting with format:', values.format); |
| 147 | + console.log('Fix:', values.fix); |
| 148 | + if (values.verbose) { |
| 149 | + console.log('Config:', values.config ?? '(default)'); |
| 150 | + } |
| 151 | + }, |
| 152 | + { aliases: ['l'], description: 'Lint source files' }, |
| 153 | + ) |
| 154 | + .parseAsync(); |
0 commit comments