node.js 有人可以解释一下node.js中“process.argv”的含义吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22213980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:58:16  来源:igfitidea点击:

Could someone explain what "process.argv" means in node.js please?

node.js

提问by Justin R.

I'm currently learning node.js, and I was just curious what that meant, I am learning and could you tell me why this code does what it does:

我目前正在学习 node.js,我只是好奇这意味着什么,我正在学习,你能告诉我为什么这段代码会这样做:

var result = 0;

  for (var i = 2; i < process.argv.length; i++){
    result += Number(process.argv[i]);
}
  console.log(result);

I know it adds the numbers that you add to the command line, but why does "i" start with 2? I understand the for loop, so you don't have to go into detail about that.

我知道它会添加您添加到命令行的数字,但为什么“i”以 2 开头?我了解 for 循环,因此您不必详细说明。

Thank you so much in advance.

非常感谢你。

回答by slezica

You could just settle this with console.log(process.argv).

你可以用 解决这个问题console.log(process.argv)

It starts on 2 because process.argvcontains the whole command-line invocation:

它从 2 开始,因为process.argv包含整个命令行调用:

process.argv = ['node', 'yourscript.js', ...]

Elements 0and 1are not what you would call "arguments", but there they are.

Elements 0and1不是您所说的“参数”,但它们确实存在。

回答by Ray Toal

It starts with 2 because the code will be run with

它以 2 开头,因为代码将运行

node myprogram.js firstarg secondarg

So

所以

process.argv[0] == "node"

process.argv[1] == "myprogram.js"

process.argv[2] == "firstarg"

Online docs

在线文档

回答by maerics

Your program prints the sum of the numerical values of the "command line arguments" provided to the node script.

您的程序打印提供给节点脚本的“命令行参数”的数值总和。

For example:

例如:

$ /usr/local/bin/node ./sum-process-argv.js 1 2 3
6

From the Node.js API documentation for process.argv:

来自Node.js API 文档process.argv

An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

包含命令行参数的数组。第一个元素将是“节点”,第二个元素将是 JavaScript 文件的名称。下一个元素将是任何其他命令行参数。

In the above examples those values are:

在上面的例子中,这些值是:

process.argv[0] == '/usr/local/bin/node'
process.argv[1] == '/Users/maerics/src/js/sum-process-argv.js'
process.argv[2] == '1'
process.argv[3] == '2'
process.argv[4] == '3'

See also the Number(...)function/contructorfor JavaScript.

另请参阅JavaScript的Number(...)函数/构造函数

回答by Kadiro Elemo

In the node.js, the command line arguments are always stored in an array. In that array, the first element is the node command we refer to because we begin the command line with word “node”. The second element is the javascript (JS) file we refer to that often comes after the node command.

在 node.js 中,命令行参数总是存储在一个数组中。在该数组中,第一个元素是我们引用的节点命令,因为我们以单词“node”开始命令行。第二个元素是我们引用的 javascript (JS) 文件,它经常出现在 node 命令之后。

As we know, in the first element in JS array begins from zero, the second element is 1, and it goes 2, 3, 4 and on. Let's name this array process.argv and add command line arguments x and y. Then, this is how we are going to call these elements:

众所周知,JS数组的第一个元素从0开始,第二个元素是1,依次为2、3、4。让我们将此数组命名为 process.argv 并添加命令行参数 x 和 y。然后,这就是我们将如何调用这些元素:

var process.argv = ['node', 'file.js', ‘x', ‘y'];
var process.argv [0] = node;
var process.argv [1]= file.js;
var process.argv[2] = x;
var process.argv[3] = y;

In short, element 0 and 1 are native to node.js, and we don't use them when we write any command line argument. That's why we ignore 0 and 1 and always begin from 2.

简而言之,元素 0 和 1 是 node.js 原生的,我们在编写任何命令行参数时不使用它们。这就是为什么我们忽略 0 和 1 并且总是从 2 开始。

If we want to loop through the command line arguments, again we have to start from 2. Here is what we use for looping.

如果我们想循环遍历命令行参数,我们必须再次从 2 开始。这是我们用于循环的内容。

for (i = 2; i < process.argv.length; i++){
console.log(process.argv[i]);
}

回答by ?rem Uzunbaz

When you execute it like:

当你像这样执行它时:

node code.js <argument> <argument>....

It take into account all command line invocation. For process.argv[]array will have ["node","code.js","<argument>",...]
Because of that your arguments that in array start with index 2

它考虑了所有命令行调用。因为process.argv[]数组将有["node","code.js","<argument>",...]
因为数组中的参数以索引 2 开头

回答by robe007

My answer is not about on how process.argvworks -'cause there is a lot of answers here-, instead, it is on how you can get the values using array destructuring syntax.

我的答案不是关于如何process.argv工作 - 因为这里有很多答案 - 相反,它是关于如何使用array destructuring syntax.

For example, if you run your script with:

例如,如果您使用以下命令运行脚本:

node app.js arthur 35

you can get those values in a more readableway like this:

您可以像这样以更具可读性的方式获取这些值:

const [node, script, name, age] = process.argv;

console.log(node); // node
console.log(script); // app.js
console.log(name); // arthur
console.log(age); // 35

You can omit the first and second placesof your process.argv, and stay only with nameand age:

你可以省略你的第一个和第二个位置process.argv只留下nameage

const [, , name, age] = process.argv;


If you want all the arguments in an array, you can do it using the rest syntax, that collects multiple elements and condenses them into a single elementlike this:

如果你想要一个数组中的所有参数,你可以使用rest syntax,它收集多个元素并将它们压缩成一个元素,如下所示:

const [node, script, ...params] = process.argv;

console.log(node); // node
console.log(script); // app.js
console.log(params); // [ 'arthur', '35' ]

and of course, you can omit the first and second placesof your process.argv, and stay only with your params:

当然,你可以省略你的第一个和第二个地方process.argv只留下你的参数

const [, , ...params] = process.argv;

回答by sugandh goyal

process.agrv[i]- basically loops through the command line arguments passed in the terminal while executing the file. for example- If you run the file as
$ node prog.js 1 2 3 , then process.argv[0]=1 and so on..

process.agrv[i]- 基本上循环执行文件时在终端中传递的命令行参数。例如-如果您将文件作为
$ node prog.js 1 2 3 运行,则 process.argv[0]=1 等等..