无法将args传递给rake任务 - 语法是否已更改?

时间:2011-11-25 12:04:43

标签: ruby rake

我正在使用具有rake(0.9.2.2)的rails 3.1.3项目。我想在rake任务中执行此操作:将其称为

rake tale:import_kml /path/to/file.txt

然后在rake任务中,以args.filename

访问“/path/to/file.txt”

我以为我能够这样做(看跌期权有点调试):

namespace :tale do
  desc "Expects to get a file or folder name as the first argument, and passes that to Tale.import_kml"
  task(:import_kml, [:filename] => :environment) do |t, args|
    puts "args = #{args.inspect}"
    if File.exists?(args.filename)
      Tale.import_kml(filename)
    end
  end
end

但是,我明白了:

** Invoke tale:import_kml (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute tale:import_kml
args = {}
rake aborted!
can't convert nil into String

所以,filename并没有把它变成args。我无法解决我在这里做错的事情......

2 个答案:

答案 0 :(得分:2)

试试这样的片段:

namespace :tale do
  desc "Expects to get a file or folder ..."
  task(:import_kml, [:filename]) do |t, args|
    args.with_default(:filename => :environment)
    puts "args = #{args.inspect}"
  end
end

rake tale:import_kml[foo]  # => args = {:filename => "foo"}

答案 1 :(得分:0)

WarHog帮助我解决了这个问题:我不得不将“任务”行更改为

task :import_kml, [:filename] => [:environment] do |t, args|

并将其称为

rake tale:import_kml[/path/to/file.txt]
相关问题