25

I have a Grunt build file. My build file has a task that looks like the following:

myTask: {
  options: {
    configFile: "config/default.js",
    args: {   }
  },
  dev: {
    configFile: 'config/local.js',
    options: { args: {} },
  },
  test: {
    configFile: 'config/remote.js',
    options: { args: {} }
  }
}

...

grunt.registerTask('customTask', ['myTask:dev']);
grunt.registerTask('customTask-Test', ['myTask:test']);

Currently, I can run the following from the command line:

> grunt customTask

Everything works fine. However, I need to add the ability to do something like this:

> grunt customTask --myParam=myValue

I need to look at the value of myParam in my "dev" task target. However, I can't figure out how to do it. I would be happy if I could just print out the value of myParam when myTask:dev is ran. In other words, I'd like to see the following when run

> grunt customTask

> grunt customTask --myParam=hello
You entered hello

> grunt customTask-Test

> grunt customTask-Test --myParam=hello

How do I do something like this?

SL Dev
  • 855
  • 3
  • 11
  • 13

4 Answers4

39

This is all explained in the grunt.option page.

In your case, you could get the value of myParam with:

var target = grunt.option('myParam');
Razor
  • 27,418
  • 8
  • 53
  • 76
badsyntax
  • 9,394
  • 3
  • 49
  • 67
  • 2
    For default arguments you can use the standard JavaScript idiom: `grunt.option('myParam') || 'defaultValue'`. – Claudiu Feb 17 '16 at 21:31
7

I made an example of use, where i can pass the module where i want my css.min to be created through this command line:

> grunt cssmin --target=my_module 

Gruntfile.js

module.exports = function(grunt) {    
 var module = grunt.option('target'); //get value of target, my_module
 var cssminPath = 'assets/' + module + '/css/all.css';

 grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    cssmin:{
        css: {
            files: [{
                    src: [
                        'bower_components/bootstrap/dist/css/bootstrap.min.css',
                    ],
                    dest: cssminPath
                }]
        }
    },
  });
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.registerTask('default', ['cssmin']);
}
Ricardo
  • 81
  • 1
  • 2
4

An alternate way: you can use the process.argv array, just like you can in a regular Node app.

GruntJS is, of course, built on NodeJS.

I used this technique in order to forward my Grunt command-line args to my Node process, called by grunt-nodemon.

Jonathan
  • 32,202
  • 38
  • 137
  • 208
2

You can also use the process.argv array te read the command line args from grunt

var args = process.argv;
runScript(args[2], args[3]);

the first & second arguments are the node command and the script name.

execute: {
    target: {
      options: {
         args : [arg1, arg2]
      },
      src: ['script.js']
    }
}

using grunt-execute

Thami Bouchnafa
  • 1,987
  • 1
  • 15
  • 21
  • This was a great way for me to have my watch task in `initConfig` be different depending upon which task was being called. I wanted to set a variable to pass into my `watch` task based on what the task was, but before running `grunt.registerTask`, there is nothing in `grunt.task.current.name`. Thanks for posting this! – Groovetrain Mar 29 '17 at 20:43
  • You're welcome, i'm glad to have helped someone anyway with this comment – Thami Bouchnafa Apr 05 '17 at 11:48