Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[minor] support override (close issue #6)

  • Loading branch information...
commit ec85235e6ab36fb3bae936a2dd58cf156b86acc7 1 parent e1b1faf
Dominic Tarr authored
Showing with 78 additions and 1 deletion.
  1. +38 −0 README.md
  2. +4 −0 lib/prompt.js
  3. +36 −1 test/prompt-test.js
38 README.md
View
@@ -123,6 +123,44 @@ Lets examine the anatomy of a prompt property:
empty: false // If false, value entered must be non-empty.
}
```
+### skipping prompts
+
+Sometimes power users may wish to skip promts and specify all data as command line options.
+if a value is set as a property of `prompt.override` prompt will use that instead of
+prompting the user.
+
+``` js
+//prompt-everride.js
+
+var prompt = require('prompt'),
+ optimist = require('optimist')
+
+//
+// set the overrides
+//
+prompt.override = optimist.argv
+
+//
+// Start the prompt
+//
+prompt.start();
+
+//
+// Get two properties from the user: username and email
+//
+prompt.get(['username', 'email'], function (err, result) {
+ //
+ // Log the results.
+ //
+ console.log('Command-line input received:');
+ console.log(' username: ' + result.username);
+ console.log(' email: ' + result.email);
+})
+
+//: node prompt-everride.js --username USER --email EMAIL
+
+```
+
### Adding Properties to an Object
A common use-case for prompting users for data from the command-line is to extend or create a configuration object that is passed onto the entry-point method for your CLI tool. `node-prompt` exposes a convenience method for doing just this:
4 lib/prompt.js
View
@@ -192,6 +192,10 @@ prompt.getInput = function (prop, callback) {
read = prop.hidden ? prompt.readLineHidden : prompt.readLine,
length, msg;
+ if (prompt.override && prompt.override [name]) {
+ return callback (null, prompt.override [name])
+ }
+
if (prop.default) {
raw.splice(2, -1, ' (' + prop.default + ')');
}
37 test/prompt-test.js
View
@@ -269,7 +269,16 @@ vows.describe('prompt').addBatch({
}
}
}
- }
+ }/*,
+ "skip prompt with prompt.overide": {
+ topic: function () {
+ prompt.overide = { coconihet: 'whatever' }
+ prompt.get('coconihet', this.callback);
+ },
+ "skips prompt and uses overide": function (err, results) {
+ assert.equal(results.coconihet, 'whatever')
+ }
+ }*/
},
"the addProperties() method": {
topic: function () {
@@ -316,4 +325,30 @@ vows.describe('prompt').addBatch({
}
}
}
+}).addBatch({
+ "when using node-prompt": {
+ topic: function () {
+ //
+ // Reset the prompt for mock testing
+ //
+ prompt.started = false;
+ prompt.start({
+ stdin: helpers.stdin,
+ stdout: helpers.stdout
+ });
+
+ return null;
+ },
+ "the get() method": {
+ topic: function () {
+ prompt.override = { xyz: 468, abc: 123 }
+ prompt.get(['xyz', 'abc'], this.callback);
+ },
+ "should respond with overrides": function (err, results) {
+ assert.isNull(err);
+ assert.deepEqual(results, { xyz: 468, abc: 123 });
+ }
+ },
+
+ }
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.