【问题标题】:Asking questions in rake tasks在 rake 任务中提问
【发布时间】:2011-07-10 09:53:30
【问题描述】:

我有一个从另一个 rake 任务调用的 rake 任务。

在这个 rake 任务中,我需要询问用户一些文本输入,然后根据答案继续,或者停止一切继续(包括调用 rake 任务)。

我该怎么做?

【问题讨论】:

    标签: ruby rake


    【解决方案1】:
    task :input_test do
      input = ''
      STDOUT.puts "What is the airspeed velocity of a swallow?"
      input = STDIN.gets.chomp
      raise "bah, humbug!" unless input == "an african or european swallow?"
    end
    task :blah_blah => :input_test do 
    end
    

    我觉得应该可以

    【讨论】:

    • 请使用 STDIN.gets.chomp 截断换行符.. 以获得精确的字符串匹配..
    • @kaiser ,只需决定如何处理 if/then/else 或 unless 或什至 case 类型的控制结构。在这种情况下,我提出了一个例外(以停止执行,如 OP 的问题),但您可以对输入做任何事情,只需更改问题以明确您想要一个是否,其他答案之一是我认为
    • 谢谢。我已经在 CodCademy 上开设了 Ruby 课程。对于elsifunless,我的PHP 太多了...:P
    • 知道在输入“no”后按下回车键时此代码何时会显示“^M”吗??
    【解决方案2】:
    task :ask_question do
      puts "Do you want to go through with the task(Y/N)?"
      get_input
    end
    
    task :continue do
      puts "Task starting..."
      # the task is executed here
    end
    
    def get_input
      STDOUT.flush
      input = STDIN.gets.chomp
      case input.upcase
      when "Y"
        puts "going through with the task.."
        Rake::Task['continue'].invoke
      when "N"
        puts "aborting the task.."
      else
        puts "Please enter Y or N"
        get_input
      end
    end 
    

    【讨论】:

      【解决方案3】:

      HighLine gem 让这一切变得简单。

      对于简单的是或否问题,您可以使用agree

      require "highline/import"
      task :agree do
        if agree("Shall we continue? ( yes or no )")
          puts "Ok, lets go"
        else
          puts "Exiting"
        end
      end
      

      如果您想做更复杂的事情,请使用ask

      require "highline/import"
      task :ask do
        answer = ask("Go left or right?") { |q|
          q.default   = "left"
          q.validate  = /^(left|right)$/i
        }
        if answer.match(/^right$/i)
          puts "Going to the right"
        else
          puts "Going to the left"
        end
      end
      

      这是宝石的描述:

      HighLine 对象是输入上的“面向行的高级”外壳 和一个输出流。 HighLine 简化了常见的控制台交互, 有效地替换 puts() 和 gets()。用户代码可以简单地指定 要问的问题以及有关用户交互的任何详细信息,然后离开 剩下的工作交给 HighLine。当 HighLine.ask() 返回时,您将 有你要求的答案,即使HighLine不得不问很多次, 验证结果、执行范围检查、转换类型等。

      更多信息您可以read the docs

      【讨论】:

      • 感谢您发布此内容,highline 也解决了我的问题。太棒了!
      • 知道在输入“no”后按下回车键时此代码何时会显示“^M”吗??
      【解决方案4】:

      虽然这个问题已经很老了,但对于没有外部 gem 的简单提示来说,这可能仍然是一个有趣的(也许鲜为人知的)替代方案:

      require 'rubygems/user_interaction'
      include Gem::UserInteraction
      
      task :input_test do
        input = ask("What is the airspeed velocity of a swallow?")
        raise "bah, humbug!" unless input == "an african or european swallow?"
      end
      task :blah_blah => :input_test do 
      end
      

      【讨论】:

        猜你喜欢
        • 2012-02-05
        • 1970-01-01
        • 2010-10-09
        • 1970-01-01
        • 2013-06-15
        • 2012-12-19
        • 2012-02-16
        • 1970-01-01
        • 2011-01-19
        相关资源
        最近更新 更多