Create Your First CLI
The fastest way to get started with Bunli is usingcreate-bunli, the official scaffolding tool.
Create a new project
Run the following command to create a new CLI project:You’ll be prompted to choose a template:The tool will create a new directory with your project and install dependencies automatically.
Choose basic for this quickstart. You can explore other templates later.
Navigate to your project
Change into your new project directory:Your project structure looks like this:
Understanding Your First Command
Let’s examine the command that was generated insrc/commands/hello.ts:
src/commands/hello.ts
Key Concepts
defineCommand: Creates a type-safe command definitionoption(): Defines command options with Zod validation- Zod schemas: Provide runtime validation and type inference
- Short flags: Single-letter aliases for options (e.g.,
-nfor--name) - Handler context: Access to
flags,colors,spinner, and more
Build Your CLI
Compile your CLI to a standalone binary:Test Your CLI
Bunli includes testing utilities out of the box. Run tests with:The basic template doesn’t include tests by default, but you can add them using
@bunli/test. See the Testing Guide for details.Available Templates
Beyond the basic template,create-bunli offers other starting points:
Basic
Perfect for: Simple CLIs with a single commandIncludes:
- One example command
- TypeScript configuration
- Basic build setup
Advanced
Perfect for: Complex CLIs with multiple commandsIncludes:
- Multiple commands
- Configuration management
- File validation system
- Development server example
Monorepo
Perfect for: Large projects with multiple packagesIncludes:
- Turborepo configuration
- Multiple packages setup
- Shared dependencies
- Parallel builds
Common Commands
Here are the essential commands you’ll use regularly:| Command | Description |
|---|---|
bun run dev | Start development mode with hot reload |
bun run build | Compile CLI to production binary |
bun test | Run tests with Bun’s test runner |
bun run typecheck | Check TypeScript types without building |
bunli generate | Generate TypeScript definitions from commands |
Using create-bunli Options
create-bunli supports several options for customization:
Customizing Your CLI
Editsrc/index.ts to customize your CLI metadata:
src/index.ts
Next Steps
Now that you have a working CLI, dive deeper:Build from Scratch
Learn how to build a CLI from scratch without templates
Core Concepts
Understand commands, options, and handlers in depth
Add Plugins
Extend your CLI with the powerful plugin system
Type Generation
Enable autocomplete and type-safe wrappers