Skip to content

Commit eb3ceb2

Browse files
author
Van Miranda
committed
Shell#ask: support a noecho option for stdin
1 parent 8a09e6b commit eb3ceb2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/thor/shell/basic.rb

Lines changed: 7 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
@@ -381,7 +382,12 @@ def ask_simply(statement, color, options)
381382
default = options[:default]
382383
message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
383384
say(message, color)
384-
result = stdin.gets
385+
386+
result = if options[:noecho]
387+
stdin.noecho(&:gets)
388+
else
389+
stdin.gets
390+
end
385391

386392
return unless result
387393

spec/shell/basic_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def shell
2929
expect(shell.ask("")).to eq(nil)
3030
end
3131

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

3338
it "prints a message to the user with the available options and determines the correctness of the answer" do
3439
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ')

0 commit comments

Comments
 (0)