0

Example of request:

node index.js --DIR="/Downloads" --PATTERN=\.js
  • I found out that process.argv store all command line params. But I'm can't assign some elements of process.argv to DIR and PATTERN constant.
  • And the order params can be different.
Nick Gl
  • 119
  • 6
  • 2
    There's really not much more to say than "`process.argv` stores all command line parameters". – Pointy Nov 30 '17 at 23:06
  • 1
    Possible duplicate of [How do I pass command line arguments?](https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments) – AuxTaco Nov 30 '17 at 23:56
  • Look into [Commander](https://github.com/tj/commander.js/) – Paul Nov 30 '17 at 23:58

1 Answers1

2

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
AuxTaco
  • 4,883
  • 1
  • 12
  • 27