Roy Truelove
Roy Truelove

Reputation: 22466

gruntjs - command line arguments

Where can I get a handle for command line arguments?

eg grunt dist --env=UAT. How do I get the value for env?

While I'm at it, how would I assign a default value to this if it's not set on the command line?

Upvotes: 17

Views: 6235

Answers (2)

WickyNilliams
WickyNilliams

Reputation: 5308

I find the handling of defaults in grunt to be sorely lacking. The method outlined above works, but it quickly gets tiresome when you have lots of options.

A little helper function can ease this:

function defaultOptions(options) {
  for(var key in options) {
    if(options.hasOwnProperty(key) && !grunt.option(key)) {
      grunt.option(key, options[key]);
    }
  }
}

You can then use like:

defaultOptions({
  env : "staging"
});

And at the CLI:

grunt // { env : "staging" }
grunt --env=UAT // { env : "UAT" }

Upvotes: 0

Kyle Robinson Young
Kyle Robinson Young

Reputation: 13762

You can use grunt.option() or more specifically:

var env = grunt.option('env') || 'default';

to grab the env argument or default to the string 'default' if the argument is not present.

Upvotes: 36

Related Questions