Module: CLI::UI::Prompt
- Defined in:
- lib/cli/ui/prompt.rb,
lib/cli/ui/prompt/options_handler.rb,
lib/cli/ui/prompt/interactive_options.rb
Class Method Summary collapse
-
.ask(question, options: nil, default: nil, is_file: nil, allow_empty: true, &options_proc) ⇒ Object
Ask a user a question with either free form answer or a set of answers (multiple choice) Can use arrows, y/n, numbers (1/2), and vim bindings to control multiple choice selection Do not use this method for yes/no questions.
-
.confirm(question) ⇒ Object
Asks the user a yes/no question.
Class Method Details
.ask(question, options: nil, default: nil, is_file: nil, allow_empty: true, &options_proc) ⇒ Object
Ask a user a question with either free form answer or a set of answers (multiple choice)
Can use arrows, y/n, numbers (1/2), and vim bindings to control multiple choice selection
Do not use this method for yes/no questions. Use confirm
- Handles free form answers (options are nil)
- Handles default answers for free form text
- Handles file auto completion for file input
- Handles interactively choosing answers using
InteractiveOptions
https://user-images.githubusercontent.com/3074765/33799822-47f23302-dd01-11e7-82f3-9072a5a5f611.png
Attributes
question- (required) The question to ask the user
Options
:options- Options that the user may select from. Will useInteractiveOptionsto do so.:default- The default answer to the question (e.g. they just press enter and don't input anything):is_file- Tells the input to use file auto-completion (tab completion):allow_empty- Allows the answer to be empty
Note:
:optionsor providing aBlockconflicts with:defaultand:is_file, you cannot set options with either of these keywords:defaultconflicts with +:allow_empty:, you cannot set these together:optionsconflicts with providing aBlock, you may only set one
Block (optional)
- A Proc that provides a
OptionsHandlerand uses the public:optionmethod to add options and their respective handlers
Return Value
- If a
Blockwas not provided, the selected option or response to the free form question will be returned - If a
Blockwas provided, the evaluted value of theBlockwill be returned
Example Usage:
Free form question CLI::UI::Prompt.ask('What color is the sky?')
Free form question with a file answer CLI::UI::Prompt.ask('Where is your Gemfile located?', is_file: true)
Free form question with a default answer CLI::UI::Prompt.ask('What color is the sky?', default: 'blue')
Free form question when the answer can be empty CLI::UI::Prompt.ask('What is your opinion on this question?', allow_empty: true)
Interactive (multiple choice) question CLI::UI::Prompt.ask('What kind of project is this?', options: %w(rails go ruby python))
Interactive (multiple choice) question with defined handlers CLI::UI::Prompt.ask('What kind of project is this?') do |handler| handler.option('rails') { |selection| selection } handler.option('go') { |selection| selection } handler.option('ruby') { |selection| selection } handler.option('python') { |selection| selection } end
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cli/ui/prompt.rb', line 74 def ask(question, options: nil, default: nil, is_file: nil, allow_empty: true, &) if (( || block_given?) && (default || is_file)) raise(ArgumentError, 'conflicting arguments: options provided with default or is_file') end if || block_given? ask_interactive(question, , &) else ask_free_form(question, default, is_file, allow_empty) end end |
.confirm(question) ⇒ Object
Asks the user a yes/no question. Can use arrows, y/n, numbers (1/2), and vim bindings to control
Example Usage:
Confirmation question CLI::UI::Prompt.confirm('Is the sky blue?')
94 95 96 |
# File 'lib/cli/ui/prompt.rb', line 94 def confirm(question) ask_interactive(question, %w(yes no)) == 'yes' end |