How to pass command line arguments to a rake task

后端 未结 19 2183
走了就别回头了
走了就别回头了 2020-11-22 10:13

I have a rake task that needs to insert a value into multiple databases.

I\'d like to pass this value into the rake task from the command line, or from another

19条回答
  •  旧巷少年郎
    2020-11-22 10:50

    I just wanted to be able to run:

    $ rake some:task arg1 arg2
    

    Simple, right? (Nope!)

    Rake interprets arg1 and arg2 as tasks, and tries to run them. So we just abort before it does.

    namespace :some do
      task task: :environment do
        arg1, arg2 = ARGV
    
        # your task...
    
        exit
      end
    end
    

    Take that, brackets!

    Disclaimer: I wanted to be able to do this in a pretty small pet project. Not intended for "real world" usage since you lose the ability to chain rake tasks (i.e. rake task1 task2 task3). IMO not worth it. Just use the ugly rake task[arg1,arg2].

提交回复
热议问题