问题
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?
回答1:
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.
回答2:
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" }
来源:https://stackoverflow.com/questions/13351932/gruntjs-command-line-arguments