Grunt - Command Line Arguments, not working

15,424

Solution 1

The proper syntax for specifying a command line argument in Grunt is:

grunt --option1=myValue

Then, in the grunt file you can access the value and print it like this:

console.log( grunt.option( "option1" ) );

Also, another reason you are probably having issues with --vers is because its already a grunt option that returns the version:

★  grunt --vers
grunt-cli v0.1.7
grunt v0.4.1

So it would probably be a good idea to switch to a different option name.

Solution 2

It is worth mentioning that as the amount of command line arguments you want to use grows, you will run into collisions with some arguments that grunt uses internally.

I got around this problem with nopt-grunt

From the Plugin author:

Grunt is awesome. Grunt's support for using additional command line options is not awesome. The current documentation is misleading in that they give examples of using boolean flags and options with values, but they don't tell you that it only works that way with a single option. Try and use more than one option and things fall apart quickly.

It's definitely worth checking out

Share:
15,424

Related videos on Youtube

user2386872
Author by

user2386872

Updated on January 10, 2020

Comments

  • user2386872
    user2386872 over 4 years

    I am using command line options in my grunt script: http://kurst.co.uk/transfer/Gruntfile.js

    However the command grunt --vers:0.0.1 always returns 'undefined' when I try to get the option:

    var version = grunt.option('vers') || ''; 
    

    Can you help me get this working ?

    I tried different (CLI) commands:

    grunt vers:asd
    grunt -vers:asd
    grunt vers=asd
    

    as well as using :

    grunt.option('-vers');
    grunt.option('--vers');
    

    But no luck so far. Hopefully I am missing something simple.

    This is my package.js file:

    {
        "name": "",
        "version": "0.1.0",
        "description": "Kurst EventDispatcher / Docs Demo ",
        "devDependencies": {
            "grunt": "~0.4.1",
            "grunt-contrib-yuidoc": "*",
            "grunt-typescript": "~0.1.3",
            "uglify-js": "~2.3.5",
            "grunt-lib-contrib": "~0.6.0",
            "grunt-contrib-uglify":"*"
        }
    }
    
  • mmmeff
    mmmeff almost 11 years
    Not sure why I had such a hard time finding this on the grunt documentation. Thanks so much!
  • PeterM
    PeterM about 8 years
    This plugin has been deprecated and is no longer maintained. Grunt has corrected the issues which made this plugin useful.