asks

An altered fork of inquirer.js, a collection of common interactive command line user interfaces.

asks

A collection of common interactive command line user interfaces.

This is an altered fork of inquirer.js. Thanks Simon Boudrias for the awesome package.

  • this.async()is now available for .defaultfunction.
  • fixes the bug that filter()will be executed more than once.

Goal and philosophy

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

Inquirershould ease the process of asking end user questions, parsing, validatinganswers, managing hierarchical promptsand providing error feedback.

Inquirerprovide 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 asks --save
var asks = require("asks");
asks.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

asks.prompt( questions, callback )

Launch the prompt interface (inquiry session)

Objects

Question

A question object is a hashcontaining 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 asks 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 asks session answers. Array values can be simple strings, or objectscontaining a name(to display) and a valueproperties (to save in the answers hash). Values can also be a Separator.
  • validate: (Function) Receive the user input and should return trueif the value is valid, and an error message (String) otherwise. If falseis 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 Answershash.
  • when: (Function) Receive the current user answers hash and should return trueor falsedepending on whether or not this question should be asked.

default(if defined as a function), validate, filterand whenfunctions can be asynchronously using this.async(). You just have to pass the value you'd normally return to the callback option.

{
  default: function(answers){
    var done = this.async();
    getAsynchronously(function(value){
      done(value);
    });
  },
  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(null);
    }, 3000);
  }
}

Answers

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

  • KeyThe nameproperty of the questionobject
  • Value(Depends on the prompt)
    • confirm: (Boolean)
    • input: User input (filtered if filteris defined) (String)
    • rawlist, list: Selected choice value (or name if no value specified) (String)

Separator

A separator can be added to any choicesarray:

// In the question object
choices: [ "Choice A", new asks.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 Stringvalue that'll be use as the separator. If omitted, the separator will be --------.

Separator instances have a property typeequal 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 indexin the array or a choice value)


Raw List - { type: "rawlist" }

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


Expand - { type: "expand" }

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

Note that the choiceobject will take an extra parameter called keyfor the expandprompt. This parameter must be a single (lowercased) character. The hoption is added by the prompt and shouldn't be defined by the user.

See examples/expand.jsfor a running example.


Checkbox - { type: "checkbox" }

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

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

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


Confirm - { type: "confirm" }

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


Input - { type: "input" }

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


Password - { type: "password" }

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

User Interfaces and layouts

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

Bottom Bar - asks.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 asks.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 - asks.ui.Prompt

This is UI layout used to run prompt. This layout is returned by asks.promptand you should probably always use asks.promptto 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.jsstyle 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 testto run the test suite.

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

Pull Requests
Send fixesPR on the masterbranch. 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) Altered by Kael (@kaelzhang) Licensed under the MIT license.

HomePage

https://github.com/kaelzhang/node-asks

Repository

https://github.com/kaelzhang/node-asks


上一篇:cortex-command-errors
下一篇:cortex-deps-traveller

相关推荐

  • 通过microtasks和macrotasks看JavaScript异步任务执行顺序 | 拓跋的前端客栈

    setTimeout(fn,0)的含义是,指定某个任务在主线程最早可得的空闲时间执行,也就是说,尽可能早得执行。它在”任务队列”的尾部添加一个事件,因此要等到同步任务和”任务队列”现有的事件都处理完,...

    2 年前
  • vhug-tasks

    Buildtasks for VHUG VHUG BUILDING TASKS What is this script doing? In charge of running the dev...

    3 个月前
  • undertaker-common-tasks

    Proofofconcept custom registry that predefines tasks undertakercommontasks NPM vers...

    1 年前
  • ucd-theme-tasks

    Base automation for compiling a UCD frontend. ucdthemetasks Base automation for compiling a UCD fr...

    7 个月前
  • rx-grunt-tasks

    undefined Rx Grunt Tasks for Widgets How to start To start using corporate grunt tasks: ...

    10 个月前
  • react-component-gulp-tasks

    Gulp Build Tasks for React Component Projects reactcomponentgulptasks ========================== T...

    1 年前
  • neat-tasks

    terminal tasks for neatlog neattasks terminal task list for neatlog npmnpmimagenpmurl travistrav...

    5 个月前
  • lvsf-gulp-tasks

    Some common tasks for gulp lvsfgulptasks Build Status(https://travisci.org/LiveSafe/lvsfgulptasks....

    3 个月前
  • load-grunt-tasks

    Load multiple grunt tasks using globbing patterns loadgrunttasks Build Status(https://travisci.org...

    2 年前
  • gulp-require-tasks

    Loads Gulp tasks from directories and individual files gulprequiretasks npm version(https://badge...

    6 个月前

官方社区

扫码加入 JavaScript 社区