Class: Depager::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/depager/cli.rb

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/depager/cli.rb', line 7

def run(argv)
  output_file_name = nil

  OptionParser.new do |opt|
    opt.banner = "Usage: depager [options] [file]"
    opt.version = Depager::VERSION
    opt.on("-v", "Enable verbose mode") { Depager.configuration[:verbose] = true }
    opt.on("-g[FLAGS]", "Enable debug mode") { |v| Depager.configuration[:debug] = v || "sgc" }
    opt.on("-I DIRECTORY", "Add the directory to $LOAD_PATH") { |v| $LOAD_PATH.unshift(v) }
    opt.on("-o OUTPUT", "Set the output file name") { |v| output_file_name = v }
    opt.parse!(argv)
  end

  d_parser = Depager::DeclarationPartParser.new
  result   = d_parser.parse(argv[0] ? File.open(argv[0]) : $stdin)

  if output_file_name
    File.write(output_file_name, result)
  else
    $stdout.write result
  end
  0
rescue Depager::ErrorExit
  1
rescue SystemExit => e
  e.status
rescue Exception => e # rubocop:disable Lint/RescueException
  warn "#{File.basename $PROGRAM_NAME}: fatal error."
  warn "| #{e}"
  warn "| - #{d_parser.input_path}:#{d_parser.file.lineno}" if d_parser
  warn "| ( #{File.basename $1}:#{$2} )" if e.backtrace[0] =~ /^(.+):(\d+)(:.+)?$/
  raise if Depager.debug_mode?

  warn "Try -g option if you want to see more error infomation."
  2
end