class PutText::Cmdline

Constants

USAGE_TEXT

Public Class Methods

run(args) click to toggle source

Run the commmand line tool puttext. @param [Array<String>] args the command line arguments.

# File lib/puttext/cmdline.rb, line 12
def self.run(args)
  new.run(args)
end

Public Instance Methods

run(args) click to toggle source

Run the commmand line tool puttext. @param [Array<String>] args the command line arguments.

# File lib/puttext/cmdline.rb, line 18
def run(args)
  options = parse_args(args)
  po_file = Extractor.new.extract(options[:path])

  if options[:output_file]
    with_output_file(options[:output_file]) { |f| po_file.write_to(f) }
  else
    po_file.write_to(STDOUT)
  end
rescue => e
  error(e)
end

Private Instance Methods

error(exception) click to toggle source
# File lib/puttext/cmdline.rb, line 39
def error(exception)
  puts "error: #{exception.message}"
  puts exception.backtrace
  exit 1
end
parse_args(args) click to toggle source
# File lib/puttext/cmdline.rb, line 45
def parse_args(args)
  args_left, options = parse_options(args)

  if args_left.length != 1
    puts USAGE_TEXT
    exit 1
  else
    options[:path] = args_left[0]
    options
  end
end
parse_options(args) click to toggle source
# File lib/puttext/cmdline.rb, line 57
def parse_options(args)
  options = {}

  args_left = OptionParser.new do |opts|
    opts.banner = USAGE_TEXT

    opts.on('-o', '--output PATH', 'Output file path') do |o|
      options[:output_file] = o
    end
  end.parse!(args)

  [args_left, options]
end
with_output_file(path) { |f| ... } click to toggle source
# File lib/puttext/cmdline.rb, line 33
def with_output_file(path)
  File.open(path, 'w') do |f|
    yield(f)
  end
end