Pass Parameter to Gulp Task

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?)

Leave a Comment