Skip to content

Commit bd4e62b

Browse files
Devan HurstDevan Hurst
authored andcommitted
Add :case-insensitive flag to allow case-insensitive matching for :limited_to options.
1 parent 37001c9 commit bd4e62b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/thor/shell/basic.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,22 @@ def ask_simply(statement, color, options)
451451

452452
def ask_filtered(statement, color, options)
453453
answer_set = options[:limited_to]
454+
case_insensitive = options.fetch(:case_insensitive, false)
454455
correct_answer = nil
455456
until correct_answer
456457
answers = answer_set.join(", ")
457458
answer = ask_simply("#{statement} [#{answers}]", color, options)
458-
correct_answer = answer_set.include?(answer) ? answer : nil
459+
correct_answer = answer_match(answer_set, answer, case_insensitive)
459460
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
460461
end
461462
correct_answer
462463
end
463464

465+
def answer_match(possibilities, answer, case_insensitive)
466+
return possibilities.detect{ |possibility| possibility == answer } unless case_insensitive
467+
possibilities.detect{ |possibility| possibility.downcase == answer.downcase }
468+
end
469+
464470
def merge(destination, content) #:nodoc:
465471
require "tempfile"
466472
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|

spec/shell/basic_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,40 @@ def shell
6868
expect(shell.ask("What's your password?", :echo => false)).to eq("mysecretpass")
6969
end
7070

71-
it "prints a message to the user with the available options and determines the correctness of the answer" do
71+
it "prints a message to the user with the available options, expects case-sensitive matching, and determines the correctness of the answer" do
7272
flavors = %w(strawberry chocolate vanilla)
7373
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("chocolate")
7474
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
7575
end
7676

77-
it "prints a message to the user with the available options and reasks the question after an incorrect response" do
77+
it "prints a message to the user with the available options, expects case-sensitive matching, and reasks the question after an incorrect response" do
7878
flavors = %w(strawberry chocolate vanilla)
7979
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
8080
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("moose tracks", "chocolate")
8181
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
8282
end
8383

84+
#this one
85+
it "prints a message to the user with the available options, expects case-sensitive matching, and reasks the question after a case-insensitive match" do
86+
flavors = %w(strawberry chocolate vanilla)
87+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
88+
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("cHoCoLaTe", "chocolate")
89+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
90+
end
91+
92+
it "prints a message to the user with the available options, expects case-insensitive matching, and determines the correctness of the answer" do
93+
flavors = %w(strawberry chocolate vanilla)
94+
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors, :case_insensitive => true).and_return("CHOCOLATE")
95+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
96+
end
97+
98+
it "prints a message to the user with the available options, expects case-insensitive matching, and reasks the question after an incorrect response" do
99+
flavors = %w(strawberry chocolate vanilla)
100+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
101+
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors, :case_insensitive => true).and_return("moose tracks", "chocolate")
102+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
103+
end
104+
84105
it "prints a message to the user containing a default and sets the default if only enter is pressed" do
85106
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ', :default => "vanilla").and_return("")
86107
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :default => "vanilla")).to eq("vanilla")

0 commit comments

Comments
 (0)