Saturday, September 5, 2020

Command line arguments and console input

 

console.log(process.argv)
//1st value--node.exe path.. 2nd js file
//node 3.js 1 2 3 uday --user kiran 
console.log(process.argv.indexOf('--user'));//returns -1 if not found

process.stdout.write('Ask me a question');

process.stdin.on('data', function(answer){
 console.log(answer.toString());
  process.exit();
});

//readline.js:
const readline = require('readline');
var util = require('util');
var RL = readline.createInterface(process.stdin,process.stdout);
RL.question("What is yor name",(name)=>{
console.log(name);

RL.setPrompt(`${name} how old are you?`);
RL.prompt();
RL.on('line',(age)=>{//listening to line event
if(age<18){
 util.log(`${name.trim()} because ${age} can't proceed`);
 RL.close();
}
else{
 util.log(`${name.trim()} because ${age} can proceed`);
 RL.close();
}
});

});

No comments:

Post a Comment