class TraceLocation::CLI

Attributes

argv[R]

Public Class Methods

new(argv) click to toggle source
# File lib/trace_location/cli.rb, line 7
def initialize(argv)
  @argv = argv
end

Public Instance Methods

run() click to toggle source
# File lib/trace_location/cli.rb, line 11
def run
  opt = OptionParser.new
  opt.on('-f FORMAT', '--format=FORMAT', 'Report format (default: :md)', &:to_sym)
  opt.on('-m REGEXP', '--match=REGEXP') { |str| Regexp.new(str) }
  opt.on('-i REGEXP', '--ignore=REGEXP') { |str| Regexp.new(str) }
  opt.on('-d DIR', '--dest-dir=DIR')
  opt.on('-e code')

  params = {}
  opt.order!(argv, into: params)
  params.transform_keys! { |k| k.to_s.gsub('-', '_').to_sym }

  if code = params.delete(:e)
    exec_code code, params
  else
    file = argv.shift
    unless file
      puts opt.help
      exit 1
    end

    exec_command file, params
  end
end

Private Instance Methods

exec_code(code, params) click to toggle source
# File lib/trace_location/cli.rb, line 53
def exec_code(code, params)
  TraceLocation.trace(params) do
    eval code
  end
end
exec_command(cmd, params) click to toggle source
# File lib/trace_location/cli.rb, line 38
def exec_command(cmd, params)
  path =
    if File.exist?(cmd)
      cmd
    else
      `which #{cmd}`.chomp
    end

  $PROGRAM_NAME = cmd

  TraceLocation.trace(params) do
    load path
  end
end