Skip to content

kcmr/cli-helper

Repository files navigation

CliHelper

A multicommand CLI helper that uses yargs and inquirer.

Install

npm i @kuscamara/cli-helper

Usage

CliHelper creates CLIs from the specified commands object and uses yargs for command options and inquirer to prompt for missing params not passed as flags / options.

const { CliHelper } = require('@kuscamara/cli-helper');
const cli = new CliHelper({ options });

Options

  • description (string): Main command description.
  • defaultCommandMessage (string): Prompt message of the default command. Default "Choose a command".
  • commands (object): CLI commands. Accepts an action key (function) for each command that will receive the executed command and the command options as options object. The params property uses the same options that inquirer questions.

Full example

const { CliHelper } = require('@kuscamara/cli-helper');

const cli = new CliHelper({
  description: 'My awesome CLI',
  commands: {
    'print': {
      desc: 'Prints something',
      params: {
        color: {
          message: 'Use colors in output',
          type: 'boolean'
        }
      },
      action: ({ command, options }) => {
        console.log(`${command} executed with ${options.color}`);
      }
    },
    'greet': {
      desc: 'Says hello',
      params: {
        name: {
          message: 'Name',
          type: 'string'
        }
      },
      action: ({ options }) => {
        console.log(`Hello ${options.name}!`);
      }
    }
  }
});

cli.run();

Registering custom prompt types

const { CliHelper } = require('@kuscamara/cli-helper');
const { PathPrompt } = require('inquirer-path');

CliHelper.registerPrompt('path', PathPrompt);

const cli = new CliHelper(options);

cli.run();

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published