Tasks with Arguments -- Rakefile
1. Arguments and Default Values
下面的程式碼,主要是在task中加入參數跟預設值
說實在的還滿好懂的,我就不細講了 :P
task :name, [:first_name, :last_name] do |t, args|
args.with_defaults(:first_name => "Carter", :last_name => "Tsai")
puts t
puts "First Name is #{args.first_name}"
puts "Last Name is #{args.last_name}"
end
* 沒有參數就會帶入預設值
$> rake name
name
First Name is Carter
Last Name is Tsai
* 有參數的時候
$> rake name[A,B]
name
First Name is A
Last Name is B
2. Prerequisites
這部份的話就是加入 => [:pre_name],就是說在執行這個任務之前你要先做的事(就是先去執行:pre_name這個任務)
task :pre_name do
puts "test..."
end
task :name, [:first_name, :last_name] => [:pre_name] do |t, args|
args.with_defaults(:first_name => "Carter", :last_name => "Tsai")
puts t
puts "First Name is #{args.first_name}"
puts "Last Name is #{args.last_name}"
end
$> rake name[Carter, Tsai]
test...
name
First Name is Carter
Last Name is Tsai
留言