@ingentis/cli
v0.2.12
Published
Small library for creating command-line interfaces with Node.js
Downloads
5
Readme
Description
A small library for building command-line interfaces with Node.js.
Design
This library is designed to support a particular style of command-line interface.
$ cafe order tea --hot --cups=2
cafe
is the commandorder
is a subcommand that accepts:tea
as an argument--hot
and--cups
as options
Arguments are required; options are not.
If this looks like your cup of tea, give it a try! If you prefer a less defined style, consider commander.
Quickstart
In your main executable (e.g.,
cafe.ts
), make an instance of theCli
class and provide asubcommand
.#!/usr/bin/env node import { Cli } from '@ingentis/cli'; import { order } from './subcommands/order'; const cli = new Cli(process.argv.slice(2)); cli.subcommand(order);
Write the
subcommand
's file (e.g.,order.ts
).import { Cli, Subcommand } from '@ingentis/cli'; export const order: Subcommand = { names: ['o', 'order'], args: { beverage: { format: 'string' } }, options: { hot: { names: ['h', 'hot'], format: 'boolean' } }, program: async (args: any, options: any): Promise<number> => { // Construct the base message. let message = `here's your ${args.beverage}.`; // Add a caution if it's hot! if (options.hot) message += ` careful -- it's hot!`; // Print the message. console.log(message); return 0; }; }
Add your main executable to your
package.json
bin:{ "name": "my-cafe-cli", "version": "1.0.0", "bin": { "cafe": "./bin/cafe.js" } }
Run your CLI:
$ npm link $ cafe order coffee --hot here's your coffee. careful - it's hot!