我不明白如何向 rake 任务添加参数。 (摘自文档)
I don't understand how to add arguments to a rake task. (Excerpt from the documentation)
我正在尝试创建一个接受两个参数并在我的代码中使用它们的自定义 rake 任务。
我正在查看 rails 文档,我看到 运行 一个带有参数的 rails 任务的摘录:
task :task_name, [:arg_1] => [:pre_1, :pre_2] do |t, args|
# You can use args from here
end
然后可以像这样调用 rake 任务:
bin/rake "task_name[value 1]"
但是,这对我来说太模糊了。 rails 文档未能给出带有参数的 rake 任务的具体示例。
例如,我正在查看这段代码,我在想 bin/rake "task_name[value 1]"
做了什么? [:pre1, :pre2]
是什么?
此外,我还发现了一些其他非常棒的链接,它们的功能略有不同。这是链接。
thoughtbot版本中有这个例子
task :send, [:username] => [:environment] do |t, args|
Tweet.send(args[:username])
end
什么是[:username => [:environment]
?它不同于官方 rails 文档。
这是另一个:
4 ways to write rake tasks with arguments
我还查看了 officail optparser 文档,它也有不同的工作方式。
我想要的只是这个示例代码,我必须在我的 .rake
文件上工作:
require 'optparse'
task :add do
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: rake add"
opts.on("-o", "--one ARGV", Integer) { |one| options[:one] = one }
opts.on("-t", "--two ARGV", Integer) { |two| options[:two] = two }
end.parse!
puts options[:one].to_i + options[:two].to_i
end
由于 invalid option: -o
,代码失败。我只想完成这项工作,这样我就可以继续前进。有人有什么想法吗?
这是我的一个带参数的抽成任务:
namespace :admin do
task :create_user, [:user_email, :user_password, :is_superadmin] => :environment do |t, args|
email = args[:email]
password = args[:password]
is_superadmin = args[:is_superadmin]
... lots of fun code ...
end
end
我这样调用这个任务:
rake admin:create_user['admin@example.com','password',true]
编辑
要传递标志,您可以这样做:
task :test_task do |t, args|
options = {a: nil, b: nil}
OptionParser.new do |opts|
opts.banner = "Usage: admin:test_task [options]"
opts.on("--a", "-A", "Adds a") do |a|
options[:a] = true
end
opts.on("--b", "-B", "Adds b") do |b|
options[:b] = true
end
end.parse!
puts options.inspect
end
以及调用它的例子:
rake admin:test_task -A -B
rake admin:test_task -A
rake admin:test_task -B
我正在尝试创建一个接受两个参数并在我的代码中使用它们的自定义 rake 任务。
我正在查看 rails 文档,我看到 运行 一个带有参数的 rails 任务的摘录:
task :task_name, [:arg_1] => [:pre_1, :pre_2] do |t, args|
# You can use args from here
end
然后可以像这样调用 rake 任务:
bin/rake "task_name[value 1]"
但是,这对我来说太模糊了。 rails 文档未能给出带有参数的 rake 任务的具体示例。
例如,我正在查看这段代码,我在想 bin/rake "task_name[value 1]"
做了什么? [:pre1, :pre2]
是什么?
此外,我还发现了一些其他非常棒的链接,它们的功能略有不同。这是链接。
thoughtbot版本中有这个例子
task :send, [:username] => [:environment] do |t, args|
Tweet.send(args[:username])
end
什么是[:username => [:environment]
?它不同于官方 rails 文档。
这是另一个: 4 ways to write rake tasks with arguments
我还查看了 officail optparser 文档,它也有不同的工作方式。
我想要的只是这个示例代码,我必须在我的 .rake
文件上工作:
require 'optparse'
task :add do
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: rake add"
opts.on("-o", "--one ARGV", Integer) { |one| options[:one] = one }
opts.on("-t", "--two ARGV", Integer) { |two| options[:two] = two }
end.parse!
puts options[:one].to_i + options[:two].to_i
end
由于 invalid option: -o
,代码失败。我只想完成这项工作,这样我就可以继续前进。有人有什么想法吗?
这是我的一个带参数的抽成任务:
namespace :admin do
task :create_user, [:user_email, :user_password, :is_superadmin] => :environment do |t, args|
email = args[:email]
password = args[:password]
is_superadmin = args[:is_superadmin]
... lots of fun code ...
end
end
我这样调用这个任务:
rake admin:create_user['admin@example.com','password',true]
编辑
要传递标志,您可以这样做:
task :test_task do |t, args|
options = {a: nil, b: nil}
OptionParser.new do |opts|
opts.banner = "Usage: admin:test_task [options]"
opts.on("--a", "-A", "Adds a") do |a|
options[:a] = true
end
opts.on("--b", "-B", "Adds b") do |b|
options[:b] = true
end
end.parse!
puts options.inspect
end
以及调用它的例子:
rake admin:test_task -A -B
rake admin:test_task -A
rake admin:test_task -B