node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »


nopt-grunt Build Status

Allows additional command line options to be properly registered using nopt along with those natively supported by grunt.


$ npm install nopt-grunt


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.

// Gruntfile.js -> grunt --foo 
module.exports = function(grunt) {
    assert.strictEqual(grunt.option('foo'), true); // pass 
    grunt.registerTask('default', []);
// Gruntfile.js -> grunt --bar=42 
module.exports = function(grunt) {
    assert.strictEqual(grunt.option('bar'), 42);   // pass 
    grunt.registerTask('default', []);
// Gruntfile.js -> grunt --foo --bar=42 
module.exports = function(grunt) {
    assert.strictEqual(grunt.option('foo'), true); // fail -> foo === '--bar=42' 
    assert.strictEqual(grunt.option('bar'), 42);   // fail -> bar === undefined 
    grunt.registerTask('default', []);

Not helpful. Back to the documentation! Still not helpful. Enter nopt-grunt.

// Gruntfile.js -> grunt --foo --bar=42 
module.exports = function(grunt) {
    // require it at the top and pass in the grunt instance 
    // new method now available 
        // longhand 
        foo: {
            info: 'Some flag of interest.',
            type: Boolean
        // shorthand 
        bar: [Number, null]
    assert.strictEqual(grunt.option('foo'), true); // pass! 
    assert.strictEqual(grunt.option('bar'), 42);   // pass! 
    // reference values as before 
    grunt.registerTask('default', []);


MIT alpha