7

I see that it is possible to pass arguments to a rake task:

task :task_name, :arg_name do |t, args|

What I'd like to do is pass arguments into a cucumber rake task:

Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
  t.cucumber_opts = ['--tags', #args?]
end

Is this sort of thing possible? This way I could do:

rake cucumber:tags['tag1', 'tag2', ...]

And have it run only those tags. Most sources say to use an environment variable, which I have done, but I'd prefer to just provide the arguments the "right" way.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • 1
    this is a duplicate of: http://stackoverflow.com/questions/825748/how-do-i-pass-command-line-arguments-to-a-rake-task – Luke Cowell Nov 17 '12 at 16:49
  • 3
    No it isn't. This question is specific to Cucumber, you cannot add arguments to Cucumber rake tasks like you can in the answer to that question. – Logan Serman Nov 17 '12 at 22:06
  • Gotcha. Would it be possible to write your own rake tasks that wrap the cucumber ones ? – Luke Cowell Dec 05 '12 at 16:16

2 Answers2

5

You can get it to work by doing something like this:

task :task_name, :arg1 do |t, args|
  Cucumber::Rake::Task.new(:run) do |t|
    t.cucumber_opts = "--format pretty --tags @#{args[:arg1]}"
  end
  Rake::Task[:run].invoke()
end
Kent Le
  • 65
  • 1
  • 5
-1

One way of doing this would be through environment variables like so: rake cucumber:tags TAGS=tag1,tag2 and then in your rake task just parse ARGV.

tags = ARGV[1].gsub(/.+=/, '')

Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
  t.cucumber_opts = ['--tags', tags]
end

You can also make it more flexible like this

tags_index = ARGV.index {|a| a.start_with?('TAGS') }
tags = ARGV[tags_index].gsub(/.+=/, '')

but probably you'd be better of with something like OptionParser.

Lenart
  • 3,101
  • 1
  • 26
  • 28