如何将命令行参数传递给嵌套脚本? [英] How to pass a command line argument to a nested script?

查看:55
本文介绍了如何将命令行参数传递给嵌套脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这不是关于将args发送到顶级脚本,而是发送到 b

在我的package.json中,当我调用一个直接接受命令行args的脚本时,它可以工作。但是当我调用一个调用其他脚本的脚本时,它并没有将命令行args传递给它。我如何通过它们?

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

当我运行以下命令时,args通过:

  npm run takes-args  -  -env dev 

但是当我通过其他脚本运行它时,它永远不会得到args。有没有办法让他们失望?也许通过像美元符号这样的变量标记?

  //顶级脚本获取args,BUT take-args NOT 
npm run calls-takes-args - -env dev

有没有办法?

解决方案

您的脚本字段应如下所示:

  {
...
take-args:somemodule,
来电 - take-args:npm run takes-args -
}

注意 - call-takes-args 结束时。



- 之后传递的任何内容都会直接附加到您正在运行的脚本上。当你运行 npm run calls-takes-args - -env dev 时,这相当于运行 npm run takes-args -env dev 。当然,这不起作用。



如果你将 - 添加到来电-takes-args ,当你运行 npm run calls-takes-args - -env dev npm run 运行 npm run takes-args - -env dev 。成功!



如果你没有将任何args传递给 calls-takes-args ,则尾随 - 不会受到伤害。






编辑:



如果你不能/不想修改你的 package.json ,你可以运行

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

那将运行 somemodule -env dev


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. But when I call a script that calls that other script, it's not passing the command line args to it. 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:

npm run takes-args -- -env dev

But when I run it through the other script, it never gets the 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:

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

Notice the -- at the end of 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. 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. Success!

If you don't pass any args to calls-takes-args, the trailing -- won't hurt.


Edit:

If you can't/don't want to modify your package.json, you can run

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

That will run somemodule -env dev.

这篇关于如何将命令行参数传递给嵌套脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆