admin管理员组文章数量:1222521
I am trying to implement raw command line arguments in Node.js.
When I implement simple variables everything works
(node example.js variable)
But when I implement as an argument an array it doesn't work
(node example.js "['127.0.0.5', '127.0.0.3']" )
Full code:
if (process.argv.length <= 3) {
console.log("Usage: " + __filename + " SOME_PARAM");
process.exit(-1);
}
var variable = process.argv[2];
var array = process.argv[3];
console.log('Host: ' + variable);
console.log('array: ' + array);
Problem
Example of input of arguments ( node example.js variable "['127.0.0.5', '127.0.0.3']"
)
How to pass the second argument ("['127.0.0.5', '127.0.0.3']"
) as an array rather than as a string (as it is now), so that later I might access array's n-th element (example array[0] = '127.0.0.5'
)
SOLUTION
The input should be like ( '["127.0.0.5", "127.0.0.3"]'
changing the quotes), and also we need to parse the argument as JSON.
if (process.argv.length <= 3) {
console.log("Usage: " + __filename + " SOME_PARAM");
process.exit(-1);
}
var variable = process.argv[2];
var array = JSON.parse(process.argv[4]);
console.log('Host: ' + variable);
console.log('array: ' + array);
console.log(array[1]
I am trying to implement raw command line arguments in Node.js.
When I implement simple variables everything works
(node example.js variable)
But when I implement as an argument an array it doesn't work
(node example.js "['127.0.0.5', '127.0.0.3']" )
Full code:
if (process.argv.length <= 3) {
console.log("Usage: " + __filename + " SOME_PARAM");
process.exit(-1);
}
var variable = process.argv[2];
var array = process.argv[3];
console.log('Host: ' + variable);
console.log('array: ' + array);
Problem
Example of input of arguments ( node example.js variable "['127.0.0.5', '127.0.0.3']"
)
How to pass the second argument ("['127.0.0.5', '127.0.0.3']"
) as an array rather than as a string (as it is now), so that later I might access array's n-th element (example array[0] = '127.0.0.5'
)
SOLUTION
The input should be like ( '["127.0.0.5", "127.0.0.3"]'
changing the quotes), and also we need to parse the argument as JSON.
if (process.argv.length <= 3) {
console.log("Usage: " + __filename + " SOME_PARAM");
process.exit(-1);
}
var variable = process.argv[2];
var array = JSON.parse(process.argv[4]);
console.log('Host: ' + variable);
console.log('array: ' + array);
console.log(array[1]
Share
Improve this question
edited Nov 16, 2015 at 9:25
Chris
asked Nov 16, 2015 at 8:55
ChrisChris
9443 gold badges12 silver badges23 bronze badges
1
- You can't "pass it as an array" but you can parse the argument to build your array. You get a string no matter what, it's up to you to convert it into an array. – Yabada Commented Nov 16, 2015 at 9:01
2 Answers
Reset to default 12It's an old question, but if someone still looking for a simple solution, here it is.
Pass named arguments multiple times with same name.
node example.js --variable=127.0.0.5 --variable=127.0.0.3
Use minimist NPM package to extract args as:
const parseArgs = require('minimist');
const args = parseArgs(process.argv.slice(2));
console.log(args.variable);
Output:
[ '127.0.0.5', '127.0.0.3' ]
You won't be able to pass an array. What you have to do (and possibly are in the middle of doing) is passing something like an array converted to a JSON string.
And in the application, you would just do a JSON.parse()
to get your array out of the string.
本文标签: javascriptGetting Array as a raw command line argumentStack Overflow
版权声明:本文标题:javascript - Getting Array as a raw command line argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739386374a2160953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论