Is it possible to make an interactive Rake task?

后端 未结 4 1546
盖世英雄少女心
盖世英雄少女心 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:14

    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?

提交回复
热议问题