node.js 制作一个接受命令行参数的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12925802/
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
Make a script which accept command-line arguments
提问by Anderson Green
What is the correct syntax for running a Node.js script with command-line arguments on Linux or Mac?
在 Linux 或 Mac 上运行带有命令行参数的 Node.js 脚本的正确语法是什么?
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.jswith the arguments "blah", "hee", "woohoo!".
要运行不带参数的脚本,我只需使用 command node stuff.js,但在这种情况下,我想运行一个stuff.js带参数调用的脚本"blah", "hee", "woohoo!"。
回答by hexist
See http://nodejs.org/docs/latest/api/process.html#process_process_argv
请参阅http://nodejs.org/docs/latest/api/process.html#process_process_argv
In summary you'll run it like
总之,你会像这样运行它
node stuff.js blah hee "whoohoo!"
node stuff.js blah hee "whoohoo!"
Then your arguments are available in process.argv
那么你的论点可以在 process.argv
回答by zemirco
If you want to do more sophisticated stuff, the following modules are really helpful:
如果你想做更复杂的事情,以下模块真的很有帮助:
And for fun
并且为了好玩
- cli-tableby Guillermo Rauch
- node-multimeterby substack
- chalkby Sindre Sorhus
回答by Andreas
This simple node module is also helpfull: command-line-args
这个简单的节点模块也很有帮助:命令行参数
It allows to define a simple definition:
它允许定义一个简单的定义:
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'timeout', alias: 't', type: Number }
]
It validates your options and allows you to access them in a simple way.
它验证您的选项并允许您以简单的方式访问它们。
回答by Rohit Jaiswal
The arguments are stored in
参数存储在
process.argvand to pass the arguments in command line please check below example:
process.argv并在命令行中传递参数,请检查以下示例:
ex. in this example below i have used commander NPM Module. var args = require('commander')
前任。在下面的这个例子中,我使用了指挥官 NPM 模块。 var args = require('指挥官')
Optionswith commander are defined with the .option()method. The example below parses args and options from process.argv, leaving remaining args as the program.args array which were not consumed by options. here process.argvis 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 after executing.
带有指挥官的选项是用.option()方法定义的。下面的示例解析process.argv 中的args 和选项,将剩余的 args 保留为 program.args 数组,这些数组未被选项消耗。这里process.argv是一个包含命令行参数的数组。第一个元素将是“节点”,第二个元素将是 JavaScript 文件的名称。下一个元素将是执行后的任何其他命令行参数。
function list(val) {
return val.split(',');
}
args.version('0.11.2')
.option('-t, --tag [value]', 'tags to ignore', list, undefined)
.parse(process.argv);
here to take input from command-line, we have to execute .js file with -t and after that arguments separated by comma(,)incase of multiple arguments ex. : node example.js -t tagnamehere i have used list to process multiple command line arguments ,so that we can pass multiple command line arguments ex. node example.js -t tagname1, tagname2so after this , all input passed as command line arguments will be available in array named args, so can use this array for your purpose and you can read more about it from here:-
这里要从命令行获取输入,我们必须使用 -t 执行 .js 文件,然后用逗号 (,) 分隔的参数在多个参数 ex 的情况下执行。: node example.js -t tagname这里我使用 list 来处理多个命令行参数,以便我们可以传递多个命令行参数 ex。node example.js -t tagname1, tagname2所以在此之后,作为命令行参数传递的所有输入都将在名为 args 的数组中可用,因此可以将此数组用于您的目的,您可以从此处阅读有关它的更多信息:-
https://nodejs.org/docs/latest/api/process.html#process_process_argv
https://nodejs.org/docs/latest/api/process.html#process_process_argv
or you can make use of the following modules :
或者您可以使用以下模块:
- commander:-
- 指挥官:-
- yargs:-
- yargs:-
- vorpal:-
- 涡流:-

