概述
我希望能够从标准输入(stdin)中向一个node脚本传递参数。
通常情况下,我希望做到以下这点:
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' ]
现在的问题是,如果该脚本通过标准输入流(例如通过 cat
或 curl
)输入
cat dumpars.js | node
the arguments you passed in were
[ 'node' ]
有没有一种好的方法将参数传递给它?
not node: with bash, using
dumpargs.sh
this timeecho "the arguments you passed in were" printf "> $@" echo
the answer would look like
cat dumpargs.sh | bash -s - "--verbose --dry-run file.txt" the arguments you passed in were > --verbose --dry-run file.txt
bash
的-s
选项一样使用预设的argv
启动node
REPL。 - browles