Nick Gl
Nick Gl

Reputation: 119

Nodejs: Need to assign the command line parameters

Example of request:

node index.js --DIR="/Downloads" --PATTERN=\.js

Upvotes: 0

Views: 172

Answers (1)

AuxTaco
AuxTaco

Reputation: 5171

process.argv stores command line parameters, yes. But they're just plain strings. Node doesn't know what you want to do with them; you'll need to parse them yourself. You'll loop through process.argv.slice(2), figure out which constant is being assigned, handle any quoting or escaping weirdness, and do the assignment manually.

Or, if you don't feel like reinventing the wheel, use something like Yargs:

const argv = require('yargs').argv;

console.log(argv.DIR);      // /Downloads
console.log(argv.PATTERN);  // \.js

Upvotes: 2

Related Questions