My script prints text to the console every second and stops after 4 seconds. I need to make sure that the interval and stop values can be specified when the file is run. The file should be run with this command: node index.js -i 1000 -t 4000. How can i do this?
app.get("/", (req, res) => {
function outputText() {
console.log("Some Text");
}
const interval = setInterval(outputText, 1000);
setTimeout(() => {
clearInterval(interval);
}, 4000);
res.send();
});
app.listen(3000);
>Solution :
You can use the npm package yargs
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv;
console.log('Interval', argv.i);
console.log('Stop', argv.t);