npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

interactive-commander

v0.5.190

Published

Commander.js with integrated interactive prompts

Downloads

272

Readme

Interactive Commander

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.