Is it possible to make an interactive Rake task?

后端 未结 4 1556
盖世英雄少女心
盖世英雄少女心 2021-01-31 14:21

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 wa

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 15:05

    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
    

提交回复
热议问题