Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fardjad/node-interactive-commander

Commander.js with integrated interactive prompts
https://github.com/fardjad/node-interactive-commander

cli command-line commander commanderjs inquirer interactive prompt

Last synced: 3 months ago
JSON representation

Commander.js with integrated interactive prompts

Awesome Lists containing this project

README

        

# Interactive Commander

NPM Version Monthly Downloads test-and-release Workflow Status

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

![Video Demo](/media/demo.gif)

## 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][2] prompts for boolean, multiple-choice, and string
options, along with the ability to create custom prompts
- [Support for plugins](/examples/plugin.ts)

## Installation

```bash
npm install --save interactive-commander
```

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

```bash
npm create interactive-commander@latest
```

## Usage

```typescript
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 of pizzas")
// InteractiveOption supports all the methods of Commander.js' Option
.argParser(Number)
.default(1),
)

.addOption(
new InteractiveOption(
"--non-interactive-option ",
"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")
.choices(["small", "medium", "large"])
.default("medium")
.makeOptionMandatory(),
)

.addOption(
new InteractiveOption("-m, --name ", "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 ", "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](/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](examples/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][3] (e.g. `--no-interactive`).

See [no-interactive.ts](examples/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](examples/dependent-prompts.ts) for an example.

[1]: https://github.com/tj/commander.js
[2]: https://github.com/SBoudrias/Inquirer.js
[3]: https://github.com/tj/commander.js#other-option-types-negatable-boolean-and-booleanvalue
[4]: https://github.com/fardjad/node-create-interactive-commander