Sha256: a78fff52357a84d896b0a3ffd304f3f22afc55bc3543b0779204c66c71e256dd

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

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


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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proutils-0.3.0 work/icli/utils/consoleutils.rb
proutils-0.3.1 work/icli/utils/consoleutils.rb