Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Node Module provides a CLI prompt? [closed]

I have been searching far and wide for a node module that provides a REPL-like CLI prompt interface for a Node app.

What I am looking for is kind of a hybrid between inquirer or prompt, and commander.

Node provides a built-in REPL module, however this just exposes the raw javascript of your application. I want to built a virtual interface for an application.

Example, supposing you run node server.js, you would get a prompt:

server:~$

This would behave like a basic unix prompt, in your own virtual context. Pressing enter:

server:~$
server:~$

Or help:

server:~$ help

Commands:

debug [setting]   Enables or disables debugging.
show stats        Displays live stats for your web server.

server:~$

And you could execute custom commands:

server:~$ debug on -v 7
Debugging turned on with a verbosity of 7
... live logging ...

Any idea of what NPM modules handle this?


Update 9 months later

Due to nothing matching my needs, I ended up building and publishing Vorpal, a framework for building Interactive CLIs in Node.

like image 649
dthree Avatar asked May 20 '15 17:05

dthree


2 Answers

You could use the standard REPL module and plug in a custom command evaluator. The "up arrow to repeat commands" and such is already baked into the repl module, so you don't need to worry about that.

For example, here's a crappily implemented evaluator that does the things you've described in your question:

var repl = require("repl");

var cmds = {
    "help" : function(input, context) { 
        return "debug [setting]   Enables or disables debugging..." 
    },
    "debug" : function(input, context) {
        var args = input.split(/\s+/).slice(1);

        var onoff = args[0];
        var verbosity = args[2];

        return "Debugging turned " + onoff + " with a verbosity of " + verbosity;
    },
    "exit": function(input, context) {
        process.exit();
    },
    "default" : function(input, context) { 
        return "Command not understood";
    }
};

function eval(input, context, filename, callback) {
    var cmd = input.split(/\s+/)[0];
    var result = (cmds[cmd] || cmds["default"])(input, context);
    callback(null, result);
}

repl.start({
    prompt: "server:~$ ",
    eval: eval
});

Please note the focus of the demo here is how to implement a custom REPL; there are obviously better ways to implement the evaluator than whitespace splitting and a hash of functions, but how you implement the command evaluator depends on what your application is supposed to do

like image 187
Asad Saeeduddin Avatar answered Sep 30 '22 06:09

Asad Saeeduddin


There is a node-shell available, see also the home page.

Features from the home page are:

Shell brings a Connect inspired API, Express inspired routing, and other similar functionality to console based applications.

  • Run both in shell mode and command mode
  • First class citizen for console application (arrows, ctrl-a, ctrl-u,…)
  • User friendly with history, help messages and many other plugings
  • Foundation to structure and build complex based applications
  • Command matching, parameters and advanced functionnalities found in Express routing
  • Flexible architecture based on middlewares for plugin creation and routing enhancement
  • Familiar API for those of us using Connect or Express
  • Predifined commands through plugins for Redis, HTTP servers, Cloud9, CoffeeScript, …

You find a basic reddis client example on the homepage which comes with command completion and history.

like image 30
ChrLipp Avatar answered Sep 30 '22 05:09

ChrLipp