Skip to content

Instantly share code, notes, and snippets.

@ALEXOTANO
Created August 8, 2020 12:35
Show Gist options
  • Save ALEXOTANO/495d04b8913ff656a5688b49d1f13365 to your computer and use it in GitHub Desktop.
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'
/**
* 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