【发布时间】:2017-12-13 16:32:27
【问题描述】:
作为 npm 脚本参数使用时,是否可以调用从 yargs 检索密钥?
OSX 终端中的用户类型:
npm run scaffold --name=blah
在 package.json 中执行:
"scaffold" : "node ./scaffold/index.js -- "
这会导致
const yargs = require('yargs').argv
if (yargs) {
console.log(yargs);
console.log(yargs.name);
process.exit(1)
}
...
result:
{ _: [], '$0': 'scaffold/index.js' }
undefined
这只有在我在 package.json "scaffold" : "node scaffold/index.js --name=blah" 中硬编码时才有效,但我需要它是可配置的。
正如我所说,我正在使用 args,因为它似乎可以很容易地按名称检索键(而不是数组)。接受建议。
我错过了什么?
2017 年 11 月 7 日更新 相关:Sending command line arguments to npm script
但是,在命令行中传递 1: npm run scaffold name=hello
或 2: npm run scaffold --name=hello 会产生:
1: { _: [], '$0': 'scaffold/index.js' }
2: { _: [ 'name=hello' ], '$0': 'scaffold/index.js' }
仍然看不到检索yargs.name 属性的方法。仍然未定义。
2017 年 7 月 13 日更新
暂时,我已经放弃了。这似乎是不可能的。我在终端中手动运行脚本。 例如
node ./scaffold/index.js --name=blah
下图显示了直接执行节点脚本,而不是通过 npm 脚本运行。我添加了https://www.npmjs.com/package/nopt 节点模块,看看它是否有帮助(它没有)。通过 npm 脚本运行时,process.argv.name 仍然未定义。
2017 年 7 月 18 日更新
添加 github 示例:https://github.com/sidouglas/stackoverflow-node-arguments
2017 年 7 月 24 日更新
在命令开始之前添加变量有效
myvar="hello npm run scaffold 而不是 npm run scaffold myvar="hello world"
【问题讨论】:
标签: javascript npm yargs