Skip to content

Commit dbfdd30

Browse files
author
Van Miranda
committed
Update Ask documentation and change API to be :echo => false
1 parent eb3ceb2 commit dbfdd30

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/thor/shell/basic.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ def padding=(value)
4141
# they will be shown a message stating that one of those answers
4242
# must be given and re-asked the question.
4343
#
44+
# If asking for sensitive information, the :echo option can be set
45+
# to false to mask user input from $stdin.
46+
#
4447
# ==== Example
4548
# ask("What is your name?")
4649
#
4750
# ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])
4851
#
52+
# ask("What is your password?", :echo => false)
53+
#
4954
def ask(statement, *args)
5055
options = args.last.is_a?(Hash) ? args.pop : {}
5156
color = args.first
@@ -383,7 +388,7 @@ def ask_simply(statement, color, options)
383388
message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
384389
say(message, color)
385390

386-
result = if options[:noecho]
391+
result = if options[:echo] == false
387392
stdin.noecho(&:gets)
388393
else
389394
stdin.gets

spec/shell/basic_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def shell
2020
it "prints a message to the user and gets the response" do
2121
$stdout.should_receive(:print).with("Should I overwrite it? ")
2222
$stdin.should_receive(:gets).and_return('Sure')
23+
$stdin.should_not_receive(:noecho).and_return('Sure')
2324
expect(shell.ask("Should I overwrite it?")).to eq("Sure")
2425
end
2526

@@ -29,10 +30,10 @@ def shell
2930
expect(shell.ask("")).to eq(nil)
3031
end
3132

32-
it "prints a message to the user and does not echo stdin if the noecho option is set to true" do
33+
it "prints a message to the user and does not echo stdin if the echo option is set to false" do
3334
$stdout.should_receive(:print).with('What\'s your password? ')
3435
$stdin.should_receive(:noecho).and_return('mysecretpass')
35-
expect(shell.ask("What's your password?", :noecho => true)).to eq("mysecretpass")
36+
expect(shell.ask("What's your password?", :echo => false)).to eq("mysecretpass")
3637
end
3738

3839
it "prints a message to the user with the available options and determines the correctness of the answer" do

0 commit comments

Comments
 (0)