Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@oclif/command
Advanced tools
@oclif/command is a framework for building command-line interface (CLI) applications. It provides a robust set of tools and conventions for creating, organizing, and managing CLI commands, arguments, and options.
Creating a Basic Command
This feature allows you to create a basic command that prints 'Hello, world!' to the console. The Command class is extended to define the behavior of the command.
const { Command } = require('@oclif/command');
class HelloWorldCommand extends Command {
async run() {
this.log('Hello, world!');
}
}
HelloWorldCommand.run();
Adding Arguments and Flags
This feature allows you to add arguments and flags to your command. In this example, the command takes an optional 'name' argument and a 'greeting' flag to customize the output.
const { Command, flags } = require('@oclif/command');
class GreetCommand extends Command {
async run() {
const { args, flags } = this.parse(GreetCommand);
const name = args.name || 'world';
const greeting = flags.greeting || 'Hello';
this.log(`${greeting}, ${name}!`);
}
}
GreetCommand.args = [
{ name: 'name' }
];
GreetCommand.flags = {
greeting: flags.string({ char: 'g', description: 'greeting to use' })
};
GreetCommand.run();
Command Aliases
This feature allows you to define aliases for your commands. In this example, the 'HelloWorldCommand' can be invoked using 'hw' or 'hello' as aliases.
const { Command } = require('@oclif/command');
class HelloWorldCommand extends Command {
async run() {
this.log('Hello, world!');
}
}
HelloWorldCommand.aliases = ['hw', 'hello'];
HelloWorldCommand.run();
Commander is a popular library for building command-line interfaces. It provides a simple and flexible API for defining commands, arguments, and options. Compared to @oclif/command, Commander is more lightweight and has fewer built-in features, but it is easier to get started with for simple CLI applications.
Yargs is another library for building command-line interfaces. It focuses on providing a powerful and user-friendly API for parsing arguments and generating help messages. Yargs offers more advanced argument parsing capabilities compared to @oclif/command, but it may require more configuration for complex CLI applications.
Caporal is a full-featured framework for building command-line applications. It provides a rich set of features for defining commands, arguments, options, and subcommands. Caporal is similar to @oclif/command in terms of functionality, but it has a different API and design philosophy.
oclif base command
This is about half of the main codebase for oclif. The other half lives in @oclif/config. This can be used directly, but it probably makes more sense to build your CLI with the generator.
Without the generator, you can create a simple CLI like this:
TypeScript
#!/usr/bin/env ts-node
import * as fs from 'fs'
import {Command, flags} from '@oclif/command'
class LS extends Command {
static flags = {
version: flags.version(),
help: flags.help(),
// run with --dir= or -d=
dir: flags.string({
char: 'd',
default: process.cwd(),
}),
}
async run() {
const {flags} = this.parse(LS)
let files = fs.readdirSync(flags.dir)
for (let f of files) {
this.log(f)
}
}
}
LS.run()
.catch(require('@oclif/errors/handle'))
JavaScript
#!/usr/bin/env node
const fs = require('fs')
const {Command, flags} = require('@oclif/command')
class LS extends Command {
async run() {
const {flags} = this.parse(LS)
let files = fs.readdirSync(flags.dir)
for (let f of files) {
this.log(f)
}
}
}
LS.flags = {
version: flags.version(),
help: flags.help(),
// run with --dir= or -d=
dir: flags.string({
char: 'd',
default: process.cwd(),
}),
}
LS.run()
.catch(require('@oclif/errors/handle'))
Then run either of these with:
$ ./myscript
...files in current dir...
$ ./myscript --dir foobar
...files in ./foobar...
$ ./myscript --version
myscript/0.0.0 darwin-x64 node-v9.5.0
$ ./myscript --help
USAGE
$ @oclif/command
OPTIONS
-d, --dir=dir [default: /Users/jdickey/src/github.com/oclif/command]
--help show CLI help
--version show CLI version
See the generator for all the options you can pass to the command.
FAQs
oclif base command
The npm package @oclif/command receives a total of 757,280 weekly downloads. As such, @oclif/command popularity was classified as popular.
We found that @oclif/command demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.