gruntjs - command line arguments

◇◆丶佛笑我妖孽 提交于 2019-12-03 11:25:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!