将参数传递给Gulp任务 [英] Pass Parameter to Gulp Task

查看:290
本文介绍了将参数传递给Gulp任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我们可以通过类似于 gulp mytask 的方式从控制台运行gulp任务。无论如何,我可以通过参数传递给吞噬任务吗?如果可能的话,请展示如何完成它。

解决方案

这是功能程序不能没有的功能。你可以试试yargs。

  npm install --save-dev yargs 

您可以像这样使用它:

  gulp mytask  - -production --test 1234 

在代码中,例如:

  var argv = require('yargs')。argv; 
var isProduction =(argv.production === undefined)?假:真;

为了您的理解:

 > gulp watch 
console.log(argv.production === undefined); < - true
console.log(argv.test === undefined); < - true

> gulp watch --production
console.log(argv.production === undefined); < - false
console.log(argv.production); < - true
console.log(argv.test === undefined); < - true
console.log(argv.test); < - undefined

> gulp手表--production --test 1234
console.log(argv.production === undefined); < - false
console.log(argv.production); < - true
console.log(argv.test === undefined); < - false
console.log(argv.test); < - 1234

希望您可以从这里购买。



还有另一个可以使用的插件,minimist。还有另外一篇帖子,里面有很多yargs和minimist的例子:(是否可以通过一个标志让Gulp让它以不同的方式运行任务?


Normally we can run gulp task from console via something like gulp mytask. Is there anyway that I can pass in parameter to gulp task? If possible, please show example how it can be done.

解决方案

It's a feature programs cannot stay without. You can try yargs.

npm install --save-dev yargs

You can use it like this:

gulp mytask --production --test 1234

In the code, for example:

var argv = require('yargs').argv;
var isProduction = (argv.production === undefined) ? false : true;

For your understanding:

> gulp watch
console.log(argv.production === undefined);  <-- true
console.log(argv.test === undefined);        <-- true

> gulp watch --production
console.log(argv.production === undefined);  <-- false
console.log(argv.production);                <-- true
console.log(argv.test === undefined);        <-- true
console.log(argv.test);                      <-- undefined

> gulp watch --production --test 1234
console.log(argv.production === undefined);  <-- false
console.log(argv.production);                <-- true
console.log(argv.test === undefined);        <-- false
console.log(argv.test);                      <-- 1234

Hope you can take it from here.

There's another plugin that you can use, minimist. There's another post where there's good examples for both yargs and minimist: (Is it possible to pass a flag to Gulp to have it run tasks in different ways?)

这篇关于将参数传递给Gulp任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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