Class: Yardstick::CLI

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

Overview

Handle the yardstick command line interface

Class Method Summary collapse

Class Method Details

.display_exit(message) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Display a message and exit



68
69
70
71
# File 'lib/yardstick/cli.rb', line 68

def self.display_exit(message)
  puts message.to_str
  exit
end

.option_parser(_options) ⇒ Yardstick::OptionParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return an OptionParser instance for the command-line app



53
54
55
56
57
58
# File 'lib/yardstick/cli.rb', line 53

def self.option_parser(_options)
  opts = OptionParser.new
  opts.on_tail('-v', '--version', 'print version information and exit') { display_exit("#{opts.program_name} #{Yardstick::VERSION}") }
  opts.on_tail('-h', '--help',    'display this help and exit')         { display_exit(opts.to_s) }
  opts
end

.parse_config(args) ⇒ Config

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse the options provided from the command-line



35
36
37
38
39
40
41
42
# File 'lib/yardstick/cli.rb', line 35

def self.parse_config(args)
  args << '--help' if args.empty?
  options = {}
  option_parser(options).parse!(args)
  Config.new(options.merge(:path => args))
rescue OptionParser::InvalidOption => error
  display_exit(error.message)
end

.run(*args) ⇒ Yardstick::MeasurementSet

Parse the command line options, and run the command

Examples:

Yardstick::CLI.run(%w[ article.rb ])  # => [ Measurement ]


20
21
22
23
24
# File 'lib/yardstick/cli.rb', line 20

def self.run(*args)
  measurements = Yardstick.measure(parse_config(args))
  measurements.puts
  measurements
end