npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inquirer-longer

v0.5.0

Published

A collection of common interactive command line user interfaces.

Downloads

3

Readme

Inquirer.js

npm tests dependencies

A collection of common interactive command line user interfaces.

Goal and philosophy

We strive at providing easily embeddable and beautiful command line interface for Node.js; some hope in becoming the CLI Xanadu.

Inquirer should ease the process of asking end user questions, parsing, validating answers, managing hierarchical prompts and providing error feedback.

Inquirer provide the user interface, and the inquiry session flow. If you're searching for a full blown command line program utility, then check out Commander.js (inspired by) or cli-color (used internally).

Documentation

Installation

npm install inquirer
var inquirer = require("inquirer");
inquirer.prompt([/* Pass your questions in here */], function( answers ) {
	// Use user feedback for... whatever!!
});

Examples (Run it and see it)

Checkout the examples/ folder for code and interface examples.

node examples/pizza.js
node examples/checkbox.js
# etc...

Methods

inquirer.prompt( questions, callback )

Launch the prompt interface (inquiry session)

Objects

Question

A question object is a hash containing question related values:

  • type: (String) Type of the prompt. Defaults: input - Possible values: input, confirm, list, rawlist
  • name: (String) The name to use when storing the answer in the anwers hash.
  • message: (String) The question to print.
  • default: (String|Number|Array|Function) Default value(s) to use if nothing is entered, or a function that returns the default value(s). If defined as a function, the first parameter will be the current inquirer session answers.
  • choices: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers.
    Array values can be simple strings, or objects containing a name (to display) and a value properties (to save in the answers hash). Values can also be a Separator.
  • validate: (Function) Receive the user input and should return true if the value is valid, and an error message (String) otherwise. If false is returned, a default error message is provided.
  • filter: (Function) Receive the user input and return the filtered value to be used inside the program. The value returned will be added to the Answers hash.
  • when: (Function) Receive the current user answers hash and should return true or false depending on whether or not this question should be asked.

validate, filter and when functions can be asynchronously using this.async(). You just have to pass the value you'd normally return to the callback option.

{
  validate: function(input) {

    // Declare function as asynchronous, and save the done callback
    var done = this.async();

    // Do async stuff
    setTimeout(function() {
      if (typeof input !== "number") {
        // Pass the return value in the done callback
        done("You need to provide a number");
        return;
      }
      // Pass the return value in the done callback
      done(true);
    }, 3000);
  }
}

Answers

A key/value hash containing the client answers in each prompt.

  • Key The name property of the question object
  • Value (Depends on the prompt)
    • confirm: (Boolean)
    • input : User input (filtered if filter is defined) (String)
    • rawlist, list : Selected choice value (or name if no value specified) (String)

Separator

A separator can be added to any choices array:

// In the question object
choices: [ "Choice A", new inquirer.Separator(), "choice B" ]

// Which'll be displayed this way
[?] What do you want to do?
 > Order a pizza
   Make a reservation
   --------
   Ask opening hours
   Talk to the receptionnist

The constructor takes a facultative String value that'll be use as the separator. If omitted, the separator will be --------.

Separator instances have a property type equal to separator. This should allow tools façading Inquirer interface from detecting separator types in lists.

Prompts type

Note:: allowed options written inside square brackets ([]) are optional. Others are required.

List - { type: "list" }

Take type, name, message, choices[, default, filter] properties. (Note that default must be the choice index in the array or a choice value)

List prompt


Raw List - { type: "rawlist" }

Take type, name, message, choices[, default, filter] properties. (Note that default must the choice index in the array)

Raw list prompt


Expand - { type: "expand" }

Take type, name, message, choices[, default, filter] properties. (Note that default must be the choice index in the array)

Note that the choice object will take an extra parameter called key for the expand prompt. This parameter must be a single (lowercased) character. The h option is added by the prompt and shouldn't be defined by the user.

See examples/expand.js for a running example.

Expand prompt closed Expand prompt expanded


Checkbox - { type: "checkbox" }

Take type, name, message, choices[, filter, validate, default] properties. default is expected to be an Array of the checked choices value.

Choices marked as { checked: true } will be checked by default.

Choices who're property disabled is truthy will be unselectable. If disabled is a string, then the string will be outputed next to the disabled choice, otherwise it'll default to "Disabled". The disabled property can also be a synchronous function receiving the current answers as argument and returning a boolean or a string.

Checkbox prompt


Confirm - { type: "confirm" }

Take type, name, message[, default] properties. default is expected to be a boolean if used.

Confirm prompt


Input - { type: "input" }

Take type, name, message[, default, filter, validate] properties.

Input prompt


Password - { type: "password" }

Take type, name, message[, default, filter, validate] properties.

Password prompt

User Interfaces and layouts

Along with the prompts, Inquirer offers some basic text UI.

Bottom Bar - inquirer.ui.BottomBar

This UI present a fixed text at the bottom of a free text zone. This is useful to keep a message to the bottom of the screen while outputting command outputs on the higher section.

var ui = new inquirer.ui.BottomBar();

// pipe a Stream to the log zone
outputStream.pipe( ui.log );

// Or simply write output
ui.log.write("something just happened.");
ui.log.write("Almost over, standby!");

// During processing, update the bottom bar content to display a loader
// or output a progress bar, etc
ui.updateBottomBar("new bottom bar content");

Prompt - inquirer.ui.Prompt

This is UI layout used to run prompt. This layout is returned by inquirer.prompt and you should probably always use inquirer.prompt to interface with this UI.

Support (OS Terminals)

You should expect mostly good support for the CLI below. This does not mean we won't look at issues found on other command line - feel free to report any!

  • Mac OS:
    • Terminal.app
    • iTerm
  • Windows:
    • cmd.exe
    • Powershell
    • Cygwin
  • Ubuntu:
    • Terminal

News on the march (Release notes)

Please refer to the Github releases section for the changelog

Contributing

Style Guide
Please brief yourself on Idiomatic.js style guide with two space indent

Unit test
Unit test are written in Mocha. Please add a unit test for every new feature or bug fix. npm test to run the test suite.

Documentation
Add documentation for every API change. Feel free to send corrections or better docs!

Pull Requests
Send fixes PR on the master branch. Any new features should be send on the wipbranch.

We're looking to offer good support for multiple prompts and environments. If you want to help, we'd like to keep a list of testers for each terminal/OS so we can contact you and get feedback before release. Let us know if you want to be added to the list (just tweet to @vaxilart) or just add your name to the wiki

License

Copyright (c) 2012 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.