LoginSignup
25
25

More than 5 years have passed since last update.

Gruntfile 内で引数やオプションを受け取る方法

Posted at

引数

  • 引数は grunt yourtask:yourchildtask:引数0:引数1 という風に指定する、指定していないサブタスクは自動的に引数として処理される
  • これが grunt.task.current.args 内に入っている

例えば、以下のタスクの場合:

grunt.initConfig({
  yourtask: {
    options: {
      'youropt': '<%= grunt.task.current.args[0] %>'
    }
  }
});

grunt yourtask:unk を実行すると "unk"youropt に指定される。

オプション

  • オプションは grunt.cli.options 内に入っている
  • options は Object で、オプションだけならその指定したキーに true が、値も入れたらそれが入る

例えば、以下のタスクの場合:

grunt.initConfig({
  yourtask: {
    options: {
      'youropt': '<%= grunt.task.cli.unk ? grunt.task.cli.unk : "" %>'
    }
  }
});

grunt yourtask --unk cnk なら youropt="cnk"、オプション無しなら youropt="" になる。

うまく動かんーよくわかんねー って人は

とりあえず、grunt をテンプレート変数内でダンプしてみそらしど!

<%= console.log(grunt); %>

25
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
25