oclif CLI Framework

4.23.0 · active · verified Wed Apr 22

oclif (Open CLI Framework) is a robust and extensible framework for building command-line interface (CLI) applications using Node.js and TypeScript. It provides a structured approach to CLI development, handling common tasks like command parsing, argument and flag definition, help generation, and plugin management. Currently in its 4.x major version, oclif receives frequent maintenance updates, primarily bug fixes and dependency bumps, indicating active development. Key differentiators include its extensibility via plugins, comprehensive documentation, and its adoption by major projects such as the Salesforce CLI and Heroku CLI. It's designed to support both single-command CLIs and multi-command CLIs with ease, abstracting away much of the boilerplate associated with CLI creation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic 'hello' command with arguments and flags within an oclif CLI, and how to create a new oclif project.

import { Command, Args, Flags } from '@oclif/core';

export default class Hello extends Command {
  static description = 'Say hello to a person';

  static examples = [
    '<%= config.bin %> <%= command.id %> world --from oclif',
  ];

  static flags = {
    from: Flags.string({ char: 'f', description: 'Who is saying hello', required: true }),
  };

  static args = {
    person: Args.string({ description: 'Person to say hello to', required: true }),
  };

  public async run(): Promise<void> {
    const { args, flags } = await this.parse(Hello);

    this.log(`Hello from ${flags.from}, ${args.person}!`);
  }
}

// To generate a new CLI project, run:
// npx oclif generate mynewcli
// cd mynewcli
// ./bin/run.js hello world --from me

view raw JSON →