jfhbrook / Node Prompt 0.0.x
Programming Languages
PROMPT [DEPRECATED]
Deprecated, you say?
That's right! For your prompting needs, check out
https://github.com/nodejitsu/node-prompt/ instead! If you use npm, I gave the
"prompt" name to them, so npm install prompt
should do you! If you really
need this version, you'll have to do things the "hard" way: Copy prompt.js into
your project.
Wait, why?
When I originally wrote prompt
I needed something like it for my
brainfuck interpreter so the very
clever pkrumins helped me hack a prompting library
together via a gist. After that I didn't have much of a use for prompting so I
let the project languish. I did get some help from
eldios, whose efforts I appreciate greatly.
Meanwhile, however, the hackers at Nodejitsu built their library for a core piece of their platform, jitsu. So, not only are these guys good at what they do, but the library is also battle-tested and expected to appreciate steady development over the long-term. Plus, it has a nice API.
So What's This Prompt?:
"Prompt," in this incarnation, was a way to request information from the user while a node.js script is running, on the command line, without invoking a REPL. Many languages have something very simple for this sort of thing built-in--for instance, you may have something like this in an early BASIC-ish program:
r = input( 'Give me a radius: ' );
print( 'Your area is '+ pi*r**2 + '!');
To my surprise, node.js didn't have anything quite like this, though it had the tools (process.openStdin) to make something like it. So, with a lot of help, I did.
A Quick Migration Guide:
As it turns out, Nodejitsu's prompt is already much more powerful than this incarnation. While the API is different, conversion should be relatively painless.
For example, here's an example I wrote using this version of node-prompt:
var Prompt = require('./prompt');
Prompt()
.ask('What is your name?', 'name')
.tap(function (vars) {
console.log('You said: ' + vars.name);
})
.ask('What is your quest?', 'quest')
.tap(function (vars) {
console.log('You said: ' + vars.quest);
})
.ask('What is your favorite color?', 'color')
.tap(function (vars) {
console.log('You said: ' + vars.color);
console.log('Okay, off you go!');
})
.end();
Here's a basic rewrite using the new Prompt that (basically) does the same thing:
var prompt = require('prompt').start();
prompt.get({ message: "What is your name?",
name: 'name' },
{ message: "What is your quest?",
name: 'quest' },
{ message: "What is your favorite color?",
name: 'color' }, function(err, res) {
console.log('So you\'re '+res.name+' and your quest is '+res.quest+' and your favorite color is '+res.color+'?');
console.log('Okay, off you go!');
});
You may have noticed that instead of intercepting values each time, I just
collected all of them at once. If this is a sticking point for you, a strict
port is also easy, as you can always .get
each variable separately. Oh, and
the new prompt is chainable too. Pretty awesome!