如何访问 CLI 参数的多个值
How to access multiple values of a CLI argument
我有一个节点 JS 脚本,它通过执行 ./test.js -n name1 -r us-east
从命令行读取参数。这里有两个参数 -n
& -r
分别传递了适当的值。现在我必须引入另一个参数 -i
,它将接收至少一个值和至多多个值。表示脚本执行为:
./test.js -n name1 -i 100 101 103 -r us-east
如何找到每个参数的参数值的数量,例如在上面的示例中参数 -i
有 3
值。
如何将这些值存储到数组中。
var data = new Object();
function usage()
{
return "test.js [-n|--name] [-i|--ids <id1 id2...>] [-r|--region] ";
}
function process_args()
{
var args = process.argv.slice(2);
var i;
for (i = 0; i > args.length; i++)
{
switch(args[i])
{
case "-n":
case "--name":
i++;
if (i >= args.length)
error_exit_usage("Missing name argument");
data.name =args[i];
break;
case "-i":
case "--ids":
i++;
if (i >= args.length)
error_exit_usage("Missing ids argument");
/* TODO */
break;
case "-r":
case "--region":
i++;
if (i >= args.length)
error_exit_usage("Missing region argument");
data.region = args[i];
break;
}
}
}
Node JS 的新手。
我想你可以只看下一个参数,只要它们是 ID:
case "-i":
case "--ids":
data.ids = [];
while (/^\d+$/.test(args[i])) {
data.ids.push(Number(args[i]));
i++;
}
if (data.ids.length === 0) {
error_exit_usage("Missing ids argument");
}
break;
试试这个:
function processArguments(args) {
var options = {}, optionName = null;
args.forEach(function (val, index, array) {
if (val.indexOf('-') === 0) {
optionName = val.replace('-', '');
options[optionName] = [];
} else {
if (optionName != null) {
options[optionName].push(val);
}
}
});
return options;
}
var options = processArguments(process.argv);
console.log(options['n']);
console.log(options['i']);
console.log(options['r']);
使用 commander
并将 i 选项强制到列表。将其作为逗号分隔值传入。参见 examples。
始终在 npmjs.com 或节点 modules.com 上搜索或使用 modsearch
,因为有许多模块适用于诸如此类的常见场景。
所以您可以使用的一种方法是
您可以发送类似的参数。/test.js -n name1 -i "100 101 103" -r us-east
然后使用 split function 拆分它们,这将 return 数组。
function process_args()
{
var args = process.argv.slice(2);
var i;
for (i = 0; i > args.length; i++)
{
switch(args[i])
{
case "-n":
case "--name":
i++;
if (i >= args.length)
error_exit_usage("Missing name argument");
data.name =args[i];
break;
case "-i":
case "--ids":
i++;
if (i >= args.length)
error_exit_usage("Missing ids argument");
data.id=args[i].split(" ")
break;
case "-r":
case "--region":
i++;
if (i >= args.length)
error_exit_usage("Missing region argument");
data.region = args[i];
break;
}
}
}
我有一个节点 JS 脚本,它通过执行 ./test.js -n name1 -r us-east
从命令行读取参数。这里有两个参数 -n
& -r
分别传递了适当的值。现在我必须引入另一个参数 -i
,它将接收至少一个值和至多多个值。表示脚本执行为:
./test.js -n name1 -i 100 101 103 -r us-east
如何找到每个参数的参数值的数量,例如在上面的示例中参数
-i
有3
值。如何将这些值存储到数组中。
var data = new Object(); function usage() { return "test.js [-n|--name] [-i|--ids <id1 id2...>] [-r|--region] "; } function process_args() { var args = process.argv.slice(2); var i; for (i = 0; i > args.length; i++) { switch(args[i]) { case "-n": case "--name": i++; if (i >= args.length) error_exit_usage("Missing name argument"); data.name =args[i]; break; case "-i": case "--ids": i++; if (i >= args.length) error_exit_usage("Missing ids argument"); /* TODO */ break; case "-r": case "--region": i++; if (i >= args.length) error_exit_usage("Missing region argument"); data.region = args[i]; break; } } }
Node JS 的新手。
我想你可以只看下一个参数,只要它们是 ID:
case "-i":
case "--ids":
data.ids = [];
while (/^\d+$/.test(args[i])) {
data.ids.push(Number(args[i]));
i++;
}
if (data.ids.length === 0) {
error_exit_usage("Missing ids argument");
}
break;
试试这个:
function processArguments(args) {
var options = {}, optionName = null;
args.forEach(function (val, index, array) {
if (val.indexOf('-') === 0) {
optionName = val.replace('-', '');
options[optionName] = [];
} else {
if (optionName != null) {
options[optionName].push(val);
}
}
});
return options;
}
var options = processArguments(process.argv);
console.log(options['n']);
console.log(options['i']);
console.log(options['r']);
使用 commander
并将 i 选项强制到列表。将其作为逗号分隔值传入。参见 examples。
始终在 npmjs.com 或节点 modules.com 上搜索或使用 modsearch
,因为有许多模块适用于诸如此类的常见场景。
所以您可以使用的一种方法是 您可以发送类似的参数。/test.js -n name1 -i "100 101 103" -r us-east
然后使用 split function 拆分它们,这将 return 数组。
function process_args()
{
var args = process.argv.slice(2);
var i;
for (i = 0; i > args.length; i++)
{
switch(args[i])
{
case "-n":
case "--name":
i++;
if (i >= args.length)
error_exit_usage("Missing name argument");
data.name =args[i];
break;
case "-i":
case "--ids":
i++;
if (i >= args.length)
error_exit_usage("Missing ids argument");
data.id=args[i].split(" ")
break;
case "-r":
case "--region":
i++;
if (i >= args.length)
error_exit_usage("Missing region argument");
data.region = args[i];
break;
}
}
}