What is @oclif/core?
The @oclif/core npm package is a framework for building command-line interfaces (CLIs) in Node.js. It provides a powerful set of tools to develop sophisticated CLIs with features such as argument parsing, command structure, and plugin support. This package is part of the Open CLI Framework (OCLIF) developed by Heroku.
What are @oclif/core's main functionalities?
Command Creation
This feature allows developers to create custom commands for their CLI. The code sample demonstrates how to define a simple command that accepts a name as a flag and prints a greeting message.
const {Command, flags} = require('@oclif/core');
class MyCommand extends Command {
async run() {
const {flags} = await this.parse(MyCommand);
console.log(`Hello, ${flags.name}!`);
}
}
MyCommand.description = 'Describe the command here';
MyCommand.flags = {
name: flags.string({char: 'n', description: 'name to print'}),
};
module.exports = MyCommand;
Argument Parsing
This feature simplifies the process of parsing arguments passed to commands. The code sample shows how to create a command that echoes a message passed as an argument.
const {Command, flags} = require('@oclif/core');
class EchoCommand extends Command {
async run() {
const {args} = await this.parse(EchoCommand);
console.log(`Echoing: ${args.message}`);
}
}
EchoCommand.args = [{name: 'message'}];
module.exports = EchoCommand;
Plugin Support
OCLIF supports plugins to extend the functionality of your CLI. This code sample demonstrates how to define a simple plugin that includes a custom command.
const {Command, Plugin} = require('@oclif/core');
class MyPlugin extends Plugin {
constructor(root, config) {
super(root, config);
this.name = 'my-plugin';
}
commands = [require('./commands/my-command')];
}
module.exports = MyPlugin;
Other packages similar to @oclif/core
commander
Commander is a lightweight, expressive, and powerful command-line framework for Node.js. It provides a simpler API compared to @oclif/core but lacks some of the advanced features such as plugin support and automatic help generation.
yargs
Yargs helps you build interactive command-line tools, by parsing arguments and generating an elegant user interface. It's more focused on parsing arguments and providing a fluent API, whereas @oclif/core offers a more structured approach to building CLIs with support for plugins and multi-command CLIs.
vorpal
Vorpal is a framework for building immersive CLI applications in Node. It offers a similar command-based structure to @oclif/core but with a focus on interactive command-line interfaces. Vorpal is less actively maintained compared to @oclif/core.