May 25, 2021 Grunt
Grunt's option API is used to share parameters between multiple tasks and access parameters set on the command line.
A simple case is to specify a flag for a target that distinguishes between a development period or a transition period.
On the command line:
grunt deploy --target=staging
will return
grunt.option('target')
"staging"
The following
Gruntfile
how to use
target
option:
grunt.initConfig({
compass: {
dev: {
options: {
/* ... */
outputStyle: 'expanded'
},
},
staging: {
options: {
/* ... */
outputStyle: 'compressed'
},
},
},
});
var target = grunt.option('target') || 'dev';
grunt.registerTask('deploy', ['compass:' + target]);
When you
grunt deploy
your style sheet defaults to
dev
and outputs easy-to-read CSS format code.
If you run
grunt deploy --target=staging
staging
is executed and the CSS is output after compression.
grunt.option
be used in task, as follows:
grunt.registerTask('upload', 'Upload code to specified target.', function(n) {
var target = grunt.option('target');
// do something useful with target here
});
grunt.registerTask('deploy', ['validate', 'upload']);
Note that the boolean parameter can specify only keys and omit the value.
For example, executing
grunt deploy --staging
command line will return
grunt.option('staging')
true
Get or set an option.
grunt.option(key[, val])
The option of the boolean type can
key
by
no-
before key.
Here's the case:
grunt.option('staging', false);
var isDev = grunt.option('no-staging');
// isDev === true
Initialize
grunt.option
If
initObject
option is initialized as an empty object, otherwise it is
initObject
grunt.option.init([initObject])
Return all arguments as an array of command line arguments.
grunt.option.flags()