问题
I am running an .exe file in the command line from a Ruby script that asks the user for a Yes/No response. I would like to know how the user can interact with it in Windows environment.
I have tried all the possible options: system
, backticks, %x()
, Open3, Open4... and none of them work.
Some posts ([1], [2]) resolve the issue using PTY, but as to my knowledge there is no implementation of the PTY module in Windows. Any other idea?
回答1:
Seems this is working under Windows too
pipe = IO.popen('your.exe', 'w+', :err => [:child, :out])
@pipe.each_line do |line|
if /pattern matching question/ =~ line
break
end
end
pipe.puts('Yes')
# another test can be here
pipe.close
Wise to use with https://ruby-doc.com/stdlib/libdoc/timeout/rdoc/Timeout.html
来源:https://stackoverflow.com/questions/55725954/ruby-calling-commands-and-interact-with-shell-in-windows-environment