minish
Minish makes it easy to write applications that interact with a user using a simple shell-like command-line interface.
Installing
$ npm install --save minish
Usage
Starting an interactive command-line session is very simple. Just require minish, declare some commands and start a command prompt:
var shell = require('minish');
shell.command("hello", function (context) {
shell.write("Hello world!");
context.end();
});
shell.command("exit", function (context) {
shell.exit();
});
shell.prompt();
Commands accept arguments and options. Both of these get parsed with minimist and are passed to the callback with command context object:
shell.command("echo", function (context) {
var args = context.args;
var opts = context.options;
shell.write(args, opts);
context.end();
});
Minish makes it simple to ask for input. It can also ask for passwords discretely:
shell.password("Type a secret password:", function (password) {
shell.question("Type 'show' to display the password:", function (reply) {
if (reply === "show") shell.write("The password was:", password);
});
});
Let's try something more complicated:
var shell = require('minish');
shell.command("hello", function (context) {
shell.write("Hello world!");
context.end();
});
shell.command("echo", "Shows arguments and options", function (context) {
var args = context.args;
var opts = context.options;
shell.write(args, opts);
context.end();
});
shell.command("ask", "Asks a question", function (context) {
shell.question("What's your name?", function (reply) {
shell.write("Your name is:", reply);
context.end();
});
});
shell.command("passwd", "Asks for a password without revealing its characters", function (context) {
shell.password("Type a secret password:", function (password) {
shell.question("Type 'show' to display the password:", function (reply) {
if (reply === "show") shell.write("The password was:", password);
context.end();
});
});
});
shell.command(["quit", "exit"], "Exits the example", function (context) {
shell.write("Ending...");
shell.exit();
});
shell.command("_", function (context) {
context.fail("Command '" + context.command + "' not supported");
});
shell.write("Welcome to minish.");
shell.write("Type 'help' to see a list of available commands.");
shell.prompt("> ");
Check out minish TypeScript typings for a quick overview of the rest of available API.