30秒学会 JavaScript 片段 · 2023年10月25日

30秒学会 JavaScript 片段 – Command-line arguments

Gets the command-line arguments passed to a Node.js script.

  • Use process.argv to get an array of all command-line arguments.
  • Use Array.prototype.slice() to remove the first two elements (path of the Node.js executable and the file being executed).

代码实现

const getCmdArgs = () => process.argv.slice(2);

// node my-script.js --name=John --age=30
getCmdArgs(); // ['--name=John', '--age=30']

翻译自:https://www.30secondsofcode.org/js/s/command-line-arguments