class FlattenDB::CLI

Constants

TYPES

Attributes

type[R]

Public Class Methods

defaults() click to toggle source
Calls superclass method
# File lib/flattendb/cli.rb, line 60
def defaults
  super.merge(
    :input  => '-',
    :inputs => [],
    :output => '-',
    :config => 'config.yaml'
  )
end
execute(type = nil, *args) click to toggle source
# File lib/flattendb/cli.rb, line 69
def execute(type = nil, *args)
  new(nil, type).execute(*args)
end

Public Instance Methods

run(arguments) click to toggle source
# File lib/flattendb/cli.rb, line 77
def run(arguments)
  if type
    require "flattendb/types/#{type}"
  else
    quit 'Database type is required!'
  end

  options[:input] = if type == :mdb
    if options[:inputs].empty?
      if arguments.empty?
        options[:inputs] << options[:input]
      else
        options[:inputs].concat(arguments)
        arguments.clear
      end
    end

    options[:inputs].map! { |file| open_file_or_std(file) }
  else
    open_file_or_std(
      options[:inputs].last || arguments.shift || options[:input]
    )
  end

  quit unless arguments.empty?

  options[:output] = open_file_or_std(options[:output], true)

  FlattenDB[type].to_flat!(options)
end

Private Instance Methods

init(type) click to toggle source
Calls superclass method
# File lib/flattendb/cli.rb, line 110
def init(type)
  super()
  self.type = type
end
option_parser() click to toggle source
Calls superclass method
# File lib/flattendb/cli.rb, line 124
def option_parser
  @sorted_types = TYPES.keys.sort_by { |t| t.to_s }
  super
ensure
  remove_instance_variable(:@sorted_types)
end
opts(opts) click to toggle source
# File lib/flattendb/cli.rb, line 138
def opts(opts)
  unless type
    opts.on('-t', '--type TYPE', 'Type of database [REQUIRED]') { |type|
      self.type = type
    }

    opts.separator ''
  end

  opts.on('-i', '--input FILE', 'Input file(s) [Default: STDIN]') { |input|
    (options[:inputs] ||= []) << input
  }

  opts.on('-o', '--output FILE', 'Output file (flat XML) [Default: STDOUT]') { |output|
    options[:output] = output
  }

  opts.on('-c', '--config FILE', "Configuration file (YAML) [Default: #{defaults[:config]}#{' (currently not present)' unless File.readable?(defaults[:config])}]") { |config|
    options[:config] = config
  }

  opts.separator ''
  opts.separator 'Database-specific options:'

  type ? type_options(opts) : @sorted_types.each { |t| type_options(opts, true, t) }
end
post_opts(opts) click to toggle source
# File lib/flattendb/cli.rb, line 165
def post_opts(opts)
  unless type
    opts.separator ''
    opts.separator "Supported database types: #{@sorted_types.map { |t| "#{t} (#{TYPES[t][:title]})" }.join(', ')}."
  end
end
pre_opts(opts) click to toggle source
# File lib/flattendb/cli.rb, line 131
def pre_opts(opts)
  if type
    opts.separator ''
    opts.separator "TYPE = #{type} (#{TYPES[type][:title]})"
  end
end
type=(type) click to toggle source
# File lib/flattendb/cli.rb, line 115
def type=(type)
  if type
    @type = type.to_s.downcase.to_sym
    quit "Database type not supported: #{type}" unless TYPES.has_key?(@type)
  else
    @type = nil
  end
end
type_options(opts, heading = false, type = type) click to toggle source
# File lib/flattendb/cli.rb, line 172
def type_options(opts, heading = false, type = type)
  cfg = TYPES[type]

  if heading
    opts.separator ''
    opts.separator " - [#{type}] #{cfg[:title]}"
  end

  cfg[:opts][opts, options]
end