How to pass command line arguments to a rake task

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

提交回复
热议问题