【问题标题】:Rake tast with cli arguments in Sinatra在 Sinatra 中使用 cli 参数进行 Rake 任务
【发布时间】:2015-05-23 01:32:29
【问题描述】:

我在我的Sinatra 应用程序中创建了一个rake 文件来为Mongodb 集合创建索引,我正在尝试在rake 任务db:create_indexes 中传递environment 参数。 这是我的db.rake 文件:

namespace :db do
    task :create_indexes, :environment do |t, args|
        puts "Environment : #{args}"
        unless args[:environment]
            puts "Must provide an environment"
            exit
        end
        yaml = YAML.load_file("./config/mongoid.yml")
        env_info = yaml[args[:environment]]
        unless env_info
            puts "Unknown environment"
            exit
        end
        Mongoid.configure do |config|
            config.from_hash(env_info)
        end

        Bin.mongoid:create_indexes
    end
end

应用根目录中的Rakefile 还包含:

require 'rake'
require 'rubygems'
require 'bundler/setup'
Dir.glob('lib/tasks/*.rake').each { |r| load r}

但是当我尝试使用命令rake db:create_indexes[development] 运行 rake 任务时,我收到以下错误,no matches found: db:create_indexes[development]

现在我对如何解决这个问题一无所知。

【问题讨论】:

    标签: ruby sinatra mongoid rake


    【解决方案1】:

    所以问题不在于代码,而在于我使用的 shell。 我使用zsh shell 代替bash,似乎zsh 要求您转义括号:rake my_task\['arg1'\]

    因此代码适用于rake db:create_indexes\['development'\]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2013-12-03
      • 2016-03-09
      • 2011-07-24
      • 2017-08-02
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多