Sha256: 8502bdc133c5b4648a9435d5f070a1fc870046b53b228db3cf84307db3435b4b

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ratch-0.2.1 lib/ratch/consoleutils.rb
ratch-0.2.2 lib/ratch/consoleutils.rb
ratch-0.1 lib/ratch/consoleutils.rb