|
| 1 | +# @deb/command 🛠️ |
| 2 | + |
| 3 | +> A lightweight, type-safe CLI command builder for Deno, Node.js, and browsers. |
| 4 | +
|
| 5 | +## [](https://jsr.io/@deb/command) |
| 6 | + |
| 7 | +## Features ✨ |
| 8 | + |
| 9 | +- 🧩 **Type-safe options & arguments** – Full TypeScript inference for flags, values, and variadics |
| 10 | +- 🌳 **Nested subcommands** – Build complex CLI hierarchies with ease |
| 11 | +- 🚦 **Smart parsing** – Supports `--flag`, `-f`, `--option=value`, variadic args, and more |
| 12 | +- 📋 **Auto-generated help & version** – Built-in `--help` and `--version` with beautiful formatting |
| 13 | +- ✅ **Validation & defaults** – Enforce required fields, choices, and default values at parse time |
| 14 | +- 🔒 **Zero dependencies** – Pure TypeScript, works everywhere |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Installation 📦 |
| 19 | + |
| 20 | +- **Deno**: |
| 21 | + |
| 22 | + ```bash |
| 23 | + deno add jsr:@deb/command |
| 24 | + ``` |
| 25 | + |
| 26 | +- **Node.js (18+) or Browsers**: |
| 27 | + ```bash |
| 28 | + pnpm i jsr:@dep/table |
| 29 | + ``` |
| 30 | + Then import as an ES module: |
| 31 | + ```typescript |
| 32 | + import { Command } from '@deb/command'; |
| 33 | + ``` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Usage 🎯 |
| 38 | + |
| 39 | +### CLI 💻 |
| 40 | + |
| 41 | +```ts |
| 42 | +// cli.ts |
| 43 | +import { Command, CommandError } from '@deb/command'; |
| 44 | + |
| 45 | +const cli = new Command() |
| 46 | + .name('my-cli') |
| 47 | + .description('Does something awesome') |
| 48 | + .version('2.0.0') |
| 49 | + .option('--dry-run', { |
| 50 | + kind: 'flag', |
| 51 | + shortFlag: '-n', |
| 52 | + description: 'Don’t execute, just simulate', |
| 53 | + }) |
| 54 | + .option('--output', { |
| 55 | + kind: 'value', |
| 56 | + shortFlag: '-o', |
| 57 | + description: 'Output file path', |
| 58 | + }) |
| 59 | + .option('--tags', { kind: 'variadic', description: 'List of tags' }) |
| 60 | + .argument('files', { kind: 'variadic', description: 'Files to process' }) |
| 61 | + .handler(async ({ options, args }) => { |
| 62 | + console.log('Dry run:', options.dryRun); |
| 63 | + console.log('Output:', options.output); |
| 64 | + console.log('Tags:', options.tags); |
| 65 | + console.log('Files:', args.files); |
| 66 | + }); |
| 67 | + |
| 68 | +try { |
| 69 | + await clit.run(); // (defaults tokens Deno.args | `process.argv.slice(2)`) |
| 70 | +} catch (err) { |
| 71 | + if (err instanceof CommandError) { |
| 72 | + console.error(`\nError: ${err.message}\n`); |
| 73 | + cmd.help(); |
| 74 | + Deno.exit(1); //or process.exit(1); |
| 75 | + } |
| 76 | + throw err; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Run it: |
| 81 | + |
| 82 | +```bash |
| 83 | +deno run -A cli.ts src/*.ts --dry-run -o dist/ --tags build prod |
| 84 | +# → Dry run: true |
| 85 | +# → Output: dist/ |
| 86 | +# → Tags: [ 'build', 'prod' ] |
| 87 | +# → Files: [ 'src/index.ts', 'src/utils.ts' ] |
| 88 | +``` |
| 89 | + |
| 90 | +Use `--help`: |
| 91 | + |
| 92 | +```bash |
| 93 | +deno run -A cli.ts --help |
| 94 | +``` |
| 95 | + |
| 96 | +``` |
| 97 | +Usage: my-cli [files...] [options] |
| 98 | +
|
| 99 | +Does something awesome |
| 100 | +
|
| 101 | +Arguments: |
| 102 | + files... Files to process |
| 103 | +
|
| 104 | +Options: |
| 105 | + --dry-run, -n Don’t execute, just simulate |
| 106 | + --output, -o Output file path |
| 107 | + --tags List of tags |
| 108 | + --help, -h Show help |
| 109 | + --version, -v Show version |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### Subcommands 🌿 |
| 115 | + |
| 116 | +```ts |
| 117 | +import { Command, CommandError } from '@deb/command'; |
| 118 | + |
| 119 | +const cli = new Command() |
| 120 | + .name('git') |
| 121 | + .description('Git-like CLI') |
| 122 | + .command('commit', 'Record changes') |
| 123 | + .option('--message', { kind: 'value', shortFlag: '-m' }) |
| 124 | + .option('--all', { kind: 'flag', shortFlag: '-a' }) |
| 125 | + .handler(({ options }) => { |
| 126 | + console.log('Committing with message:', options.message); |
| 127 | + }) |
| 128 | + .command('push', 'Push changes') |
| 129 | + .handler(() => { |
| 130 | + console.log('Pushing...'); |
| 131 | + }); |
| 132 | + |
| 133 | +try { |
| 134 | + await clit.run(); // (defaults tokens Deno.args | `process.argv.slice(2)`) |
| 135 | +} catch (err) { |
| 136 | + if (err instanceof CommandError) { |
| 137 | + console.error(`\nError: ${err.message}\n`); |
| 138 | + cmd.help(); |
| 139 | + Deno.exit(1); //or process.exit(1); |
| 140 | + } |
| 141 | + throw err; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +```bash |
| 146 | +deno run -A git.ts commit -m "fix bug" --all |
| 147 | +# → Committing with message: fix bug |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +### API 🧩 |
| 153 | + |
| 154 | +```ts |
| 155 | +const cmd = new Command() |
| 156 | + .name('build') |
| 157 | + .option('--watch', { kind: 'flag' }) |
| 158 | + .argument('entry', { kind: 'value' }); |
| 159 | + |
| 160 | +// Parse custom tokens |
| 161 | +const input = cmd.parse(['app.ts', '--watch']); // (defaults tokens Deno.args | `process.argv.slice(2)`) |
| 162 | +console.log(input.options.watch); // true |
| 163 | +console.log(input.args.entry); // "app.ts" |
| 164 | +``` |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## Advanced Options |
| 169 | + |
| 170 | +```ts |
| 171 | +.option('--mode', { |
| 172 | + kind: 'value', |
| 173 | + choices: ['development', 'production'], |
| 174 | + default: 'development' |
| 175 | +}) |
| 176 | +.option('--config', { |
| 177 | + kind: 'inline', // --config=path |
| 178 | + optional: true |
| 179 | +}) |
| 180 | +``` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## License 📄 |
| 185 | + |
| 186 | +MIT License – see [LICENSE](LICENSE) for details. |
| 187 | + |
| 188 | +**Author:** Estarlin R ([estarlincito.com](https://estarlincito.com)) |
0 commit comments