cli-interact
Advanced tools
Comparing version 0.1.5 to 0.1.6
39
index.js
@@ -33,2 +33,38 @@ //============================== | ||
function getChoiceByChar (title, choices, flagAllowNoAnswer) { | ||
var answer; | ||
var i; | ||
var numChoices = choices.length; | ||
var parts = ['Select ', title, ' (']; | ||
var queryText; | ||
var str; | ||
var value; | ||
for (i = 1; i <= numChoices; i += 1) { // display 1 based to user | ||
parts.push(i); | ||
parts.push('='); | ||
parts.push(choices[i - 1]); // choices array is 0-based | ||
parts.push('; '); | ||
} | ||
parts.pop(); // get rid of trailing ';' | ||
parts.push(') ?'); | ||
queryText = parts.join(''); | ||
while (true) { | ||
answer = readlineSync.question(queryText); | ||
if (answer === "\u0003") { | ||
// Ctl-C | ||
process.exit(); | ||
} else if (flagAllowNoAnswer && (answer === '')) { | ||
return answer; | ||
} else { | ||
value = parseInt(answer, 10); | ||
if ((value >= 1) && (value <= numChoices)) { | ||
return choices[value - 1]; // user input is 1-based; choices array is 0-based | ||
} | ||
} | ||
} | ||
} | ||
function getInteger (promptText) { // convenience method | ||
@@ -71,3 +107,3 @@ return getNumber(promptText, true); | ||
var answer; | ||
var queryText = title + ' (y/n)? ' | ||
var queryText = title + ' (y/n)? '; | ||
while (true) { | ||
@@ -105,2 +141,3 @@ answer = readlineSync.question(queryText); | ||
getChar : getChar | ||
, getChoiceByChar : getChoiceByChar | ||
, getInteger : getInteger | ||
@@ -107,0 +144,0 @@ , getIPversion : getIPversion |
{ | ||
"name": "cli-interact", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"title": "cli interact", | ||
@@ -5,0 +5,0 @@ "description": "Simple helper tools for interacting synchronously with user at command line", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9373
13
224