Make a script which accept command

What is the correct syntax for running a Node.js script with command-line arguments on Linux or Mac?

To run the script with no arguments, I would simply use the command node stuff.js , but in this case, I'd like to run a script called stuff.js with the arguments "blah", "hee", "woohoo!" .


See http://nodejs.org/docs/latest/api/process.html#process_process_argv

In summary you'll run it like

node stuff.js blah hee "whoohoo!"

Then your arguments are available in process.argv


If you want to do more sophisticated stuff, the following modules are really helpful:

  • yargs by Benjamin Coe
  • commander by TJ Holowaychuk
  • vorpal by David Caccavella
  • nopt by Isaac Schlueter
  • And for fun

  • cli-table by Guillermo Rauch
  • node-multimeter by substack
  • chalk by Sindre Sorhus

  • Nomnom是另一种可能的解决方案。

    链接地址: http://www.djcxy.com/p/32682.html

    上一篇: 在Node.js中重新加载文件?

    下一篇: 制作一个接受命令的脚本