Skip to content

Commit 71a892b

Browse files
Merge remote-tracking branch 'socialcast/stdin-noecho-support'
* socialcast/stdin-noecho-support: Update Ask documentation and change API to be :echo => false Shell#ask: support a noecho option for stdin Conflicts: spec/shell/basic_spec.rb
2 parents 58c22cb + dbfdd30 commit 71a892b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/thor/shell/basic.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'tempfile'
2+
require 'io/console'
23

34
class Thor
45
module Shell
@@ -40,11 +41,16 @@ def padding=(value)
4041
# they will be shown a message stating that one of those answers
4142
# must be given and re-asked the question.
4243
#
44+
# If asking for sensitive information, the :echo option can be set
45+
# to false to mask user input from $stdin.
46+
#
4347
# ==== Example
4448
# ask("What is your name?")
4549
#
4650
# ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])
4751
#
52+
# ask("What is your password?", :echo => false)
53+
#
4854
def ask(statement, *args)
4955
options = args.last.is_a?(Hash) ? args.pop : {}
5056
color = args.first
@@ -381,7 +387,12 @@ def ask_simply(statement, color, options)
381387
default = options[:default]
382388
message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
383389
say(message, color)
384-
result = stdin.gets
390+
391+
result = if options[:echo] == false
392+
stdin.noecho(&:gets)
393+
else
394+
stdin.gets
395+
end
385396

386397
return unless result
387398

spec/shell/basic_spec.rb

Lines changed: 7 additions & 1 deletion
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
expect($stdout).to receive(:print).with("Should I overwrite it? ")
2222
expect($stdin).to receive(:gets).and_return('Sure')
23+
expect($stdin).to_not receive(:noecho).and_return('Sure')
2324
expect(shell.ask("Should I overwrite it?")).to eq("Sure")
2425
end
2526

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

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

3339
it "prints a message to the user with the available options and determines the correctness of the answer" do
3440
expect($stdout).to receive(:print).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ')
@@ -209,7 +215,7 @@ def #456 Lanç...
209215
2.times { @table.first.pop }
210216
content = capture(:stdout) { shell.print_table(@table) }
211217
expect(content).to eq(<<-TABLE)
212-
abc
218+
abc
213219
#0 empty
214220
xyz #786 last three
215221
TABLE

0 commit comments

Comments
 (0)