|
| 1 | +Citty: Elegant, zero-dependency CLI builder for Node.js. |
| 2 | + |
| 3 | +**Important:** Keep `AGENTS.md` updated with project information. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +src/ |
| 9 | +├── index.ts # Public API re-exports |
| 10 | +├── types.ts # All type definitions (ArgDef, CommandDef, ParsedArgs, etc.) |
| 11 | +├── command.ts # defineCommand(), runCommand(), resolveSubCommand() |
| 12 | +├── main.ts # runMain(), createMain() — CLI entry point with --help/--version |
| 13 | +├── args.ts # parseArgs() — argument parsing and validation |
| 14 | +├── usage.ts # renderUsage(), showUsage() — help text generation |
| 15 | +├── _parser.ts # Low-level parser wrapping node:util.parseArgs (internal) |
| 16 | +├── _utils.ts # toArray, formatLineColumns, resolveValue, CLIError (internal) |
| 17 | +└── _color.ts # ANSI color helpers with NO_COLOR support (internal) |
| 18 | +
|
| 19 | +test/ # Vitest tests (args, parser, main, usage, utils) |
| 20 | +playground/ # Example CLI apps (run with `pnpm play`) |
| 21 | +``` |
| 22 | + |
| 23 | +Internal files use `_` prefix and are not exported from `index.ts`. |
| 24 | + |
| 25 | +## Public API |
| 26 | + |
| 27 | +```typescript |
| 28 | +// Core |
| 29 | +defineCommand(def) // Define a typed command |
| 30 | +runCommand(cmd, opts) // Execute a command programmatically |
| 31 | +runMain(cmd, opts?) // CLI entry point (handles --help, --version, process.exit) |
| 32 | +createMain(cmd) // Returns a function wrapping runMain() |
| 33 | + |
| 34 | +// Utilities |
| 35 | +parseArgs(rawArgs, argsDef) // Parse CLI arguments against definitions |
| 36 | +renderUsage(cmd, parent?) // Generate help text string |
| 37 | +showUsage(cmd, parent?) // Print help to console |
| 38 | +``` |
| 39 | +
|
| 40 | +## Architecture |
| 41 | +
|
| 42 | +### Argument Types |
| 43 | +
|
| 44 | +- `positional` — unnamed args (`cli <name>`) |
| 45 | +- `string` — named string options (`--name value`) |
| 46 | +- `boolean` — flags (`--verbose`, `--no-verbose` for negation) |
| 47 | +- `enum` — constrained to `options` array (`--level=info|warn|error`) |
| 48 | +
|
| 49 | +Arguments are **case-agnostic** — `--user-name` and `--userName` resolve to the same value via auto-generated aliases (uses `scule` for case conversion) and a Proxy-based accessor. |
| 50 | +
|
| 51 | +### Command Lifecycle |
| 52 | +
|
| 53 | +`setup()` → resolve subcommand or `run()` → `cleanup()` (always runs in `finally`) |
| 54 | +
|
| 55 | +### Lazy Loading |
| 56 | +
|
| 57 | +`Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>)` — used for `meta`, `args`, and `subCommands` to support dynamic imports. |
| 58 | +
|
| 59 | +### Error Handling |
| 60 | +
|
| 61 | +Custom `CLIError` class with error codes: `EARG`, `E_UNKNOWN_COMMAND`, `E_NO_COMMAND`, `E_NO_VERSION`. Usage is auto-shown before error messages in `runMain`. |
| 62 | +
|
| 63 | +## Dependencies |
| 64 | +
|
| 65 | +**Zero runtime dependencies.** Only `scule` is used from source code (bundled at build time). |
| 66 | +
|
| 67 | +## Scripts |
| 68 | +
|
| 69 | +| Command | Description | |
| 70 | +|---------|-------------| |
| 71 | +| `pnpm dev` | Vitest watch mode | |
| 72 | +| `pnpm test` | Lint + typecheck + vitest with coverage | |
| 73 | +| `pnpm test:types` | Type checking via `tsgo --noEmit` | |
| 74 | +| `pnpm lint` | `oxlint . && oxfmt --check` | |
| 75 | +| `pnpm lint:fix` | `oxlint . --fix && oxfmt` | |
| 76 | +| `pnpm build` | Build with obuild → `dist/` | |
| 77 | +| `pnpm play` | Run playground CLI | |
| 78 | +
|
| 79 | +## Tooling |
| 80 | +
|
| 81 | +- **Build:** obuild (single entry `src/index.ts` → `dist/index.mjs`) |
| 82 | +- **Test:** Vitest with `@vitest/coverage-v8`, typecheck enabled |
| 83 | +- **Lint:** oxlint (plugins: unicorn, typescript, oxc) |
| 84 | +- **Format:** oxfmt |
| 85 | +- **TypeScript:** strict mode, `nodenext` module, `verbatimModuleSyntax` |
| 86 | +
|
| 87 | +## Conventions |
| 88 | +
|
| 89 | +- ESM with explicit `.ts` extensions in imports |
| 90 | +- Internal files prefixed with `_` (not exported) |
| 91 | +- Tests use inline snapshots for usage output verification |
| 92 | +- Colors respect `NO_COLOR`, `TERM=dumb`, `TEST`, `CI` env vars |
| 93 | +- `--no-flag` negation requires `default: true` or `negativeDescription` on the arg def |
0 commit comments