Skip to content

CLI API Reference

The cli module is the foundation of the clapp framework, providing methods for creating and configuring CLI applications.

Creating a CLI Application

cli(options)

Creates a new CLI application instance.

ts
import { cli } from '@stacksjs/clapp'

const app = cli({
  name: 'mycli',
  version: '1.0.0',
  description: 'My CLI application',
})

Options

OptionTypeDescriptionDefault
namestringThe name of the CLI applicationRequired
versionstringThe version of the CLI applicationRequired
descriptionstringA description of the CLI application''
binstringThe binary name to use when the CLI is installedSame as name
strictCommandsbooleanWhether to error for unknown commandsfalse
strictOptionsbooleanWhether to error for unknown optionsfalse
autoHelpbooleanWhether to automatically show help for unknown commandstrue
autoVersionbooleanWhether to automatically add a version commandtrue
helpCommandbooleanWhether to add a help commandtrue
exitProcessbooleanWhether to exit the process when a command failstrue
noDefaultHelpbooleanWhether to disable the default help outputfalse

CLI Instance Methods

run(argv?)

Runs the CLI application with the given arguments.

ts
// Parse process.argv
app.run()

// Parse custom arguments
app.run(['--version'])

Parameters

ParameterTypeDescriptionDefault
argvstring[]The arguments to parseprocess.argv.slice(2)

option(flags, description, defaultValue?)

Adds a global option to the CLI application.

ts
app.option('-v, --verbose', 'Enable verbose output')
app.option('--config <path>', 'Path to config file', './config.json')

Parameters

ParameterTypeDescriptionDefault
flagsstringThe option flagsRequired
descriptionstringA description of the option''
defaultValueanyThe default value for the optionundefined

Returns

Returns the CLI instance for chaining.

beforeRun(fn)

Registers a function to run before any command is executed.

ts
app.beforeRun(() => {
  console.log('Starting CLI...')
})

Parameters

ParameterTypeDescriptionDefault
fnFunctionThe function to runRequired

Returns

Returns the CLI instance for chaining.

afterRun(fn)

Registers a function to run after all commands have executed.

ts
app.afterRun(() => {
  console.log('CLI execution complete')
})

Parameters

ParameterTypeDescriptionDefault
fnFunctionThe function to runRequired

Returns

Returns the CLI instance for chaining.

version(version)

Sets the version of the CLI application.

ts
app.version('1.0.0')

Parameters

ParameterTypeDescriptionDefault
versionstringThe version stringRequired

Returns

Returns the CLI instance for chaining.

help(options)

Customizes the help output for the CLI application.

ts
app.help({
  header: 'Custom header',
  description: 'Custom description',
  examples: [
    'mycli command --option value',
    'mycli other-command',
  ],
  footer: 'For more information, visit our website.',
})

Parameters

ParameterTypeDescriptionDefault
optionsobjectHelp configuration optionsRequired

Options Object

OptionTypeDescriptionDefault
headerstringText to display at the top of the help outputCLI name
descriptionstringThe description to displayCLI description
examplesstring[]Example usage patterns[]
footerstringText to display at the bottom of the help output''
includeOptionsbooleanWhether to include options in the help outputtrue
includeCommandsbooleanWhether to include commands in the help outputtrue

Returns

Returns the CLI instance for chaining.

CLI Class Properties

name

Gets the name of the CLI application.

ts
console.log(app.name) // 'mycli'

version

Gets the version of the CLI application.

ts
console.log(app.version) // '1.0.0'

description

Gets the description of the CLI application.

ts
console.log(app.description) // 'My CLI application'

commands

Gets the registered commands.

ts
console.log(app.commands) // [Command, Command, ...]

options

Gets the registered global options.

ts
console.log(app.options) // [Option, Option, ...]

Usage Examples

Basic CLI

ts
import { cli, command } from '@stacksjs/clapp'

const app = cli({
  name: 'mycli',
  version: '1.0.0',
  description: 'My CLI application',
})

command('hello')
  .description('Say hello')
  .action(() => {
    console.log('Hello, world!')
  })

app.run()

CLI with Global Options

ts
import { cli, command } from '@stacksjs/clapp'

const app = cli({
  name: 'mycli',
  version: '1.0.0',
})

// Add global options
app.option('-v, --verbose', 'Enable verbose output')
app.option('--no-color', 'Disable colored output')

command('build')
  .description('Build the project')
  .action((options) => {
    if (options.verbose) {
      console.log('Verbose mode enabled')
    }

    console.log(`Color output: ${options.color ? 'enabled' : 'disabled'}`)
    console.log('Building project...')
  })

app.run()

CLI with Lifecycle Hooks

ts
import { cli, command } from '@stacksjs/clapp'

const app = cli({
  name: 'mycli',
  version: '1.0.0',
})

// Add lifecycle hooks
app.beforeRun(() => {
  console.log('Starting CLI...')
})

app.afterRun(() => {
  console.log('CLI execution complete')
})

command('hello')
  .description('Say hello')
  .action(() => {
    console.log('Hello, world!')
  })

app.run()

CLI with Custom Help

ts
import { cli, command, style } from '@stacksjs/clapp'

const app = cli({
  name: 'mycli',
  version: '1.0.0',
})

// Customize help output
app.help({
  header: style.bold.blue('MYCLI HELP'),
  description: 'A powerful CLI application',
  examples: [
    'mycli hello',
    'mycli build --watch',
    'mycli deploy --env production',
  ],
  footer: style.dim('For more information, visit https://example.com'),
})

command('hello')
  .description('Say hello')
  .action(() => {
    console.log('Hello, world!')
  })

app.run()

Released under the MIT License.