How to pass command line arguments to a rake task

后端 未结 19 1933
走了就别回头了
走了就别回头了 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:32

    Actually @Nick Desjardins answered perfect. But just for education: you can use dirty approach: using ENV argument

    task :my_task do
      myvar = ENV['myvar']
      puts "myvar: #{myvar}"
    end 
    
    rake my_task myvar=10
    #=> myvar: 10
    
    0 讨论(0)
  • 2020-11-22 10:33

    The ways to pass argument are correct in above answer. However to run rake task with arguments, there is a small technicality involved in newer version of rails

    It will work with rake "namespace:taskname['argument1']"

    Note the Inverted quotes in running the task from command line.

    0 讨论(0)
  • 2020-11-22 10:35
    desc 'an updated version'
    task :task_name, [:arg1, :arg2] => [:dependency1, :dependency2] do |t, args|
        puts args[:arg1]
    end
    
    0 讨论(0)
  • 2020-11-22 10:37

    To pass arguments to the default task, you can do something like this. For example, say "version" is your argument:

    task :default, [:version] => [:build]
    
    task :build, :version do |t,args|
      version = args[:version]
      puts version ? "version is #{version}" : "no version passed"
    end
    

    Then you can call it like so:

    $ rake
    no version passed
    

    or

    $ rake default[3.2.1]
    version is 3.2.1
    

    or

    $ rake build[3.2.1]
    version is 3.2.1
    

    However, I have not found a way to avoid specifying the task name (default or build) while passing in arguments. Would love to hear if anyone knows of a way.

    0 讨论(0)
  • 2020-11-22 10:38

    I couldn't figure out how to pass args and also the :environment until I worked this out:

    namespace :db do
      desc 'Export product data'
      task :export, [:file_token, :file_path] => :environment do |t, args|
        args.with_defaults(:file_token => "products", :file_path => "./lib/data/")
    
           #do stuff [...]
    
      end
    end
    

    And then I call like this:

    rake db:export['foo, /tmp/']
    
    0 讨论(0)
  • 2020-11-22 10:38

    I like the "querystring" syntax for argument passing, especially when there are a lot of arguments to be passed.

    Example:

    rake "mytask[width=10&height=20]"
    

    The "querystring" being:

    width=10&height=20
    

    Warning: note that the syntax is rake "mytask[foo=bar]" and NOT rake mytask["foo=bar"]

    When parsed inside the rake task using Rack::Utils.parse_nested_query , we get a Hash:

    => {"width"=>"10", "height"=>"20"}
    

    (The cool thing is that you can pass hashes and arrays, more below)

    This is how to achieve this:

    require 'rack/utils'
    
    task :mytask, :args_expr do |t,args|
      args.with_defaults(:args_expr => "width=10&height=10")
      options = Rack::Utils.parse_nested_query(args[:args_expr])
    end
    

    Here's a more extended example that I'm using with Rails in my delayed_job_active_record_threaded gem:

    bundle exec rake "dj:start[ebooks[workers_number]=16&ebooks[worker_timeout]=60&albums[workers_number]=32&albums[worker_timeout]=120]"
    

    Parsed the same way as above, with an environment dependency (in order load the Rails environment)

    namespace :dj do
      task :start, [ :args_expr ] => :environment do |t, args|
        # defaults here...
        options = Rack::Utils.parse_nested_query(args[:args_expr])  
      end
    end
    

    Gives the following in options

    => {"ebooks"=>{"workers_number"=>"16", "worker_timeout"=>"60"}, "albums"=>{"workers_number"=>"32", "worker_timeout"=>"120"}}
    
    0 讨论(0)
提交回复
热议问题