Bunli
API Reference

createCLI

Create a new CLI instance with configuration

createCLI

Creates a new CLI instance with global configuration.

Syntax

function createCLI(config: CLIConfig): CLI

Parameters

config

The configuration object for your CLI.

interface CLIConfig {
  name: string
  version: string
  description?: string
  commands?: {
    manifest?: string
  }
  plugins?: PluginConfig[]
}

Returns

A CLI instance with the following methods:

interface CLI {
  command(cmd: Command): void
  load(manifest: CommandManifest): Promise<void>
  init(): Promise<void>
  run(argv?: string[]): Promise<void>
}

Examples

Basic Usage

import { createCLI } from '@bunli/core'

const cli = createCLI({
  name: 'my-cli',
  version: '1.0.0',
  description: 'My awesome CLI tool'
})

// Add commands
cli.command({
  name: 'hello',
  description: 'Say hello',
  handler: async () => {
    console.log('Hello, World!')
  }
})

// Run the CLI
await cli.run()

With Command Manifest

import { createCLI } from '@bunli/core'

const cli = createCLI({
  name: 'my-cli',
  version: '1.0.0',
  description: 'My CLI with lazy-loaded commands',
  commands: {
    manifest: './commands/manifest.js'
  }
})

// Initialize (loads manifest)
await cli.init()

// Run the CLI
await cli.run()

Loading Commands Manually

import { createCLI } from '@bunli/core'
import manifest from './commands/manifest.js'

const cli = createCLI({
  name: 'my-cli',
  version: '1.0.0'
})

// Load commands from manifest
await cli.load(manifest)

// Run the CLI
await cli.run()

With Plugins

import { createCLI } from '@bunli/core'
import { configMergerPlugin } from '@bunli/plugin-config'
import { aiAgentPlugin } from '@bunli/plugin-ai-detect'

const cli = await createCLI({
  name: 'my-cli',
  version: '1.0.0',
  plugins: [
    // Load configuration from files
    configMergerPlugin(),
    
    // Detect AI agents
    aiAgentPlugin({ verbose: true })
  ] as const // Use 'as const' for better type inference
})

// Commands can access plugin store
cli.command({
  name: 'info',
  handler: async ({ context }) => {
    if (context?.env.isAIAgent) {
      console.log('Running in AI agent!')
      console.log(`Agents: ${context.store.aiAgents.join(', ')}`)
    }
  }
})

await cli.run()

CLI Methods

command(cmd)

Add a command to the CLI.

cli.command({
  name: 'build',
  description: 'Build the project',
  handler: async () => {
    // Command implementation
  }
})

load(manifest)

Load commands from a manifest for lazy loading.

const manifest = {
  build: () => import('./commands/build.js'),
  test: () => import('./commands/test.js'),
  deploy: () => import('./commands/deploy.js')
}

await cli.load(manifest)

init()

Initialize the CLI. This loads commands from the manifest if configured.

await cli.init()

run(argv?)

Run the CLI with the given arguments. If no arguments are provided, uses process.argv.slice(2).

// Use process arguments
await cli.run()

// Use custom arguments
await cli.run(['build', '--watch'])

Automatic Features

When you create a CLI with createCLI, you get:

  • Automatic help generation - --help flag shows available commands
  • Version display - --version flag shows CLI version
  • Command routing - Automatically routes to the correct command
  • Error handling - Friendly error messages for unknown commands
  • Nested command support - Commands can have subcommands
  • Alias support - Commands can have shortcuts
  • Plugin system - Extend functionality with type-safe plugins
  • Type-safe context - Plugin stores are fully typed in commands

Help Output

Running with --help shows:

$ my-cli --help
my-cli v1.0.0
My awesome CLI tool

Commands:
  hello                Say hello
  build                Build the project
  test                 Run tests

Error Handling

Unknown commands show helpful messages:

$ my-cli unknown
Unknown command: unknown

The CLI automatically handles help flags at any level. For example, my-cli build --help shows help for the build command specifically.

See Also