我想运行一个Rake任务,要求用户输入.
我知道我可以在命令行上提供输入,但是我想询问用户是否确定他们想要继续执行特定操作,以防他们错误输入提供给Rake任务的其中一个值.
ric*_*rte 84
这样的事可能有用
task :action do
STDOUT.puts "I'm acting!"
end
task :check do
STDOUT.puts "Are you sure? (y/n)"
input = STDIN.gets.strip
if input == 'y'
Rake::Task["action"].reenable
Rake::Task["action"].invoke
else
STDOUT.puts "So sorry for the confusion"
end
end
Run Code Online (Sandbox Code Playgroud)
任务重新启用和调用如何从Rake任务中运行Rake任务?
这是一个不使用其他任务的示例。
task :solve_earth_problems => :environment do
STDOUT.puts "This is risky. Are you sure? (y/n)"
begin
input = STDIN.gets.strip.downcase
end until %w(y n).include?(input)
if input != 'y'
STDOUT.puts "So sorry for the confusion"
return
end
# user accepted, carry on
Humanity.wipe_out!
end
Run Code Online (Sandbox Code Playgroud)
用户输入的一个便利功能是将其置于do..while循环中,仅在用户提供有效输入时才继续.Ruby没有明确地拥有这个构造,但你可以用begin和实现相同的东西until.这将增加接受的答案如下:
task :action do
STDOUT.puts "I'm acting!"
end
task :check do
# Loop until the user supplies a valid option
begin
STDOUT.puts "Are you sure? (y/n)"
input = STDIN.gets.strip.downcase
end until %w(y n).include?(input)
if input == 'y'
Rake::Task["action"].reenable
Rake::Task["action"].invoke
else
# We know at this point that they've explicitly said no,
# rather than fumble the keyboard
STDOUT.puts "So sorry for the confusion"
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12814 次 |
| 最近记录: |