Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active July 12, 2016 06:31
Show Gist options
  • Save nepsilon/bc07b5e02dfd7652345e to your computer and use it in GitHub Desktop.
Save nepsilon/bc07b5e02dfd7652345e to your computer and use it in GitHub Desktop.
Pass custom options to gulp — First published in fullweb.io issue #15

How to pass custom options to gulp

If you use gulp as build tool for your web projects, you might have wondered how to pass custom flags to your tasks from CLI. Well, with a lil’ help from the node package minimist, you can add this neat feature!

// gulpfile.js
var gulp = require('gulp'); 
var options = require('minimist')(process.argv); 

gulp.task('show-flags', function () { 
  console.log(options); 
}); 
$ gulp show-flags -abc --foo bar --bar=foo --zoo 
# '_' contains unparsed args
> { _: [...],   a: true, 
  b: true, 
  c: true, 
  foo: 'bar', 
  bar: 'foo', 
  zoo: true }

Usage of flag options is up to you to define your tasks. It's now time to substitute your old:

$ gulp build-dev with gulp build --env=dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment