
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
interactive-commander
Advanced tools
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.
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
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.
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.
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.
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.
FAQs
Commander.js with integrated interactive prompts
The npm package interactive-commander receives a total of 7,473 weekly downloads. As such, interactive-commander popularity was classified as popular.
We found that interactive-commander demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.