node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

nopt-grunt

nopt-grunt Build Status

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

Install

$ npm install nopt-grunt

Example

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 
    require('nopt-grunt')(grunt);
 
    // new method now available 
    grunt.initOptions({
        // 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.option('no-foo');
 
    grunt.initConfig({});
    grunt.registerTask('default', []);
};

License

MIT

githalytics.com alpha