How to pass command line arguments to a rake task

392,353

Solution 1

Options and dependencies need to be inside arrays:

namespace :thing do
  desc "it does a thing"
  task :work, [:option, :foo, :bar] do |task, args|
    puts "work", args
  end
  
  task :another, [:option, :foo, :bar] do |task, args|
    puts "another #{args}"
    Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])
    # or splat the args
    # Rake::Task["thing:work"].invoke(*args)
  end

end

Then

rake thing:work[1,2,3]
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}

rake thing:another[1,2,3]
=> another {:option=>"1", :foo=>"2", :bar=>"3"}
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}

NOTE: variable task is the task object, not very helpful unless you know/care about Rake internals.

RAILS NOTE:

If running the task from Rails, it's best to preload the environment by adding => [:environment] which is a way to setup dependent tasks.

  task :work, [:option, :foo, :bar] => [:environment] do |task, args|
    puts "work", args
  end

Solution 2

You can specify formal arguments in rake by adding symbol arguments to the task call. For example:

require 'rake'

task :my_task, [:arg1, :arg2] do |t, args|
  puts "Args were: #{args} of class #{args.class}"
  puts "arg1 was: '#{args[:arg1]}' of class #{args[:arg1].class}"
  puts "arg2 was: '#{args[:arg2]}' of class #{args[:arg2].class}"
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] => :my_task #<- 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 => :default_1, :arg2 => :default_2)
  puts "Args with defaults were: #{args}"
end

Then, from the command line:

> rake my_task[1,false]
Args were: {:arg1=>"1", :arg2=>"false"} of class Rake::TaskArguments
arg1 was: '1' of class String
arg2 was: 'false' of class String

> 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"}

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.

Looking at the code in rake.rb, it appears that rake does not parse task strings to extract arguments for prerequisites, so you can't do task :t1 => "dep[1,2]". The only way to specify different arguments for a prerequisite would be to invoke it explicitly within the dependent task action, as in :invoke_my_task and :invoke_my_task_2.

Note that some shells (like zsh) require you to escape the brackets: rake my_task\['arg1'\]

Solution 3

In addition to answer by kch (I didn't find how to leave a comment to that, sorry):

You don't have to specify variables as ENV variables before the rake command. You can just set them as usual command line parameters like that:

rake mytask var=foo

and access those from your rake file as ENV variables like such:

p ENV['var'] # => "foo"

Solution 4

If you want to pass named arguments (e.g. with standard OptionParser) you could use something like this:

$ rake user:create -- --user [email protected] --pass 123

note the --, that's necessary for bypassing standard Rake arguments. Should work with Rake 0.9.x, <= 10.3.x.

Newer Rake has changed its parsing of --, and now you have to make sure it's not passed to the OptionParser#parse method, for example with parser.parse!(ARGV[2..-1])

require 'rake'
require 'optparse'
# Rake task for creating an account

namespace :user do |args|
  desc 'Creates user account with given credentials: rake user:create'
  # environment is required to have access to Rails models
  task :create do
    options = {}
    OptionParser.new(args) do |opts|
      opts.banner = "Usage: rake user:create [options]"
      opts.on("-u", "--user {username}","User's email address", String) do |user|
        options[:user] = user
      end
      opts.on("-p", "--pass {password}","User's password", String) do |pass|
        options[:pass] = pass
      end
    end.parse!

    puts "creating user account..."
    u = Hash.new
    u[:email] = options[:user]
    u[:password] = options[:pass]
    # with some DB layer like ActiveRecord:
    # user = User.new(u); user.save!
    puts "user: " + u.to_s
    puts "account created."
    exit 0
  end
end

exit at the end will make sure that the extra arguments won't be interpreted as Rake task.

Also the shortcut for arguments should work:

 rake user:create -- -u [email protected] -p 123

When rake scripts look like this, maybe it's time to look for another tool that would allow this just out of box.

Solution 5

I've found the answer from these two websites: Net Maniac and Aimred.

You need to have version > 0.8 of rake to use this technique

The normal rake task description is this:

desc 'Task Description'
task :task_name => [:depends_on_taskA, :depends_on_taskB] do
  #interesting things
end

To pass arguments, do three things:

  1. Add the argument names after the task name, separated by commas.
  2. Put the dependencies at the end using :needs => [...]
  3. Place |t, args| after the do. (t is the object for this task)

To access the arguments in the script, use args.arg_name

desc 'Takes arguments task'
task :task_name, :display_value, :display_times, :needs => [:depends_on_taskA, :depends_on_taskB] do |t, args|
  args.display_times.to_i.times do
    puts args.display_value
  end
end

To call this task from the command line, pass it the arguments in []s

rake task_name['Hello',4]

will output

Hello
Hello
Hello
Hello

and if you want to call this task from another task, and pass it arguments, use invoke

task :caller do
  puts 'In Caller'
  Rake::Task[:task_name].invoke('hi',2)
end

then the command

rake caller

will output

In Caller
hi
hi

I haven't found a way to pass arguments as part of a dependency, as the following code breaks:

task :caller => :task_name['hi',2]' do
   puts 'In Caller'
end
Share:
392,353
Tilendor
Author by

Tilendor

I'm a programmer &amp; Geek

Updated on August 06, 2022

Comments

  • Tilendor
    Tilendor almost 2 years

    I have a rake task that needs to insert a value into multiple databases.

    I'd like to pass this value into the rake task from the command line, or from another rake task.

    How can I do this?

    • Brian Maltzan
      Brian Maltzan about 13 years
    • Jonathan Allard
      Jonathan Allard over 9 years
      Docs have been mirrored by SeattleRb.
  • Tilendor
    Tilendor about 15 years
    This doesn't tell me how to run the rake task with arguments from another task. It covers only command line usage
  • gaqzi
    gaqzi almost 15 years
    To invoke a task within a namespace simpy do: Rake::Task['namespace:task'].invoke
  • JasonSmith
    JasonSmith almost 14 years
    Frankly I was hoping for rake task -- these --go --to -a program and my task could get them from ARGV. Unfortunately I'm not sure if that's possible however I am currently using your solution: rake var1=val1 var2=val2
  • opsb
    opsb over 13 years
    Actually amazed, I've looked for the answer to this so many times and it's always been rake task arg1=2 arg2=3. This is much simpler when the arguments are in series.
  • Rob
    Rob over 13 years
    Thanks, I particularly needed to pass arguments to prerequisite task, your examples work perfectly.
  • mu is too short
    mu is too short over 13 years
    @jhs: rake blah -- --these --go --to --a-program (note the -- to tell rake that its switches have ended), see stackoverflow.com/questions/5086224/…
  • inger
    inger over 13 years
    @Rob, @Nick: "particularly needed to pass arguments to prerequisite task". I can't see an example explicitly passing parameters to prereq task.. Did I miss something? Is there a way to do this, rather than invoking?
  • igorsantos07
    igorsantos07 almost 13 years
    Is there a way to call a task more than one time in a row? I tried 5.times { Rake::Task[:my_task].invoke } and it only worked for the first time.
  • Nick Desjardins
    Nick Desjardins almost 13 years
    That's a separate question, Igoru, but the reason your call to invoke only runs once is that rake is dependency-oriented, so it will only execute a task if it is needed. For generic tasks that means if it hasn't already run. To explicitly execute a task regardless of its dependencies or if it is needed, call execute instead of invoke.
  • madh
    madh over 12 years
    The format for this functionality has changed as this warning states: 'task :t, arg, :needs => [deps]' is deprecated. Please use 'task :t, [args] => [deps]' instead.
  • Ajedi32
    Ajedi32 almost 12 years
    Note: According to rake, this syntax for accepting variables in tasks is deprecated: WARNING: 'task :t, arg, :needs => [deps]' is deprecated. Please use 'task :t, [args] => [deps]' instead.
  • Juanda
    Juanda over 11 years
    To prevent frozen strings issues, use dup at the end: db = ARGV[1].dup
  • Seth Bro
    Seth Bro almost 11 years
    Note that zsh fails to parse the command line arguments correctly (zsh: no matches found: ...), so you need to escape the brackets: rake my_task\['arg1'\]. From robots.thoughtbot.com/post/18129303042/…
  • Joe
    Joe over 10 years
    From my perspective this really is the best answer. Bypass environment variable kludges, strange syntax with task arguments, the additional benefit for standard --option-names. My only suggestion would be to use exit rather than abort as abort will leave you with a return code of 1 to the shell. If the rake task is a part of a higher-level script it's more common to assume a non-zero exit is some type of error.
  • GMA
    GMA over 10 years
    @SethBro YES. If only your comment hadn't been hidden behind the "See more comments" link I wouldn't have wasted 10 minutes unable to make this work.
  • Rik Smith-Unna
    Rik Smith-Unna over 10 years
    I agree with Joe, this is the best answer. The natural thing is to use the same interface for passing options to rake as you would when passing options to a script.
  • Augustin Riedinger
    Augustin Riedinger over 10 years
    I agree this is the best answer. Ain't there a way to bypass the ugly --? Like passing rake arguments to the actual task or something? Like task :my_task, :*args do |t, args| or something?
  • Augustin Riedinger
    Augustin Riedinger over 10 years
    Besides, I don't understand what the {username} is here for. Where is it used? Why isn't it there in -u {username}? Cheers
  • Tombart
    Tombart over 10 years
    As far as I know there's no way how to bypass ugly -- (just not using rake). {username} should be replaced by your real username. It's just a notation for variable, it's not interpreted by ruby.
  • Dex
    Dex about 10 years
    To call this, go: rake task_name[hello, world]
  • Gayle
    Gayle about 10 years
    from rake.rubyforge.org/files/doc/rakefile_rdoc.html "Just a few words of caution. The rake task name and its arguments need to be a single command line argument to rake. This generally means no spaces. If spaces are needed, then the entire rake + argument string should be quoted. Something like this: rake "name[billy bob, smith]" "
  • Justin Tanner
    Justin Tanner over 9 years
    exit("account created.") is causing an error rake aborted! wouldn't puts "account created"; exit(0) be a better approach?
  • pawel7318
    pawel7318 over 9 years
    It worked for me after I ARGV.shift twice to remove create:user and -- from ARGV. I'm sure it's not the way it should be done. I will appreciate if someone will show how to fix it properly.
  • Tombart
    Tombart over 9 years
    @pawel7318 Which version of Rake do you use? Rake doesn't seem to follow semantic versioning conventions, there is a compatibility breaking change since 10.4.0
  • Tombart
    Tombart over 9 years
    The way how Rake parses ARGV was changed in 10.4.1 and reverted in 10.4.2. github.com/ruby/rake/commit/…
  • pawel7318
    pawel7318 over 9 years
    I'm using 10.4.2. I created new question here.
  • ZiggyTheHamster
    ZiggyTheHamster almost 9 years
    If you need a JSON instructions file for your Rake task, you're probably doing too many things in your Rake task.
  • jeffdill2
    jeffdill2 over 8 years
    This is way over-complicating something that's incredibly simple.
  • Chandrashekhar Singh
    Chandrashekhar Singh over 8 years
    Add alias rake='noglob rake' in your .zshrc and forget escaping the brackets.
  • Andre Figueiredo
    Andre Figueiredo about 8 years
    Event better db = ARGV[1].dup unless ARGV[1].nil? to prevent exception of duping a nil.
  • res
    res almost 8 years
    valid syntax for current rails (5) is: task :task_name, [:var1, :var2] => :environment do |t, vars|. Inside task vars looks like: {:var1 => val, :var2 => val}
  • fatty
    fatty almost 8 years
    Needed to make this _, arg1, arg2 = ARGV as the first arg was seen to be the name of the rake task. But that exit is a neat trick.
  • rpbaltazar
    rpbaltazar almost 8 years
    Also, make sure you don't use spaces between the arguments. E.g don't do this: rake thing:work[1, 2, 3] as it won't work and you'll get an error Don't know how to build task
  • Nuclearman
    Nuclearman over 7 years
    rake task[arg1,arg2] && rake task2 && rake task3 Not sure if that's less ugly than rake task[arg1,arg2] task2 task3. Probably less efficient though.
  • theterminalguy
    theterminalguy over 7 years
    Also, make sure you enclose the argument in string. e.g from your command line run the rake task like so rake thing:work'[1,2,3]'
  • Blair Anderson
    Blair Anderson over 7 years
    @DamianSimonPeter you do not need to use strings. can simply do rake thing:workd[true,false,cheese] the values will be strings!
  • valheru
    valheru over 7 years
    What about when you're using bundle exec rake?
  • William Entriken
    William Entriken over 7 years
    For completeness: a task cannot explicitly specify (or override) values for its dependent task's arguments.
  • hutusi
    hutusi almost 7 years
    Unfortuanely, zsh can not parse the call correctly, you need type the command on zsh like this: rake thing:work\[1,2,3\], or this rake 'thing:work[1,2,3]'
  • sakurashinken
    sakurashinken about 6 years
    This failed for me with the error Don't know how to build task 'environment' (see --tasks) Nick Desjardins answer worked great.
  • Blair Anderson
    Blair Anderson about 6 years
    @sakurashinken you can remove the :environment symbol from your task. rails applications have an :environment task...
  • Joshua Pinter
    Joshua Pinter about 6 years
    Your code needs a few well-placed empty lines. I don't know how you read that wall of text.
  • Joshua Pinter
    Joshua Pinter about 6 years
    Instead of having a note to explain that t means task, why not just use task as the param name?
  • Joshua Pinter
    Joshua Pinter about 6 years
    NOTE: Do not add a space between arguments. Use rake my_task[1,2] instead of rake my_task[1, 2]. Otherwise you get the dreaded Don't know how to build task 'my_task[1,' error and you'll be scratching your head for longer than you'd like to admit.
  • Joshua Pinter
    Joshua Pinter about 6 years
    @BlairAnderson Explicitness. I love it! :-)
  • XtraSimplicity
    XtraSimplicity over 5 years
    _, *args = ARGV is perfect for capturing all subsequent arguments! Thanks heaps!
  • user2490003
    user2490003 about 5 years
    Rake tasks seem to have an almost nonsensical layout for name, dependencies, and arguments. The conclusion - while it works - is not something you could have arrived at intuitively.
  • stevec
    stevec about 5 years
    This is the best simplest answer IMO. It worked right away. What exactly does the p mean?
  • kqcef
    kqcef about 5 years
    @user5783745 Like puts but instead of logging value.to_s to standard out it calls Obj.inspect and logs that to standard out. ruby-doc.org/core-2.0.0/Kernel.html#method-i-p
  • user2490003
    user2490003 almost 5 years
    This is such a counter intuitive setup for a task runner / system. It's neither easily readable or easily writeable
  • Damien Roche
    Damien Roche over 4 years
    And override environment variables? Fantastic!
  • lzap
    lzap over 4 years
    Rake is utterly overengineered mess and this is the only way which worked. And it's not just me, this answer has the same amount of votes as the "correct" answer.
  • Olivier JM
    Olivier JM about 4 years
    Thanks for this, great solution while maintaining the :environment
  • tundervirld
    tundervirld over 3 years
    We were using a rake task to do many complex things like a task. One of them was to be the input to an ETL process, and you could need many input fields to do it.We were using a rake task to do many complex things like a task. One of them was to be the input to an ETL process, and you could need many input fields to do it. If you are thinking that a Rake Task is for easiest thing only, maybe you aren't using in other complex context. Thanks for commenting.
  • vlsd
    vlsd about 3 years
    for your last example with defaults, is that a recursive call to with_defaults or does the task name and the method name just happen to the be same?
  • x-yuri
    x-yuri about 3 years
    What's the point of escaping x and y in rake with_defaults['x','y']? What rake gets is with_defaults[x,y] anyway.
  • karatedog
    karatedog almost 3 years
    @muistooshort unfortunately (not knowing how it worked back in '11) this will try to run all the arguments passed as if they were tasks. One of the half ugly solution is to create empty tasks based on ARGV,content so these task will indeed be run, they just won't do anything, the second is to exit at the end of the task. Exiting is the easier, but that will break any compound task that try to run the exiting task along others as exit will halt task execution and exit Rake.
  • mu is too short
    mu is too short almost 3 years
    @karatedog Are you sure about that? I just tried it to make sure and it seems okay, am I missing something?
  • karatedog
    karatedog almost 3 years
    @muistooshort Right, passing arguments with double dash works. I cannot correct the previous comment, the error was on passing linux style command line arguments like: --switch1 value1 --switch2 value2.
  • karatedog
    karatedog almost 3 years
    the side effect of using exit 0 is this task exits Rake when it is finished. So if you have a task that calls 2 tasks, and it calls the above first, the second task won't run.
  • Duarte
    Duarte over 2 years
    I get an error when trying this: rake aborted! Don't know how to build task 'hello world'
  • guero64
    guero64 about 2 years
    Actually, I think single quote goes after rake; i.e. rake 'namespace1:task1["1","2","3"]'. Otherwise thank you for helpful answer.