module Ratch module ConsoleUtils # Convenient method to get simple console reply. def ask(question, answers=nil) print "#{question}" print " [#{answers}] " if answers until inp = $stdin.gets ; sleep 1 ; end inp end # Ask for a password. (FIXME: only for unix so far) def password(prompt=nil) msg ||= "Enter Password: " inp = '' print "#{prompt} " begin system "stty -echo" #inp = gets.chomp until inp = $stdin.gets sleep 1 end ensure system "stty echo" end return inp.chomp end end end class Array # Convert an array into command line parameters. # The array is accepted in the format of Ruby # method arguments --ie. [arg1, arg2, ..., hash] def to_params flags = (Hash===last ? pop : {}) flags = flags.collect do |f,v| m = f.to_s.size == 1 ? '-' : '--' case v when Array v.collect{ |e| "#{m}#{f} '#{e}'" }.join(' ') when true "#{m}#{f}" when false, nil '' else "#{m}#{f} '#{v}'" end end return (flags + self).join(" ") end # Not empty? def not_empty? !empty? end end class Hash # Setup a console arguments vector from a set of config options. def command_vector(args_field=nil) config = dup if args_field args = [config.delete(args_field)].flatten.compact else args = [] end args << config return args end end