Tìm thấy ví dụ này trên blog post này và cú pháp có vẻ hơi bụi.
Ví dụ, nếu bạn đã có một nhiệm vụ say_hello
, bạn có thể gọi nó với bất kỳ số lượng các đối số, như vậy:
$ rake say_hello Earth Mars Venus
Đây là cách hoạt động:
task :say_hello do
# ARGV contains the name of the rake task and all of the arguments.
# Remove/shift the first element, i.e. the task name.
ARGV.shift
# Use the arguments
puts 'Hello arguments:', ARGV
# By default, rake considers each 'argument' to be the name of an actual task.
# It will try to invoke each one as a task. By dynamically defining a dummy
# task for every argument, we can prevent an exception from being thrown
# when rake inevitably doesn't find a defined task with that name.
ARGV.each do |arg|
task arg.to_sym do ; end
end
end
tôi không thể vượt qua tham số cách này ?? rake test_rake_task [par1, par2, par3] và truy cập nó ?? –
Tôi đã mô tả cách duy nhất tôi biết để chuyển các tham số cho tác vụ rake. Nó đường nối không thể thực hiện nhiệm vụ rake với một cái gì đó như 'rake test_rake_task [par1, par2, par3]'. – lest
Nó không phải là không thể thực hiện nhiệm vụ rake như vậy, câu trả lời của Henry dưới đây hoạt động để làm điều đó. –