Class: Perfer::CLI

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

Constant Summary collapse

COMMANDS =
%w[
  config
  graph
  help
  report
  results
  run
]
HELP =
<<-EOS
Usage:
  perfer <command> [options] arguments

Commands:
  run files+    - run with current ruby
  report files+ - show the results
  config reset  - reset the configuration file to the defaults (or create it)
  help          - show this help
  results
    path files+      - show the paths to the result files
    rm,delete files+ - remove the result files
    rewrite files+   - rewrite the result files in the latest format

<files+> are a set of benchmark files

Common options:
EOS

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



31
32
33
34
35
36
37
38
# File 'lib/perfer/cli.rb', line 31

def initialize(argv)
  @argv = argv

  @opts = OptionParser.new do |options|
    options.banner = HELP
    common_options(options)
  end
end

Instance Method Details

#common_options(options) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/perfer/cli.rb', line 112

def common_options(options)
  options.on('-t TIME', Float, "Minimal time to run (greater usually improve accuracy)") do |t|
    error "Minimal time must be > 0" if t <= 0
    Perfer.configuration.minimal_time = t
  end
  options.on('-m N', Integer, "Numbers of measurements per job") do |n|
    error "There must be at least 2 measurements" if n < 2
    Perfer.configuration.measurements = n
  end
  options.on('-v', "Verbose") do
    Perfer.configuration.verbose = true
  end
  options.on('-h', '--help', "Show this help") do
    puts options.help
    exit
  end
end

#configObject



103
104
105
106
107
108
109
110
# File 'lib/perfer/cli.rb', line 103

def config
  case subcommand = @argv.shift
  when "reset"
    Perfer.configuration.write_defaults
  else
    unknown_subcommand subcommand
  end
end

#error(message) ⇒ Object



62
63
64
65
66
# File 'lib/perfer/cli.rb', line 62

def error message
  $stderr.puts message
  $stderr.puts
  abort @opts.help
end

#executeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/perfer/cli.rb', line 40

def execute
  begin
    @opts.order!(@argv)
  rescue OptionParser::ParseError => e
    error e.message
  end

  @command = @argv.shift
  error "A command must be given, one of: #{COMMANDS*', '}" unless @command
  error "Unknown command: #{@command.inspect}" unless COMMANDS.include? @command

  send(@command)
end

#graphObject



97
98
99
100
101
# File 'lib/perfer/cli.rb', line 97

def graph
  each_session { |session|
    session.graph
  }
end

#helpObject



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

def help
  puts @opts.help
end

#reportObject



72
73
74
75
# File 'lib/perfer/cli.rb', line 72

def report
  measurements = (@argv.shift if @argv.first == '--measurements')
  each_session { |session| session.report(:measurements => measurements) }
end

#resultsObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/perfer/cli.rb', line 84

def results
  case subcommand = @argv.shift
  when "path"
    each_session { |session| puts session.store.file }
  when "delete", "rm"
    each_session { |session| session.store.delete }
  when "rewrite"
    each_session { |session| session.store.rewrite }
  else
    unknown_subcommand subcommand
  end
end

#runObject



77
78
79
80
81
82
# File 'lib/perfer/cli.rb', line 77

def run
  # load files
  files.each do |file|
    require file.path
  end
end

#unknown_subcommand(subcommand) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/perfer/cli.rb', line 54

def unknown_subcommand(subcommand)
  if subcommand
    error "Unknown subcommand for #{@command}: #{subcommand.inspect}"
  else
    error "`perfer #{@command}` needs a subcommand"
  end
end