鍍金池/ 教程/ 物聯(lián)網(wǎng)/ grunt.option
深入任務(wù)內(nèi)幕
grunt.option
退出碼
創(chuàng)建插件
grunt.file
grunt
快速入門
項(xiàng)目腳手架
使用命令行工具
Gruntfile 實(shí)例
配置任務(wù)
創(chuàng)建任務(wù)
grunt.log
安裝Grunt
grunt.util
grunt.event
常見問題
grunt.config
grunt.task
grunt.template
grunt.fail

grunt.option

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

grunt.option ?

獲取或者設(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.init

初始化grunt.option設(shè)置。如果忽略參數(shù)中的initObject選項(xiàng)就會初始化為一個空對象,否則設(shè)置為指定的initObject。

grunt.option.init([initObject])

grunt.options.flags

以數(shù)組的形式選為命令行參數(shù)選項(xiàng)。

grunt.option.flags()    
上一篇:grunt.log下一篇:常見問題