interactive-commander
TypeScript icon, indicating that this package has built-in type declarations

0.5.166 • Public • Published

Interactive Commander

NPM Version Monthly Downloads test-and-release Workflow Status

Interactive Commander is an extension of the widely-used Commander.js library. It seamlessly integrates configurable interactive prompts for missing options in your CLI application, enhancing user experience with minimal effort.

Video Demo

Features

  • Full compatibility with Commander.js (utilizes Commander.js under the hood and serves as a drop-in replacement for it)
  • Customizable flags for interactive mode
  • Automatic prompting for missing options in interactive mode
  • Built-in Inquirer.js prompts for boolean, multiple-choice, and string options, along with the ability to create custom prompts
  • Support for plugins

Installation

npm install --save interactive-commander

You can also scaffold a new project with Interactive Commander and TypeScript with create-interactive-commander. To do so, run the following command and follow the prompts:

npm create interactive-commander@latest

Usage

import { InteractiveCommand, InteractiveOption } from "interactive-commander";

const program = new InteractiveCommand();

program
  .command("pizza")
  // Detached options are interactive by default
  .option("-d, --drink", "drink")

  // Missing mandatory options won't throw an error in interactive mode
  .requiredOption("-o, --olive-oil", "olive oil")

  // Boolean InteractiveOptions will show a confirmation prompt by default
  .option("-c, --cheese", "cheese")
  .option("-C, --no-cheese", "no cheese")

  .addOption(
    new InteractiveOption("-n, --count <number>", "number of pizzas")
      // InteractiveOption supports all the methods of Commander.js' Option
      .argParser(Number)
      .default(1),
  )

  .addOption(
    new InteractiveOption(
      "--non-interactive-option <value>",
      "non-interactive option",
    )
      // Passing in undefined to prompt will disable interactive mode for this option
      .prompt(undefined)
      .default("default value"),
  )

  // InteractiveOptions with choices will show a select prompt by default
  .addOption(
    new InteractiveOption("-s, --size <size>", "size")
      .choices(["small", "medium", "large"])
      .default("medium")
      .makeOptionMandatory(),
  )

  .addOption(
    new InteractiveOption("-m, --name <string>", "your name")
      // You can use the prompt method to implement your own prompt logic
      .prompt(async (currentValue, option, command) => {
        // TODO: Implement your own prompt logic here
        const answer = "world";

        // Return the answer
        return answer;
      })
      .makeOptionMandatory(),
  )

  .addOption(
    new InteractiveOption("-r, --voucher-code <string>", "voucher code")
      // The prompt input gets validated by the argParser function
      .argParser((value) => {
        if (typeof value !== "string" || !/^\d{4}$/.test(value)) {
          throw new TypeError("Invalid voucher code");
        }

        return value;
      }),
  )

  .action((_options, cmd: InteractiveCommand) => {
    console.log("Options: %o", cmd.opts());
  });

await program
  // Enables interactive mode (when -i or --interactive is passed in)
  // This should almost always be called on the root command right before
  // calling parseAsync
  .interactive("-i, --interactive", "interactive mode")
  .parseAsync(process.argv);

// Try the following commands:
// command-name pizza
// command-name pizza -i
// command-name pizza -i --count 2
// command-name pizza -i --count 2 --no-cheese
// command-name pizza -i --name "John Doe"
// command-name pizza -i --name "John Doe" --non-interactive-option abc

More examples can be found in the examples directory.

Recipes

Interactive options for the root command

Interactive options on main command won't be prompted for in interactive mode if no subcommand is invoked. That is because Commander.js doesn't support pre-parse (similar to preSubcommand hooks) hooks for the main command. As a workaround, you can define a subcommand as the default command to achieve a similar effect.

See default-subcommand.ts for an example.

Enable interactive mode by default

To enable interactive mode by default, you can define the interactive flags as negatable boolean options (e.g. --no-interactive).

See no-interactive.ts for an example.

Setting default values for option prompts based on other options

In some cases, it may be necessary for the default value of an option prompt to depend on the value of another option. For example, you might want the billing address to be automatically set to the same value as the user's input for the shipping address. To achieve that, you can decorate the readFunction of the of the billing address option to dynamically set the default value of the prompt.

See dependent-prompts.ts for an example.

Package Sidebar

Install

npm i interactive-commander

Weekly Downloads

114

Version

0.5.166

License

MIT

Unpacked Size

81.5 kB

Total Files

7

Last publish

Collaborators

  • dfardjad