DEPRECATION NOTICE: This project is no longer maintained. Grunt has corrected the issues which made this plugin useful.
Allows additional command line options to be properly registered using nopt along with those natively supported by grunt.
With Node.js
$ 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
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', []);
};
This plugin is a workaround for a to-do on the Grunt team's list. While it allows you to use the api as expected, options will not be displayed when using grunt --help
. If you'd like to see this sort of functionality natively supported, go add your vote to my related Github issue.
MIT