Grunt的option API可以用來跨多任務(wù)共享參數(shù),以及在命令行中訪問參數(shù)設(shè)置。
一個簡單的例子就是給你的目標(biāo)(任務(wù)目標(biāo))打一個標(biāo)記,標(biāo)識任務(wù)是在開發(fā)階段還是暫存階段。在命令行中運(yùn)行grunt deploy --target=staging
就會讓grunt.option('target')
返回"staging"
。
下面是一個在Gruntfile
中使用target
選項(xiàng)的例子:
grunt.initConfig({
compass: {
dev: {
options: {
/* ... */
outputStyle: 'expanded'
},
},
staging: {
options: {
/* ... */
outputStyle: 'compressed'
},
},
},
});
var target = grunt.option('target') || 'dev';
grunt.registerTask('deploy', ['compass:' + target]);
當(dāng)你運(yùn)行grunt deploy
時(shí)你的樣式表默認(rèn)情況下為dev
目標(biāo),并且它會以展開的格式輸出CSS代碼。如果你運(yùn)行grunt deploy --target=staging
, staging
目標(biāo)會替代父級的dev
目標(biāo),同時(shí)它會以壓縮格式輸出CSS代碼。
grunt.option
也可以用在任務(wù)中,例如:
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']);
注意:可以只用只有鍵(屬性)而沒有值的方式來指定布爾值選項(xiàng)。例如,在命令行中運(yùn)行grunt deploy --staging
會導(dǎo)致grunt.option('staging')
返回true
。
獲取或者設(shè)置一個選項(xiàng)。
grunt.option(key[, val])
可以通過在key
鍵上使用一個前置的no-
來否定buerhi選項(xiàng)。例如:
grunt.option('staging', false);
var isDev = grunt.option('no-staging');
// isDev === true
初始化grunt.option
設(shè)置。如果忽略參數(shù)中的initObject
選項(xiàng)就會初始化為一個空對象,否則設(shè)置為指定的initObject
。
grunt.option.init([initObject])
以數(shù)組的形式選為命令行參數(shù)選項(xiàng)。
grunt.option.flags()