How to use readline-sync

Comprehensive readline-sync code examples:

How to use readline-sync.questionEmail:

2
3
4
5
6
7
8
9
10
11
// taking string
let username = read.question('Enter your name: ');
let age = read.questionInt('Enter age: ');
console.log(`Name = ${username}, Age = ${age}`);
// use read.questionNewPassword('Enter password: ') and print password
// use read.questionEmail('Enter email: ') and print email
let pass = read.questionNewPassword('Enter password: ');
console.log(`Password = ${pass}`);
let email = read.questionEMail('Enter email: ');
console.log(`Email: ${email}`)

How to use readline-sync.keyInYNStrict:

111
112
113
114
115
116
117
118
119
120
  {
    prompt: '> The project begins in: '
  }
)

data.herokuValidator = readLineSync.keyInYNStrict(
  '> Will this project be deployed with Heroku? ',
  {
    caseSensitive: false
  }

How to use readline-sync.questionFloat:

-1
console.log(`Name = ${name}, age = ${age}, amount = ${amount}, password = ${password}`);

How to use readline-sync.setDefaultOptions:

216
217
218
219
220
221
222
223
224
225
```

### <a name="basic_methods-setdefaultoptions"></a>`setDefaultOptions`

```js
currentDefaultOptions = readlineSync.setDefaultOptions([newDefaultOptions])
```

Change the [Default Options](#basic_options) to the values of properties of `newDefaultOptions` Object.  
All it takes is to specify options that you want change, because unspecified options are not updated.

How to use readline-sync.promptCLLoop:

27
28
29
30
31
32
33
34
35
36
  projectDescription: '',
  projectVersion    : '',
  herokuValidator   : ''
}

readLineSync.promptCLLoop(
  {
    npm: () => {
      data.manager = 'npm'
      return true

How to use readline-sync.questionNewPassword:

42
43
44
45
46
47
48
49
50
    }
    if (!user.email) {
        user.email = readlineSync.questionEMail('E-Mail address: ')
    }
    if (!user.password) {
        user.password = readlineSync.questionNewPassword('New password: ')
    }
    return user
}

How to use readline-sync.promptLoop:

47
48
49
50
51
52
53
54
55
56
  }
)

data.manager = data.manager === 'npm' ? 'npm i' : 'yarn add'

readLineSync.promptLoop(
  input => {
    data.projectName = input.toLowerCase()
    return data.projectName !== ''
  }, 

How to use readline-sync.keyInYN:

13
14
15
16
17
18
19
20
21
22
console.log(JSON.stringify(config.database, null, 2))
await sleep(200)
answer = readLine.keyInYN('Is it correct?')
if (!answer) return console.log('Rectify your config file and then restart this installer')

answer = readLine.keyInYN('Do you want to erase all database content?')
console.log('Starting the migration...')
let sucess = await dbSync(answer)
if (!sucess) return
answer = readLine.keyInYN('Do you want to add a new user?')

How to use readline-sync.keyInSelect:

48
49
50
51
52
53
54
55
56
57
* Let the user choose an item from a list:

```js
var readlineSync = require('readline-sync'),
  animals = ['Lion', 'Elephant', 'Crocodile', 'Giraffe', 'Hippo'],
  index = readlineSync.keyInSelect(animals, 'Which animal?');
console.log('Ok, ' + animals[index] + ' goes to your room.');
```

```console

How to use readline-sync.prompt:

181
182
183
184
185
186
187
188
189
190
```

### <a name="basic_methods-prompt"></a>`prompt`

```js
input = readlineSync.prompt([options])
```

Display a prompt-sign (see [`prompt`](#basic_options-prompt) option) to the user, and then return the input from the user after it has been typed and the Enter key was pressed.  
You can specify an `options` (see [Basic Options](#basic_options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.).

How to use readline-sync.questionEMail:

5
6
7
8
9
10
11
12
13
14
let rs = require('readline-sync');

class User {
  constructor() {
    this.nome = rs.question('Qual o seu nome? ');
    this.email = rs.questionEMail('Digite um endereço de e-mail: ');
    this.endereco = rs.question('Digite seu endereço: ');
    this.formasPagamento = rs.question('Digite uma forma de pagamento: ');
  }
}

How to use readline-sync.keyIn:

0
1
2
3
4
5
6
7
8
9
const readLine = require('readline-sync');
// const name = readLine.question("What's your name?");
// console.log('Welcome ', name);

while (true) {
  const key = readLine.keyIn('Press a key');
  if (key === 'q') {
    process.exit(0);
  }
  if (key === 'a') {

How to use readline-sync.questionInt:

-2
-1
console.log
(`Olá, ${name}. Obrigado por fornecer sua idade: ${age} e seu emaiL: ${email} ` );

How to use readline-sync.question:

11
12
13
14
15
16
17
18
19
20

```js
var readlineSync = require('readline-sync');

// Wait for user's response.
var userName = readlineSync.question('May I have your name? :');
console.log('Hi ' + userName + '!');

// Handle the secret text (e.g. password).
var favFood = readlineSync.question('What is your favorite food? :', {