Class: EasyPrompt
- Inherits:
-
Object
- Object
- EasyPrompt
- Defined in:
- lib/easyprompt.rb
Overview
EasyPrompt is a utility for command-line scripts. It handles prompts and default values, and also provides a testing facility for mocking out the command-line user.
For example, here’s an irb session that illustrates what EasyPrompt does:
irb(main):001:0> require 'easyprompt'
=> true
irb(main):002:0> prompt = EasyPrompt.new
=> #<EasyPrompt:0x5a42a0 @stdout=#<EasyPrompt::MockableStdout:0x5a3e04>>
irb(main):003:0> fname = prompt.ask( "What's your first name?" )
What's your first name? John
=> "John"
irb(main):004:0> lname = prompt.ask( "What's your last name?", "Doe" )
What's your last name? [Doe]
=> "Doe"
irb(main):005:0> correct = prompt.ask( "Is your name #{ fname } #{ lname }?", true, :boolean )
Is your name John Doe? [y]
=> true
In the first example, we ask for the user’s first name and get “John” as the response. In the second example, we supply the default of “Doe”, which the user chooses by just pressing the “Enter” key. In the third example, we supply the default of true
, which the user chooses as well. We received the boolean value true
as opposed to the string “true” or “y”, because we specified :boolean
as the response_class
.
Defined Under Namespace
Classes: MockCommandLineUser, MockableStdin, MockableStdout
Constant Summary collapse
- Version =
'0.1.0'
Instance Method Summary collapse
-
#ask(msg, default = nil, response_class = :string) ⇒ Object
Asks the user for input.
-
#initialize ⇒ EasyPrompt
constructor
A new instance of EasyPrompt.
-
#prompt(msg, default = nil, response_class = :string) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ EasyPrompt
Returns a new instance of EasyPrompt.
25 |
# File 'lib/easyprompt.rb', line 25 def initialize; @stdout = MockableStdout.get_mockable_stdout; end |
Instance Method Details
#ask(msg, default = nil, response_class = :string) ⇒ Object
Asks the user for input.
- msg
-
The prompt that tells the user what information to enter next.
- default
-
The default value that will be returned if the user enters a newline (usually by pressing “Enter”) without typing any information. The default value will be displayed in square brackets after
msg
. - response_class
-
The sort of value that EasyPrompt#ask should return. Valid response classes are:
- :string
-
This is the default.
- :boolean
-
Values will be turned into
true
orfalse
depending on whether the user enters “y” or “n”.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/easyprompt.rb', line 33 def ask( msg, default = nil, response_class = :string ) @stdout.write( prompt( msg, default, response_class ) + ' ' ) stdin = MockableStdin.get_mockable_stdin response = stdin.gets response.chomp! if response == '' response = default else response = response =~ /^y/i if response_class == :boolean end response end |
#prompt(msg, default = nil, response_class = :string) ⇒ Object
:nodoc:
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/easyprompt.rb', line 46 def prompt( msg, default = nil, response_class = :string ) #:nodoc: prompt = msg unless default.nil? if response_class == :boolean default_str = default ? 'y' : 'n' else default_str = default.to_s end prompt += " [#{ default_str }]" end prompt end |