是否可以制作交互式 Rake 任务?

发布于 2024-10-27 03:10:51 字数 99 浏览 0 评论 0原文

我想运行一个要求用户输入的 Rake 任务。

我知道我可以在命令行上提供输入,但我想询问用户是否确定要继续执行特定操作,以防他们错误输入提供给 Rake 的值之一任务。

I want to run a Rake task that asks the user for input.

I know that I can supply input on the command line, but I want to ask the user if they are sure they want to proceed with a particular action in case they mistyped one of the values supplied to the Rake task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往事随风而去 2024-11-03 03:10:51

像这样的事情可能会工作

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

任务重新启用并从 调用如何从 Rake 任务中运行 Rake 任务?

Something like this might work

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

Task reenabling and invoking from How to run Rake tasks from within Rake tasks?

独自唱情﹋歌 2024-11-03 03:10:51

这是一个不使用其他任务的示例。

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

Here's an example without using another task.

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
若水微香 2024-11-03 03:10:51

用户输入的一个方便功能是将其放入 do..while 循环中,仅在用户提供有效输入时才继续。 Ruby 没有显式地具有此构造,但您可以使用 beginuntil 实现相同的效果。这将添加到已接受的答案中,如下所示:

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

A handy feature for user input is to put it in a do..while loop, to only continue when a user has supplied valid input. Ruby doesn't explicitly have this construct, but you can achieve the same thing with begin and until. That would add to the accepted answer as follows:

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
硬不硬你别怂 2024-11-03 03:10:51

您还可以将其包装在服务类中,以便可以对其进行单元测试并在您的 rake 任务中使用:

# frozen_string_literal: true

class RakeConfirmDialog
  def initialize(question)
    @question = "#{question} (y/n)"
    @answer = "".inquiry
  end

  def confirm!
    prompt until (proceed? || abort?)

    respond

    proceed?
  end

  private

  def prompt
    STDOUT.puts @question

    @answer = STDIN.gets.strip.downcase.inquiry
  end

  def respond
    STDOUT.puts proceed? ? "Proceeding." : "Aborting."
  end

  def proceed?
    @answer.y?
  end

  def abort?
    @answer.n?
  end
end

然后在您的任务中像这样使用它:

next unless RakeConfirmDialog.new(
  "About to close the Hellmouth forever. Are you sure you want 'Buffy the Vampire Slayer' to have a happy ending?"
).confirm!

You could also wrap this up in a service class so it can be unit-tested and used across your rake tasks:

# frozen_string_literal: true

class RakeConfirmDialog
  def initialize(question)
    @question = "#{question} (y/n)"
    @answer = "".inquiry
  end

  def confirm!
    prompt until (proceed? || abort?)

    respond

    proceed?
  end

  private

  def prompt
    STDOUT.puts @question

    @answer = STDIN.gets.strip.downcase.inquiry
  end

  def respond
    STDOUT.puts proceed? ? "Proceeding." : "Aborting."
  end

  def proceed?
    @answer.y?
  end

  def abort?
    @answer.n?
  end
end

Then use it like so in your task:

next unless RakeConfirmDialog.new(
  "About to close the Hellmouth forever. Are you sure you want 'Buffy the Vampire Slayer' to have a happy ending?"
).confirm!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文