Scripts2026年5月12日·1 分钟阅读

Oclif — Open CLI Framework for Building TypeScript Command-Line Tools

A framework by Salesforce for building extensible CLI applications in TypeScript with plugins, auto-generated help, and argument parsing.

Introduction

Oclif (Open CLI Framework) is a TypeScript-first framework for building command-line tools. Created by Salesforce and used to power the Heroku CLI and Salesforce CLI, it provides a structured way to define commands, flags, arguments, and plugins for CLIs of any size.

What Oclif Does

  • Scaffolds new CLI projects with a single command using the generator
  • Provides a class-based command structure with typed flags and arguments
  • Auto-generates help text, man pages, and README documentation from command metadata
  • Supports a plugin system that lets users extend your CLI with third-party packages
  • Handles command discovery, lifecycle hooks, and error reporting out of the box

Architecture Overview

Oclif organizes a CLI as a collection of command classes, each defining its flags, arguments, and run method. At startup, the framework discovers commands from the file system or installed plugin packages and builds a command tree. Flag and argument parsing uses a strict, typed approach that validates input before the command runs. Plugins are npm packages that expose additional commands and hooks, loaded dynamically at runtime.

Self-Hosting & Configuration

  • Generate a new project: npx oclif generate my-cli
  • Define commands as TypeScript classes in the src/commands/ directory
  • Configure the CLI name, version, and plugins in package.json under the oclif key
  • Distribute via npm or build standalone installers for macOS, Windows, and Linux
  • Add plugins by listing them in the oclif.plugins array or let users install them dynamically

Key Features

  • TypeScript-first with full type inference for flags, arguments, and configuration
  • Plugin architecture powering large-scale CLIs like Heroku CLI and Salesforce CLI
  • Auto-generated help output formatted for terminal with color and layout
  • Built-in testing utilities for unit and integration testing commands
  • Standalone packaging to create single-binary installers without requiring Node.js

Comparison with Similar Tools

  • Commander.js — lightweight flag parser; Oclif adds plugins, scaffolding, and auto-generated docs
  • Yargs — flexible argument parsing; Oclif provides more structure with classes and lifecycle hooks
  • Cobra (Go) — similar command framework for Go; Oclif is the TypeScript equivalent
  • Clipanion — Yarn's CLI engine; Oclif has a larger ecosystem and standalone packaging

FAQ

Q: Can I use Oclif with plain JavaScript? A: Yes, though TypeScript is recommended. The generator scaffolds TypeScript by default.

Q: How do plugins work? A: Plugins are npm packages that export Oclif commands. Users install them with my-cli plugins install plugin-name.

Q: Can I build standalone executables? A: Yes. Oclif includes a pack command that creates tarballs and platform-specific installers.

Q: What CLIs are built with Oclif? A: Heroku CLI, Salesforce CLI (sf), Shopify CLI, and Twilio CLI are all built on Oclif.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产