Grunt - Command Line Arguments, not working

白昼怎懂夜的黑 提交于 2020-01-09 09:33:33

问题


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":"*"
    }
}

回答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.




回答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



来源:https://stackoverflow.com/questions/17012102/grunt-command-line-arguments-not-working

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