How to use the keyInSelect function from readline-sync
Find comprehensive JavaScript readline-sync.keyInSelect code examples handpicked from public code repositorys.
readline-sync.keyInSelect is a function that takes an array of options and prompts the user to select an option by typing a corresponding key.
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
+ 15 other calls in file
87 88 89 90 91 92 93 94 95 96
) data.projectVersion = readLineSync.question('> Project version (0.1.0): ') data.projectVersion = data.projectVersion === '' ? '0.1.0' : data.projectVersion data.license = readLineSync.keyInSelect( licenses, '> Select your license: ', { cancel: false
+ 15 other calls in file
How does readline-sync.keyInSelect work?
readline-sync.keyInSelect() is a synchronous method in the readline-sync library that presents a list of choices to the user and waits for them to select an option by typing in its corresponding key, returning the index of the selected option. When called, keyInSelect takes two arguments: an array of strings representing the list of choices to be presented to the user, and an optional object containing configuration options. The method then outputs the list of choices to the console, waits for the user to select an option, and returns the index of the selected option. The configuration options that can be passed include: cancel: a string to be used as the cancel option label, or false if the cancel option should not be included. guide: a boolean indicating whether or not to display the selection guide (the left-hand column of the output indicating the key corresponding to each option). hideEchoBack: a boolean indicating whether or not to hide the user's key input in the console (useful for entering passwords or other sensitive information). printFn: a function to use for outputting text to the console, allowing for customization of the output format.
64 65 66 67 68 69 70 71 72 73
return db.sequelize.sync({ force: force, logging: false }) .then(() => true) .catch(err => { if (err.name === 'SequelizeConnectionError') { console.log('Can not connect to the database.') let answer = readLine.keyInSelect(['Try again.', 'Close the installer'], 'What do you want to do?') if (answer === 0) { dbSync(force) } else { return false
+ 13 other calls in file
GitHub: AnthonyLzq/Simba.js
211 212 213 214 215 216 217 218 219 220
limitMessage: 'That is not a valid email!' }) config.version = readLineSync.question('> Project version (0.1.0): ') config.version = config.version === '' ? PROJECT_VERSION : config.version config.license = LICENSES[ readLineSync.keyInSelect(LICENSES, '> Select your license: ', { cancel: false }) ] .toLowerCase()
+ 14 other calls in file
Ai Example
1 2 3 4 5 6
const readline = require("readline-sync"); const options = ["Option 1", "Option 2", "Option 3"]; const index = readline.keyInSelect(options, "Which option do you prefer?"); console.log(`You selected ${options[index]}`);
In this example, options is an array of strings representing the available options. keyInSelect will present these options to the user and ask them to select one using the arrow keys, then press Enter to confirm their choice. The selected option's index in the array will be returned, or -1 if the user cancels the selection.
GitHub: simplify-framework/cli
154 155 156 157 158 159 160 161 162 163
function selectParameter(param, docYaml, resultParameters, stackParamteres) { const descParam = docYaml.Parameters[param].Description const allowedValues = docYaml.Parameters[param].AllowedValues const options = Object.keys(stackParamteres).map(x => `${x} = ${stackParamteres[x]}`) const index = readlineSync.keyInSelect(allowedValues || options, `Select a value for ${CPROMPT}${param}${CRESET} parameter (${descParam || ''}) ?`, { cancel: allowedValues ? `${CBRIGHT}None${CRESET} - (No change)` : ${CBRIGHT}Manual enter a value${CRESET} - (Continue)` }) if (index >= 0) { resultParameters[param] = stackParamteres[Object.keys(stackParamteres)[index]]
+ 27 other calls in file
GitHub: digistump/OakCLI
300 301 302 303 304 305 306 307 308 309
console.log('OakCLI tool version '+version); if (selectedDevice == null) { index = readlineSync.keyInSelect(devicesList, 'Which device would you like to use?', {cancel: 'Exit'}); } else { console.log("Currently selected device: ".yellow.bold+selectedDevice.cyan.bold); index = readlineSync.keyInSelect(devicesList, 'Select a device to switch active devices:', {cancel: 'Exit'}); } if (index < 0) { process.exit(0); } else if (devicesList[index] == '------------------------') {
+ 3 other calls in file
GitHub: AnthonyLzq/Simba.js
263 264 265 266 267 268 269 270 271 272
else config.ghat = false config.database = DATABASES[ Object.keys(DATABASES)[ readLineSync.keyInSelect( Object.keys(DATABASES), '> Select your database: ', { cancel: false
+ 3 other calls in file
GitHub: Alafazam/simple_projects
23 24 25 26 27 28 29 30 31
// Do something... } var animals = ['Lion', 'Elephant', 'Crocodile', 'Giraffe', 'Hippo']; var index = readlineSync.keyInSelect(animals, 'Which animal?'); console.log('Ok, ' + animals[index] + ' goes to your room.');
13 14 15 16 17 18 19 20 21 22
console.log(`->${userName}は${favFood}が好きなんだね!`); </code></pre><p><code>{hideEchoBack: true}</code>というオプションをつけると入力時は<code>*</code>で表示されます。</p><ul><li>実行</li></ul><p><img src=/img/blog/024/01_image.png alt></p><h2 id=選択肢を出して選ばせる>選択肢を出して選ばせる</h2><p>選択肢を提示してユーザーに選ばせる方法もよく使われます。</p><p>keyInSelectメソッドを使います。</p><pre><code>//app.js const readlineSync = require('readline-sync'); let animals = ['のびすけ', 'うらら', 'ちゃちゃまる', 'うこ', 'ギャル電']; let index = readlineSync.keyInSelect(animals, '誰に連絡しますか?'); console.log(`${animals[index]}に連絡します。`); </code></pre><ul><li>実行してみる</li></ul><p><img src=/img/blog/024/02_image.png alt></p><p><strong>人の名前を選択肢して、その人に連絡をする</strong>みたいなインターフェイスを作って見ました。変数がanimalsなのは気にしません。</p><h2 id=cliループでサブコマンドを作る>CLIループでサブコマンドを作る</h2><p>コマンド終了のタイミングをユーザーに委ねたいときに使えます。</p><p>promptCLLoopメソッドで関数を引数に渡すと関数をループ実行してくれます。</p><p>このサンプルだと<code>bye</code>というサブコマンドを入力すると終了します。</p><p>逆に言えば<code>bye</code>を入力するまではずっとその他コマンドの入力を待ち受けます。</p><pre><code>//app.js const readlineSync = require('readline-sync');
18 19 20 21 22 23 24 25 26 27
'Display access token', 'List my OneDrive' ]; while (choice != -1) { choice = readline.keyInSelect(choices, 'Select an option', { cancel: 'Exit' }); switch (choice) { case -1: // Exit
+ 14 other calls in file
GitHub: brendengerber/aWindyDay
797 798 799 800 801 802 803 804 805 806
//Used in prompts to obtain user input. //Options should be an array of strings. //Selection will be returned in all lower case letters. formattedPrompt(options){ let answer = readlineSync.keyInSelect(options, "", {guide: false, cancel: false, hideEchoBack: true, mask: ""}); return options[answer].toLowerCase(); }, next(){
+ 2 other calls in file
32 33 34 35 36 37 38 39 40 41
const age = readline.questionInt("Tuoi: "); console.log( "Chon gioi tinh [1: male, 2: female, 3: other] (Khong can bam Enter):" ); const genderOptions = ["male", "female", "other"]; const genderIndex = readline.keyInSelect(genderOptions); if (genderIndex === -1) { console.log("Hủy thêm sinh viên."); return; }
+ 3 other calls in file
readline-sync.question is the most popular function in readline-sync (3769 examples)