Method: CommandKit::Interactive#ask
- Defined in:
- lib/command_kit/interactive.rb
#ask(prompt, default: nil, required: false) ⇒ String
Asks the user for input.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/command_kit/interactive.rb', line 72 def ask(prompt, default: nil, required: false) prompt = prompt.chomp prompt << " [#{default}]" if default prompt << ": " stdout.print(prompt) loop do value = stdin.gets value ||= '' # convert nil values (ctrl^D) to an empty String if value.empty? if required next else return (default || value) end else return value end end end |