Package detail

sync-prompt

shovon62MITdeprecated0.4.2

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

The missing prompt function for Node.js

command, line, io, cli

readme

sync-prompt

The missing prompt function for Node.js

Build Status Dependency Status Coverage Status Flattr this git repo

You know how the browser has the prompt function that actually returns what the user entered, and not just require that you grab the value via a callback? Welp, Node.js doesn't have one, until now! Just like in the browser, call prompt and you should be able to prompt users via the command line! No callback hell!

Usage

Simply run npm install sync-prompt, and you should be good to go.

Example usage:

var prompt = require('sync-prompt').prompt;

var name = prompt('What is your name? ');
// User enters "Mike".

console.log('Hello, ' + name + '!');
// -> Hello, Mike!

var password = prompt.hidden('Password: ');
// User enters a password, but nothing will be written to the screen.

API

.prompt([question [, hidden = false]])

When called, it will prompt the user for a command-line input.

Parameters

question

Type: string. Default value: undefined. Optional

When set to a string, the value of question will be outputed to the console.

hidden Deprecated

Type: boolean. Default value: undefined. Optional, but requires question to be set to a string.

Please consider avoiding the use of .prompt('some qustion', true) for hiding what the user types. Instead, consider using the more descriptive, and--what many would argue--self-documenting .prompt.hidden('some question'). The former is deprecated, and will be removed in a future release. The latter makes more sense when you want to implement hidden prompts :).

When set to true, none of the user's input will be written to the console. This is great for passwords.

.prompt.hidden([question])

When called, it will prompt the user for a command-line input, while whatever is typed will not be echoed out to the screen.

Parameters

question

Type string. Default value: undefined. Optional

When set to a string, the value of question will be outputed to the console.

.prompt.isEOF()

Returns true if the console input (stdlib's cin) has reached EOF. False otherwise.

Running Tests

Be sure that you have the dependencies installed, locally, for the project:

npm install

And then run the tests:

npm test

Code Coverage

You can run

npm run coverage

And see a summary of the code coverage on the command line.

You can get an in-depth coverage report in the generated coverage/lcov-report/index.html file.

Licence

Unless stated in file headers, the license is found in the LICENSE file.

changelog

History

  • v0.4.1

    • Removed dependency of termios.h. Including it prevents sync-prompt from compiling on Windows.
  • v0.4.0

    • Added a isEOF method. This allows us to check to see if cin has reached EOF.
  • v0.3.2

    • Just like v0.3.1, there's absolutely no bug fixes. Just some marketing fluff update.
  • v0.3.1

    • a version bump from v0.3.0 to v0.3.1. There are absolutely no bug fixes in this one. I was simply not able to publish to v0.3.0, because I was too eager to publish to v0.3.0, but not actually wanting to publish it. But now, it seems NPM does not allow us to overrite changes to a particular version.
  • v0.3.0

    • added a prompt.hidden method
    • deprecated the hidden parameter being passed to the prompt function, in favour of prompt.hidden. The parameter will still be available, just that it may be removed in a future release
    • fixed a bug where a new-line was not appended after a hidden prompt
    • added support for Node.js v0.11
  • v0.2.2

    • GCC 4.7 now successfully compiles sync-prompt
  • v0.2.1

    • fixed erratum in README.md
  • v0.2.0

    • added an optional parameter to .prompt--hidden. If set to true, the output will not be displayed as the user types. It's great for passwords
    • some code formatting changes
    • updates to unit tests
    • added a script to view code coverage report
  • v0.1.0

    • converted the API to accept a string parameter, and display to stdout.
  • v0.0.1

    • fixed a bug that prevented the prompt from accepting stdin input with spaces in them
  • v0.0.0

    • initial release