class PgOptionsParser

Public Class Methods

parse(args) click to toggle source

Parses the command-line arguments into a Hash object. Note that the members of the Hash have the same name as the ActiveRecord parameters (e.g., :host, :database, etc.). This Hash will actually be passed to ActiveRecord for consumption.

# File lib/so2pg.rb, line 123
  def self.parse(args)
    options = {}

    opts = OptionParser.new do |opts|
      opts.banner = <<-EOB
Imports a StackOverflow data dump into a PostgreSQL database.
Usage: so2pg [options]
      EOB

      opts.on("-H", "--host HOST", "The database host") do |host|
        options[:host] = host
      end

      opts.on("-d", "--database DBNAME", "The name of the database (REQUIRED)") do |dbname|
        options[:database] = dbname
      end

      opts.on("-D", "--directory DIRECTORY", "The data directory path (REQUIRED)") do |dir|
        options[:dir] = dir
      end

      opts.on("-u", "--user USER", "The user name") do |user|
        options[:username] = user
      end

      opts.on("-p", "--password PASSWORD", "The user's password") do |password|
        options[:password] = password
      end

      opts.on("-P", "--port PORT_NUMBER", "The port number") do |port|
        options[:port] = port
      end

      opts.on("-O", "--include-optionals", "Includes optional tables") do
        options[:optionals] = true
      end

      opts.on("-R", "--include-relationships", "Includes table relationships") do
        options[:relationships] = true
      end

      opts.on("-h", "--help", "Show this help screen") do |help|
        options[:help] = true
      end

    end

    opts.parse!(args)
    if(options[:help] or !options.has_key? :dir or !options.has_key? :database)
      puts opts.help
      nil
    else
      options
    end
  end