@ingentis/cli
TypeScript icon, indicating that this package has built-in type declarations

0.2.12 • Public • Published

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 command
  • order 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

  1. In your main executable (e.g., cafe.ts), make an instance of the Cli class and provide a subcommand.

    #!/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);
  2. 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;
      };
    }
  3. Add your main executable to your package.json bin:

    {
      "name": "my-cafe-cli",
      "version": "1.0.0",
      "bin": {
        "cafe": "./bin/cafe.js"
      }
    }
  4. Run your CLI:

    $ npm link
    $ cafe order coffee --hot
    here's your coffee. careful - it's hot!

Readme

Keywords

Package Sidebar

Install

npm i @ingentis/cli

Weekly Downloads

1

Version

0.2.12

License

ISC

Unpacked Size

12.6 kB

Total Files

28

Last publish

Collaborators

  • dylangles