1

I want to create a custom rake task with supporting multiple arguments.

When finished I want to call the rake task like:

rails conifg:load -i 5 -o "now" ...

How to create them ?

The (rubyonrails guides) says :

task :task_name, [:arg_1] => [:prerequisite_1, :prerequisite_2] do |task, args|
  argument_1 = args.arg_1
end

But I dont understand it.

user229044
  • 232,980
  • 40
  • 330
  • 338
Sven Kirsten
  • 478
  • 1
  • 8
  • 27

1 Answers1

1

When finished I want to call the rake task like:

rails conifg:load -i 5 -o "now" ...

You can't, that is not how you pass arguments to Rake tasks, those arguments (-i, -o, etc) are passed to Rake itself, not to your task.

Instead, there are a few options:

  • Use thor which has (in my opinion) a far superior API, and allows for traditional Unix style arguments (-i, --help, etc). You can easily produce a script that would be invoked with:

    thor config:load -i 5 -o "now"
    
  • Use Rake's argument system, where you would pass arguments in the form

    rails config:load[5,now]
    
  • Build your own script, placed in bin. For example, bin/config can accept arbitrary arguments, and be invoked however you like. Any of the following are possible:

    ./bin/config load -i 5 -o now
    ./bin/load_config i=5 o=now
    ./bin/config.load 5 now
    # etc
    
user229044
  • 232,980
  • 40
  • 330
  • 338