Created
August 8, 2020 12:35
-
-
Save ALEXOTANO/495d04b8913ff656a5688b49d1f13365 to your computer and use it in GitHub Desktop.
How to create a nodejs script that read arguments passed through the CLI in the form of '--arg=data'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: Alex Otano | |
* This is a implementation of how to get parameters from the CLI when running a nodejs script. | |
* In this case "node-arguments.js --user=myuser --pass=secretPassword" and the script will read them. | |
*/ | |
console.log('---------------------'); | |
let pass = '' | |
let user = '' | |
console.log('process.argv',process.argv) | |
process.argv.forEach(arg=>{ | |
if(arg.indexOf('--user=') > -1) { | |
arg.split('=') | |
user = arg.split('=')[1].toString().trim() | |
} | |
if(arg.indexOf('--pass=') > -1) { | |
arg.split('=') | |
pass = arg.split('=')[1].toString().trim() | |
} | |
}) | |
console.log('user >>> ',user) | |
console.log('pass >>> ',pass) | |
console.log('---------------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment