class Lcoveralls::OptionParser

Parses CLI options for the Lcoveralls application.

Public Instance Methods

parse!(args) click to toggle source

Destructively parse the command line arguments.

@params args [Array] Arguments to parse.

@return [Hash] parsed options.

# File lib/lcoveralls/option_parser.rb, line 30
def parse!(args)
  options =  {
    :color          => $stderr.isatty,
    :git            => 'git',
    :retry_count    => 0,
    :retry_interval => 10.0,
    :service        => File.basename($0),
    :severity       => Logger::INFO
  }

  if ENV.has_key? 'TRAVIS_JOB_NUMBER' then
    options[:service] = 'travis-ci'
    options[:job_id] = ENV['TRAVIS_JOB_ID']
  end

  parser = ::OptionParser.new do |o|
  o.banner = "Usage: #{o.program_name} [options] [tracefile(s)]"
  o.summary_width = 30
  o.separator ''

  o.separator 'Code / coveralls.io options:'
  o.on(      '--dryrun',      'Do not submit to coveralls.io' ) { options[:dryrun] = true }
  o.on(      '--export [PATH=stdout]', 'Export Coveralls job data') do |path|
    options[:export] = case path
    when 'stderr'; $stderr
    when 'stdout'; $stdout
    when nil ; $stdout
    else File.new(path, 'w')
    end
  end
  o.on('-r', '--root PATH',   'Set the path to the repo root') { |path|  options[:root]  = File.realpath(path) }
  o.on('-s', '--service NAME','Set coveralls service name')    { |name|  options[:service] = name }
  o.on('-t', '--token TOKEN', 'Set coveralls repo token')      { |token| options[:token] = token }
  o.separator ''

  o.separator 'Network options:'
  o.on('--timeout DURATION',
       'Timout, in seconds, for network open and read operations') do |duration|
    options[:timeout] = duration.to_f
  end
  o.on('--retry-count COUNT', 'Number of times to retry sending to coveralls.io') do |count|
    options[:retry_count] = count.to_i
  end
  o.on('--retry-interval DURATION', 'Time to wait between retries') do |count|
    options[:retry_interval] = count.to_f;
  end
  o.separator ''

  o.separator 'Stderr output options:'
  o.on(      '--[no-]color', 'Colorize output')  { |color| options[:color] = color }
  o.on('-d', '--debug',      'Enable debugging') { options[:severity] = Logger::DEBUG }
  o.on(      '--trace',      'Maximum output')   { options[:severity] = Logger::TRACE }
  o.on('-q', '--quiet',      'Show less output') { options[:severity] = options[:severity] + 1 }
  o.on('-v', '--verbose',    'Show more output') { options[:severity] = options[:severity] - 1 }
  o.separator ''

  o.separator 'Miscellaneous options:'
  o.on(      '--[no-]git PATH', 'Path to the git program') { |path| options[:git] = path }
  o.on('-h', '--help',    'Print usage text, then exit')     { puts o; exit }
  o.on(      '--version', 'Print version number, then exit') { puts VERSION.join('.'); exit }
  o.separator ''
  end

  begin
    parser.parse! args
    options
  rescue ::OptionParser::InvalidOption => e
    $stderr.puts parser
    $stderr.puts e
    exit(false)
  end
end