Answering a cli prompt in ruby with open3?

烂漫一生 提交于 2020-01-03 06:39:11

问题


Apologies for lack of sample code, I'm on mobile at the moment.

I've gotten ruby+open3 to run commands and save stdout and stderr to a variable.

My question is, if the command line interface prompts the user is it possible to input text into the prompt and press enter? If so how would I go about doing this.

Example explanation Runs program, program in terminal then asks "what is your name?" and waits for input.

I want to input a name, press enter.

Then it asks next question, I want to put to stdin and answer that as well

This is for an automation test. If anyone has a better idea than open3 I'm all ears but I'm restricted to ruby

Thanks


回答1:


Consider this:

Create an input file using:

cat > test.input
bar
baz

Then press CTRL+D to terminate the input, which will cause the file test.input to be created.

In the same directory save this code as test.rb:

2.times do |i|
  user_input = gets.chomp
  puts "#{ i }: #{ user_input }"
end

Run the code using:

ruby test.rb < test.input

and you should see:

0: bar
1: baz

The reason this works is because gets reads the STDIN (by default) looking for a line-end, which in this case is the character trailing bar and baz. If I load the input file in IRB it's easy to see the content of the file:

input = File.read('test.input')
=> "bar\nbaz\n"

2.times says to read a line twice, so it reads both lines from the file and continues, falling out of the times loop.

This means you can create a file, pipe it into your script, and Ruby will do the right thing. If Ruby has called a sub-shell, the sub-shell will inherit Ruby's STDIN, STDOUT and STDERR streams. I can rewrite the test.rb code to:

puts `sh ./test.sh < #{ ARGV[0] }`

and create test.sh:

for i in 1 2
do
  read line
  echo $i $line
done

then call it using:

ruby test.rb test.input

and get:

1 bar
2 baz


来源:https://stackoverflow.com/questions/29953821/answering-a-cli-prompt-in-ruby-with-open3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!