Class: Clin::ShellInteraction::Choose
- Inherits:
-
Clin::ShellInteraction
- Object
- Clin::ShellInteraction
- Clin::ShellInteraction::Choose
- Defined in:
- lib/clin/shell_interaction/choose.rb
Overview
Handle a choose question
Instance Attribute Summary
Attributes inherited from Clin::ShellInteraction
Instance Method Summary collapse
Methods inherited from Clin::ShellInteraction
#initialize, #persist!, #persist?, #persist_answer
Constructor Details
This class inherits a constructor from Clin::ShellInteraction
Instance Method Details
#run(statement, choices, default: nil, allow_initials: false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/clin/shell_interaction/choose.rb', line 5 def run(statement, choices, default: nil, allow_initials: false) choices = convert_choices(choices) question = prepare_question(statement, choices, default: default, initials: allow_initials) loop do answer = @shell.ask(question, default: default, autocomplete: choices.keys) unless answer.nil? choices.each do |choice, _| if choice.casecmp(answer) == 0 || (allow_initials && choice[0].casecmp(answer[0]) == 0) return choice end end end print_choices_help(choices, allow_initials: allow_initials) end end |