How can I call a rake task with default args from the commad line?
Asked Answered
I

1

6

I have a rake task to generate a given amount of items in a xml file. If nothing is given I want to have a default. By now only 50 gets printed, but I would like to have a variable amount here, how would I call it from the command line?

namespace :utilites do
  desc "generate xml"
  task generate_xml: :environment do |t, args|
    args.with_defaults(:length => 50)
    p args[:length]
  end

end
Imbalance answered 30/7, 2015 at 18:9 Comment(0)
T
11

If you define length as a parameter it works correct:

namespace :utilities do
  desc "generate xml"
  task :generate_xml, :length do |t, args|
    args.with_defaults(:length => 50)
    p args[:length]
  end
end

The result:

C:\Temp>rake utilities:generate_xml
50

C:\Temp>rake utilities:generate_xml[99]
"99"

As you see, the given parameter is a string! If you need a number you must convert it before you use it.


I don't know what your :environment is. If it should be a prerequisite, you can use:

namespace :utilities do
  task :environment do
    puts "Run environment"
  end
  desc "generate xml"
  task :generate_xml, [:length] => :environment do |t, args|
    args.with_defaults(:length => 50)
    p args[:length]
  end
end
Turley answered 30/7, 2015 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.