Programmatically set options for grunt task?

I have a grunt task that looks at options with grunt.option('foo'). If I’m calling this task from'my-task'), how can I change those arguments?

I’m looking for something like:

  • Unit testing javascript code style
  • Manage AngularJS modules version and releases using Grunt
  • How to run single e2e test with grunt and protractor
  • Working project structure that uses grunt.js to combine JavaScript files using RequireJS?
  • Should I use angular-seed or yeoman for a new Angular application?
  • how to include ngAnimate dependency in angular with yeoman
  •'my-task', {foo: 'bar'});

    which would be the equivalent of:

    $ grunt my-task --foo 'bar'

    Is this possible?

    (This question is another issue I ran in to but is not exactly the same, because in this case I don’t have access to the original task’s Gruntfile.js.)

  • package javascript app (that uses grunt & browserify) in war
  • How can I run a script in node_modules from a grunt task?
  • AngularJS+Typescript - Controller inside a directive
  • How can a grunt plugin wrap another plugin
  • How can I make 'grunt less' automatically run autoprefixer?
  • How to create a grunt task that references other grunt tasks
  • 6 Solutions collect form web for “Programmatically set options for grunt task?”

    If you can use task-based config options instead of grunt.option, this should work to give you more granular control:

    grunt.config.set('', 'bar');

    Looks like I can use the following:

    grunt.option('foo', 'bar');'my-task');

    It feels a bit odd to set the options globally instead of just for that command, but it works.

    Create a new task which set the option, then call the modified task. This is a real life example with assemble:

    grunt.registerTask('build_prod', 'Build with production options', function () {
      grunt.config.set('assemble.options.production', true);'build');

    In addition to @Alessandro Pezzato


    grunt.registerTask('build', ['clean:dist', 'assemble', 'compass:dist', 'cssmin', 'copy:main']);
        grunt.registerTask('build-prod', 'Build with production options', function () {
            grunt.config.set('assemble.options.production', true);
        grunt.registerTask('build-live', 'Build with production options', function () {
            grunt.option('assemble.options.production', false);

    Now you can run

    $ grunt build-prod


    $ grunt build-live

    They will both do the full task ‘build’ and respectively pass a value to one of the options of assemble, namely production ‘true’ or ‘false’.

    In addition to illustrate the assemble example a bit more:

    In assemble you have the option to add a {{#if production}}do this on production{{else}}do this not non production{{/if}}

    I recently ran up against this same issue: programmatically setting grunt options and running tasks multiple times from within a single parent task. As @Raphael Verger mentions, this is not possible, as defers the running of the task until the current task is finished:

    grunt.option('color', 'red');['logColor']);
    grunt.option('color', 'blue');['logColor']);

    Will result in the color blue being logged twice.

    After some fiddling, I came up with a grunt task that allows dynamically specifying a different option/config for each sub-task to be run. I’ve published the task as grunt-galvanize. Here’s how it works:

    var galvanizeConfig = [
      {options: {color: 'red'}, configs: {}},
      {options: {color: 'blue'}, configs: {}}
    grunt.option('galvanizeConfig', galvanizeConfig);['galvanize:log']);

    This will log red then blue, as desired by running the log task with each of the options/configs specified in galvanizeConfig.

    grunt is all programmatic.. so if you have set options on tasks before, you have done this programmatically.

    just use grunt.initConfig({ ... }) to set options for tasks.

    and if you already initialized, and need to change configuration afterwards, you can do something like = {};

    I am using it for my project and it works.