npm run 命令传带横线参数

在使用 npm run xxx 时候,如何给命令传入带横线参数?如 -h -p -r 等,直接传入会导致参数丢失。

本人习惯使用项目下的 node_modules 运行 node 项目,不喜欢全局安装包。一般添加 package.json 添加一条script即可,如下

1
2
3
4
5
6
7
{
...
"scripts": {
"hexo": "./node_modules/.bin/hexo"
},
...
}

如上图,在使用 hexo命令 新建文章,可这样使用

1
$ npm run hexo new "Test"

以上毫无问题!

但是如果要给文章自定路径名字传入 -p 或者 --path 参数时,如果直接传入,会变成这样:

1
2
3
4
5
6
$ npm run hexo new 'Test' -p 'hexo/test'

> hexo-site@0.0.0 hexo linx
> hexo "new" "Test" "hexo/test"

INFO Created: xxxx

结果 -p 参数丢失了,文章去不了自定目录,被创建在了 source/_posts/hexo-test.md ,这可不是我们想要的啊。

正确传参的使用姿势:

1
2
3
4
5
6
$ npm run hexo new 'Test' -- -p 'hexo/test'

> hexo-site@0.0.0 hexo linx
> hexo "new" "Test" "-p" "hexo/test"

INFO Created: xxxx

注意 -p 之前的 -- ,这儿是双横线!

如果需要再添加一个 -r 参数,直接在后面添加即可,如:

1
2
3
4
5
6
$ npm run hexo new 'Test' -- -p 'hexo/test' -r

> hexo-site@0.0.0 hexo linx
> hexo "new" "Test" "-p" "hexo/test" "-r"

INFO Created: xxxx

(完)

本文由 linx(544819896@qq.com) 创作,采用 CC BY 4.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。本文链接为: https://blog.jijian.link/2020-01-04/npm-run-param/

如果您觉得文章不错,可以点击文章中的广告支持一下!