Skip to content

Commit f6e7c91

Browse files
authored
Merge pull request #644 from devanhurst/feature/case-insensitive-limited-to
Add :case-insensitive flag to allow case-insensitive matching for :limited_to options 🌈
2 parents ae5cc64 + fe8018b commit f6e7c91

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/thor/shell/basic.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,25 @@ 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+
if case_insensitive
467+
possibilities.detect{ |possibility| possibility.downcase == answer.downcase }
468+
else
469+
possibilities.detect{ |possibility| possibility == answer }
470+
end
471+
end
472+
464473
def merge(destination, content) #:nodoc:
465474
require "tempfile"
466475
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|

spec/shell/basic_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,39 @@ 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+
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
85+
flavors = %w(strawberry chocolate vanilla)
86+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
87+
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("cHoCoLaTe", "chocolate")
88+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
89+
end
90+
91+
it "prints a message to the user with the available options, expects case-insensitive matching, and determines the correctness of the answer" do
92+
flavors = %w(strawberry chocolate vanilla)
93+
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")
94+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
95+
end
96+
97+
it "prints a message to the user with the available options, expects case-insensitive matching, and reasks the question after an incorrect response" do
98+
flavors = %w(strawberry chocolate vanilla)
99+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
100+
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")
101+
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
102+
end
103+
84104
it "prints a message to the user containing a default and sets the default if only enter is pressed" do
85105
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ', :default => "vanilla").and_return("")
86106
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :default => "vanilla")).to eq("vanilla")

0 commit comments

Comments
 (0)