Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've found the answer from these two websites: <a href="http://nhw.pl/wp/2008/10/11/rake-and-arguments-for-tasks" rel="noreferrer">Net Maniac</a> and <a href="http://www.aimred.com/news/developers/2008/10/16/arguments-for-rake/" rel="noreferrer">Aimred</a>.</p> <p>You need to have version > 0.8 of rake to use this technique</p> <p>The normal rake task description is this:</p> <pre><code>desc 'Task Description' task :task_name =&gt; [:depends_on_taskA, :depends_on_taskB] do #interesting things end </code></pre> <p>To pass arguments, do three things:</p> <ol> <li>Add the argument names after the task name, separated by commas.</li> <li>Put the dependencies at the end using :needs => [...]</li> <li>Place |t, args| after the do. (t is the object for this task)</li> </ol> <p>To access the arguments in the script, use args.arg_name</p> <pre><code>desc 'Takes arguments task' task :task_name, :display_value, :display_times, :needs =&gt; [:depends_on_taskA, :depends_on_taskB] do |t, args| args.display_times.to_i.times do puts args.display_value end end </code></pre> <p>To call this task from the command line, pass it the arguments in []s</p> <pre><code>rake task_name['Hello',4] </code></pre> <p>will output</p> <pre><code>Hello Hello Hello Hello </code></pre> <p>and if you want to call this task from another task, and pass it arguments, use invoke</p> <pre><code>task :caller do puts 'In Caller' Rake::Task[:task_name].invoke('hi',2) end </code></pre> <p>then the command</p> <pre><code>rake caller </code></pre> <p>will output</p> <pre><code>In Caller hi hi </code></pre> <p>I haven't found a way to pass arguments as part of a dependency, as the following code breaks:</p> <pre><code>task :caller =&gt; :task_name['hi',2]' do puts 'In Caller' end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload