Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can specify formal arguments in rake by adding symbol arguments to the task call. For example:</p> <pre><code>require 'rake' task :my_task, [:arg1, :arg2] do |t, args| puts "Args were: #{args}" end task :invoke_my_task do Rake.application.invoke_task("my_task[1, 2]") end # or if you prefer this syntax... task :invoke_my_task_2 do Rake::Task[:my_task].invoke(3, 4) end # a task with prerequisites passes its # arguments to it prerequisites task :with_prerequisite, [:arg1, :arg2] =&gt; :my_task #&lt;- name of prerequisite task # to specify default values, # we take advantage of args being a Rake::TaskArguments object task :with_defaults, :arg1, :arg2 do |t, args| args.with_defaults(:arg1 =&gt; :default_1, :arg2 =&gt; :default_2) puts "Args with defaults were: #{args}" end </code></pre> <p>Then, from the command line:</p> <pre> > rake my_task[1,2] Args were: {:arg1=>"1", :arg2=>"2"} > rake "my_task[1, 2]" Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task_2 Args were: {:arg1=>3, :arg2=>4} > rake with_prerequisite[5,6] Args were: {:arg1=>"5", :arg2=>"6"} > rake with_defaults Args with defaults were: {:arg1=>:default_1, :arg2=>:default_2} > rake with_defaults['x','y'] Args with defaults were: {:arg1=>"x", :arg2=>"y"} </pre> <p>As demonstrated in the second example, if you want to use spaces, the quotes around the target name are necessary to keep the shell from splitting up the arguments at the space.</p> <p>Looking at the code in <strong>rake.rb</strong>, it appears that rake does not parse task strings to extract arguments for prerequisites, so you can't do <code>task :t1 =&gt; "dep[1,2]"</code>. The only way to specify different arguments for a prerequisite would be to invoke it explicitly within the dependent task action, as in <code>:invoke_my_task</code> and <code>:invoke_my_task_2</code>.</p> <p>Note that some shells (like zsh) require you to escape the brackets: <code>rake my_task\['arg1'\]</code></p>
 

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