简体   繁体   English

如何将命令行参数传递给嵌套脚本?

[英]How to pass a command line argument to a nested script?

NOTE: This is NOT about sending args to the top-level script, but to the script called by that script 注:这是不是发送参数传递给顶层的脚本,而是通过脚本调用的脚本

In my package.json, when I call a script that takes command line args directly, it works. 在我的package.json中,当我调用一个直接接受命令行args的脚本时,它可以工作。 But when I call a script that calls that other script, it's not passing the command line args to it. 但是当我调用一个调用其他脚本的脚本时,它并没有将命令行args传递给它。 How do i pass them? 我如何通过它们?

{
    ...
    "takes-args": "somemodule",
    "calls-takes-args": "npm run takes-args"
}

When i run the below command, the args come through: 当我运行以下命令时,args会通过:

npm run takes-args -- -env dev

But when I run it through the other script, it never gets the args. 但是当我通过另一个脚本运行它时,它永远不会得到args。 Is there some way to pass them down? 有没有办法让他们失望? Maybe by a variable marker like a dollar sign? 也许通过像美元符号这样的可变标记?

//The top-level script gets the args, BUT takes-args does NOT
npm run calls-takes-args -- -env dev

Is there any way? 有什么办法吗?

Your scripts field should look like this: 您的scripts字段应如下所示:

{
    ...
    "takes-args": "somemodule",
    "calls-takes-args": "npm run takes-args --"
}

Notice the -- at the end of calls-takes-args . 请注意--calls-takes-args结束时。

Anything you pass after the -- is directly appended onto the script you are running. --之后传递的任何内容都会直接附加到您正在运行的脚本上。 When you run npm run calls-takes-args -- -env dev , that is the equivalent of running npm run takes-args -env dev . 当你运行npm run calls-takes-args -- -env dev ,这相当于运行npm run takes-args -env dev Of course, that does not work. 当然,这不起作用。

If you add the -- to calls-takes-args , when you run npm run calls-takes-args -- -env dev , npm run runs npm run takes-args -- -env dev . 如果你添加-- to calls-takes-args ,当你运行npm run calls-takes-args -- -env devnpm run运行npm run takes-args -- -env dev Success! 成功!

If you don't pass any args to calls-takes-args , the trailing -- won't hurt. 如果你没有将任何args传递给calls-takes-args ,那么尾随--不会受到伤害。


Edit: 编辑:

If you can't/don't want to modify your package.json , you can run 如果您不能/不想修改package.json ,则可以运行

npm run calls-takes-args -- -- -env dev

That will run somemodule -env dev . 这将运行somemodule -env dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM