Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 1.34 KB

input.md

File metadata and controls

33 lines (26 loc) · 1.34 KB

Input plugin

Features

  • Prompt / Ask a question to the user
  • Hide the input characters for sensible infos

Example

const sh = require('kool-shell')

function yesNoQuestion(answer) {
  return answer === '' || answer.match(/^y(es)?$/i)
}

sh.input('Do you like omelette du fromage? (Yes)', {
    onSubmit: yesNoQuestion
  })
  .then(omeletteLover => {
    if (omeletteLover) console.log('Nice. I like it too.')
  })

Usage

sh.input(label, [options])

Ask label to the user.
Resume the process.stdin input stream and wait for the user answer.
Return a promise that will be resolved with the answer, when the user has answered.

  • label (String): A statement or query to write to the console, prepended to the prompt.
  • options (Object)
    • hidden (Boolean, default false): Hide the user input with the * char. This is useful for sensible data like passwords.
    • onSubmit: (Function): A function that will be called when the user has answered. You can return a simple value or a promise if you want to do asynchronous actions. The input will resolve with the return value from the onSubmit function. If you reject your onSubmit promise, sh.input will be rejected as well. The onSubmit option can be useful to avoid too much nested promises or to process the user answer before resolving sh.input.