Skip to content

Commit f696f0b

Browse files
committed
ask: add support for default answer.
1 parent f72576f commit f696f0b

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ line options parser code.
6969
In addition to #cli, CLI.K provides the #ask method. This is a very simple
7070
command line query method.
7171

72-
ans = ask "Are you nice? [Y/N]"
72+
ans = ask "Are you nice? [Y/n]"
7373

7474
Other Ruby libraries have their own take on the #ask method, and this very
7575
simple implementation can just as soon be overridden. No biggy. But it's nice

lib/clik/ask.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Kernel
66
# via the console. A prompt will be sent to $stdout,
77
# if given, and the input taken from $stdin...
88
#
9-
# ask "Are you happy? [Yn]"
9+
# ask "Are you happy? [Yn]" "Y"
1010
#
1111
# On the command line one would see...
1212
#
@@ -19,10 +19,15 @@ module Kernel
1919
# The ask method would return "Y".
2020
#
2121
# Returns [String]
22-
def ask(prompt=nil)
22+
def ask(prompt=nil, default_answer=Y)
2323
$stdout << "#{prompt}"
2424
$stdout.flush
25-
$stdin.gets.chomp!
25+
ans = $stdin.gets.chomp!
26+
if ans == ''
27+
default_answer
28+
else
29+
ans
30+
end
2631
end
2732

2833
end

0 commit comments

Comments
 (0)