Examples
Minimal CLI
The simplest possible CLI with Bunli
Minimal CLI Example
The absolute minimum code needed to create a working CLI with Bunli.
Complete Example
// index.ts
import { createCLI } from '@bunli/core'
const cli = createCLI({
name: 'hello',
version: '1.0.0',
description: 'A minimal CLI example'
})
// The CLI automatically includes --help and --version
await cli.run()
That's it! This creates a working CLI with:
--help
flag showing usage information--version
flag showing the version- Proper error handling for unknown commands
Running the CLI
# Make it executable
chmod +x ./index.ts
# Run it
./index.ts --help
# Output:
# hello v1.0.0
# A minimal CLI example
#
# Usage: hello [options]
#
# Options:
# --help Show help
# --version Show version
Adding a Simple Command
Let's add a single command:
// index.ts
import { createCLI, defineCommand } from '@bunli/core'
const cli = createCLI({
name: 'hello',
version: '1.0.0',
description: 'A minimal CLI example'
})
// Add a greet command
cli.command(
defineCommand({
name: 'greet',
description: 'Say hello',
handler: async () => {
console.log('Hello, World!')
}
})
)
await cli.run()
Usage:
./index.ts greet
# Output: Hello, World!
./index.ts --help
# Now shows the greet command
Command with Options
Add a simple option:
import { createCLI, defineCommand, option } from '@bunli/core'
import { z } from 'zod'
const cli = createCLI({
name: 'hello',
version: '1.0.0'
})
cli.command(
defineCommand({
name: 'greet',
description: 'Greet someone',
options: {
name: option(
z.string().default('World'),
{ description: 'Name to greet' }
)
},
handler: async ({ flags }) => {
console.log(`Hello, ${flags.name}!`)
}
})
)
await cli.run()
Usage:
./index.ts greet
# Output: Hello, World!
./index.ts greet --name Alice
# Output: Hello, Alice!
With Package.json
For a more complete setup:
{
"name": "hello-cli",
"version": "1.0.0",
"type": "module",
"bin": {
"hello": "./index.ts"
},
"dependencies": {
"@bunli/core": "latest",
"zod": "^3.0.0"
}
}
// index.ts
#!/usr/bin/env bun
import { createCLI } from '@bunli/core'
import { name, version, description } from './package.json'
const cli = createCLI({
name,
version,
description
})
await cli.run()
Install globally:
bun link
# Now use anywhere
hello --version
Even More Minimal
The absolute bare minimum:
#!/usr/bin/env bun
import { createCLI } from '@bunli/core'
await createCLI({ name: 'hi' }).run()
This 3-line CLI still has:
- Argument parsing
- Help generation
- Error handling
Building for Distribution
Build the minimal CLI:
# Development
bun run index.ts
# Build for production
bunli build --compile
# Now you have a standalone executable
./dist/hello --help
Key Takeaways
- Minimal Setup: Just a few lines to get started
- Automatic Features: Help, version, and error handling built-in
- Type Safety: Full TypeScript support out of the box
- Zero Config: Works without any configuration files
- Production Ready: Can be compiled to standalone binary
Next Steps
- Multi-Command Example - Add multiple commands
- Getting Started Guide - Step-by-step tutorial
- API Reference - Complete API documentation