将parameter passing给来自stdin的节点脚本
概观
我想将parameter passing给来自stdin的节点脚本。
一般来说,我正在拍这样的东西
nodeScript.js | node {{--attach-args??}} --verbose --dry-run
那会和…一样
node nodeScript.js --verbose --dry-run
更多详情
这里是一个简单的插图脚本dumpargs.js
console.log("the arguments you passed in were"); console.log(process.argv); console.log("");
那么你可以这样做:
node dumpargs.js --verbose --dry-run file.txt [ 'node', '/home/bill-murray/Documents/dumpargs.js', '--verbose', '--dry-run', 'file.js' ]
现在的问题是,如果脚本出现在标准input中(比如说,通过cat
或curl
)
cat dumpars.js | node the arguments you passed in were [ 'node' ]
是否有一个很好的方法来传递参数?
不是节点 :用bash,这次使用
dumpargs.sh
echo "the arguments you passed in were" printf "> $@" echo
答案看起来像
cat dumpargs.sh | bash -s - "--verbose --dry-run file.txt" the arguments you passed in were > --verbose --dry-run file.txt
这不是很好,但工作。
调用node
将启动REPL,所以你的问题应该等同于从terminal手动设置/使用argv
。 尝试做一些事情:
// argv.js process.argv[1] = 'asdf'; process.argv[2] = '1234';
并做cat argv.js dumpargs.js | node
cat argv.js dumpargs.js | node
。