Skip to content

Commit 37a2e33

Browse files
Add support for a default to #ask
1 parent dc96d00 commit 37a2e33

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/thor/shell/basic.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def padding=(value)
4848
def ask(statement, *args)
4949
options = args.last.is_a?(Hash) ? args.pop : {}
5050

51-
options[:limited_to] ? ask_filtered(statement, options[:limited_to], *args) : ask_simply(statement, *args)
51+
options[:limited_to] ? ask_filtered(statement, options, *args) : ask_simply(statement, options, *args)
5252
end
5353

5454
# Say (print) something to the user. If the sentence ends with a whitespace
@@ -372,15 +372,24 @@ def as_unicode
372372
end
373373
end
374374

375-
def ask_simply(statement, color=nil)
376-
say("#{statement} ", color)
377-
stdin.gets.tap{|text| text.strip! if text}
375+
def ask_simply(statement, options = {}, color=nil)
376+
default = options[:default]
377+
message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
378+
say(message, color)
379+
result = stdin.gets.tap{|text| text.strip! if text}
380+
381+
if default && result == ""
382+
default
383+
else
384+
result
385+
end
378386
end
379387

380-
def ask_filtered(statement, answer_set, *args)
388+
def ask_filtered(statement, options = {}, *args)
389+
answer_set = options[:limited_to]
381390
correct_answer = nil
382391
until correct_answer
383-
answer = ask_simply("#{statement} #{answer_set.inspect}", *args)
392+
answer = ask_simply("#{statement} #{answer_set.inspect}", options, *args)
384393
correct_answer = answer_set.include?(answer) ? answer : nil
385394
answers = answer_set.map(&:inspect).join(", ")
386395
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer

spec/shell/basic_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ def shell
4242
$stdin.should_receive(:gets).and_return('moose tracks', 'chocolate')
4343
expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
4444
end
45+
46+
it "prints a message to the user containing a default and sets the default if only enter is pressed" do
47+
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ("vanilla") ')
48+
$stdin.should_receive(:gets).and_return('')
49+
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla")).to eq("vanilla")
50+
end
51+
52+
it "prints a message to the user with the available options and reasks the question after an incorrect repsonse and then returns the default" do
53+
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ("vanilla") ').twice
54+
$stdout.should_receive(:puts).with('Your response must be one of: ["strawberry", "chocolate", "vanilla"]. Please try again.')
55+
$stdin.should_receive(:gets).and_return('moose tracks', '')
56+
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("vanilla")
57+
end
4558
end
4659

4760
describe "#yes?" do

0 commit comments

Comments
 (0)