Module: CliTools

Defined in:
lib/cli_tools.rb

Instance Method Summary collapse

Instance Method Details

#choice(say, array, with = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cli_tools.rb', line 27

def choice say, array, with=nil
  return nil if with == ''
  new_array = []
  array.size.times do |i|
    if (array[i].to_s=~/#{with}/) || (with.to_i==i+1)
      new_array << array[i]
      puts "\t#{new_array.size}: #{new_array.last.to_s}"
    end
  end
  new_array.empty? ? nil : (new_array.size==1 ? new_array.first : choice(say, new_array, request_value(say)))
end

#make_sure(words = '', default = 'Y', other = 'n') ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/cli_tools.rb', line 17

def make_sure words='', default='Y', other='n'
  answer = request_value(words+" [#{default}/#{other}]")
  return true if answer==''
  if answer.downcase == default.downcase or answer.downcase == other.downcase
    return answer.downcase == default.downcase
  else
    return make_sure(words, default, other)
  end
end

#request_value(words = nil) ⇒ Object



12
13
14
15
# File 'lib/cli_tools.rb', line 12

def request_value words=nil
  printf words+' ' if words
  $stdin.gets.chop
end