Is there a way to programmatically answer prompts? #166
Answered
by
piotrmurach
cswilliams
asked this question in
Q&A
-
I'd like to add a Is there any supported way in the library to do this? I realize I can just skip over the prompt entirely, but it still would be nice if the output on the screen looked the same as if the user answered "yes". I came up with something like this, but was wondering if there was a better way? Thanks!! require 'tty-prompt'
prompt = TTY::Prompt.new(interrupt: :exit)
question = "Do you like Ruby?"
yes = true if ARGV[0] == "--yes"
color = Pastel.new
if yes
puts "#{question} #{color.green("Yes")}"
else
prompt.yes?(question)
end |
Beta Was this translation helpful? Give feedback.
Answered by
piotrmurach
Jul 3, 2021
Replies: 1 comment 1 reply
-
Hi Chris, I like your solution, it's neat. Another approach could be to provide a 'scripted' input to the prompt like so: if yes
input = StringIO.new
input << "y\n"
input.rewind
prompt = TTY::Prompt.new(input: input)
end
prompt.yes?("Do you like Ruby?") |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
piotrmurach
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Chris,
I like your solution, it's neat. Another approach could be to provide a 'scripted' input to the prompt like so: