15

been googling for this and searching stackoverflow, but am not coming across anything. I want to develop an interactive shell with node, and wondering on the best approach for this. Is there a library that anyone could recommend to use?

I have written a library, and now want a CLI interface to interact with it, by 2 methods: running the app with parameters, or via interactive shell. e.g.

$ node myapp doSomething
App Result: I did something
$ node myapp cli
Entering interactive mode...
myapp>
myapp> doSomething
App Result: I did something
myapp>

Any suggestions?

1
  • A note: I am currently building using process.argv with parameters, its mainly the interactiveness that I am trying to build Commented Oct 17, 2012 at 8:15

4 Answers 4

14

I guess commander.js is what you are looking for.

https://github.com/visionmedia/commander.js

http://oscar-mejia.com/blog/how-to-create-a-command-line-program-with-nodejs/

Also have a look at REPL http://nodejs.org/api/repl.html

Sign up to request clarification or add additional context in comments.

3 Comments

Inquirer seems a nice alternative, with validation and nice prompts: github.com/SBoudrias/Inquirer.js
Only tell me how commander.js is interactive? It can do nothing but accepts cli options $ myscript.js -foo -bar -baz. Nothing interactive. Why did you suggest it?
@berdario Vorpaljs builds off of Inquirer.js to provide an API for building CLIs, which seems to be what he is asking. * Disclaimer: I made Vorpal.
5

Vorpal is a framework exactly for what the question describes, although they call it an 'immersive cli'.

var vorpal = require('vorpal')();

vorpal
  .command('doSomething')
  .action(function (args, cb){
    this.log('App Result: I did something');
    cb();
  });

vorpal
  .delimiter('myapp>')
  .show();

Comments

4

I've recently started a project for an enhanced REPL that provides plugins and multi-language support (like CoffeeScript):

http://danielgtaylor.github.com/nesh/

It might be useful for you when building interactive apps. Let me know if you'd like to see any features in Nesh!

Comments

0

This question is a bit old, but I've given some mileage to a module I built a while ago that will launch an interactive shell-like command prompt:

https://github.com/mrvisser/node-readcommand

The key difference to this over something like commander is it allows you to maintain shell session state and accept the commands internally in Node.js, as opposed to requiring every invocation to be a stateless re-run with the shell's parsed arguments. It effectively wraps node's internal readline module to provide:

  • shell-like parsing of the input
  • multi-line command support by escaping the new-line or quoting over it
  • command history support (when using readcommand.loop)
  • argument-based auto-complete (wrapped around readlines text-based auto-complete support

Hoping someone else finds it useful, too.

For a more advanced and opinionated interactive CLI framework, I also built node-corporal: https://github.com/mrvisser/node-corporal . Which is probably more than you're looking for, but it provides a structure and environment for whipping together CLI apps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.