21

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":"*"
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user2386872
  • 345
  • 1
  • 2
  • 15
  • Possible duplicate of [Grunt Command Line Parameters](http://stackoverflow.com/questions/20127586/grunt-command-line-parameters) – Claudiu Feb 17 '16 at 21:27

2 Answers2

39

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.

go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • 3
    Not sure why I had such a hard time finding this on the grunt documentation. Thanks so much! – mmmeff Jul 11 '13 at 17:50
3

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

Community
  • 1
  • 1
Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
  • 2
    This plugin has been deprecated and is no longer maintained. Grunt has corrected the issues which made this plugin useful. – PeterM Jun 29 '16 at 07:57