How to supply argument to a rake task?

Hi,

This is probably a dumb question, but I've tried all combinations I can think of and still can't get it to work, so I figured I'd swallow my pride and ask it here...

I want to create and run rake tasks from the IDE. I have created and run one successfully,however, I cannot pass an argument to it. Here is my simple task:

namespace :stuptues do
  namespace :db do
    desc 'My simple rake task'
      task :greet, :name do |cmd, args|
        puts "command: #{cmd}"
        puts "args1: #{args[:name]}"
        puts "args2: #{args.inspect}"
     end
  end
end

When I run this from the IDE with no arguments I get the following output, which tells me that the definiiton and plumbing are all okay:

command: stuptues:db:greet
args1:
args2: {}

However, when I try to run it by passing an arguement such as [wtf] or ['wtf'] I get the following:

rake aborted!
Don't know how to build task '[wtf]'

(See full trace by running task with --trace)
command: stuptues:db:greet
args1:
args2: {}

But when I supply an argument to the task from the command line it works fine:

Larry-Desk$ rake stuptues:db:greet[wtf]

command: stuptues:db:greet
args1: wtf
args2: {:name=>"wtf"}

Can someone tell me how to pass arguments to a rake task from within the IDE?

Thanks,
Larry

7 comments
Comment actions Permalink

If you put "rake stuptues:db:greet[wtf]" all on the task name line in the run configuration, it works.

0
Comment actions Permalink

Hi Dara,

Thanks for the reply. I was hoping to be able to fire up the Tools > Rake Tasks > stup_tues > db > greet, and then when the Rake dialog box comes up I would like to put my arguments in the "Arguments" textfield. But it occurs to me that maybe you can't do that from the IDE. I think that the "Arguments" field in the Rake dialog box might be for the rake command itself, not the task I am trying to run. For example, I can enter "--trace" into the "Arguments" field and it works, which means the "--trace" argument is being fed to the actual rake command, not the rake task that I am running.

I hope what I just wrote makes sense,
Larry

0
Comment actions Permalink

I don't know much about the way you're trying to do arguments, I simply found a way to make it work for you.

A simpler way to handle arguments to rake tasks is using environment variables. Here's a simple example:


desc 'My simple rake task'
task :greet do
  puts "Hello, " + ENV['name']
end



from command line:
rake greet name=Joe

in Rubymine, put name=Joe on the arguments line of the run configuration.

Hope that helps.
0
Comment actions Permalink

Hi Dara,

Thanks again for your reply and the tip about environment variables. I don't think I can do what I wanted, which is to fire up a rake task like I normally do in the IDE (using the Tools > Run Rake Task dialog box) and then entering my argument(s) in the "Arguments" text field. Your solution requires popping out to the command line, and I was looking for a way to stay within the IDE.

Thanks,
Larry

0
Comment actions Permalink

You can use environment variables in the IDE. As I said for the example I gave above:

"in Rubymine, put name=Joe on the arguments line of the run configuration."

0
Comment actions Permalink

At least as of 2018, you can pass "traditional" Rake arguments by putting them in brackets in the Arguments field of the run configuration. For instance, the following task takes up to two arguments, :calltree and :html:

task :profile, [:calltree, :html] do |_, args|
arg_hash = args.to_h
ENV['PROFILE'] = arg_hash.inspect unless arg_hash.empty?
Rake::Task[:spec].invoke
end

In the Run/Debug configuration, I can specify these as a comma-delimited list:

This will result in arg_hash containing {calltree: "1", html: "2"}. (The actual console command is rake profile[1,2]). If I omit an argument:


I get just {calltree: "1"}. (The actual console command is rake profile[1]). If I include a leading comma:

I get just {html: "1"}. (The actual console command is rake profile[,1]).

0
Comment actions Permalink

Note also that other arguments can get passed after the bracketed ones, e.g. if I want to run just a single spec I can set SPEC= and have it get passed to the wrapped :spec task:

translates to rake profile[,1] SPEC=spec/libs/error_mixin_spec.rb

0

Please sign in to leave a comment.