Class: Statkit::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



6
7
8
# File 'lib/statkit/cli.rb', line 6

def initialize()
  setup_parser
end

Instance Method Details

#do_parse(argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/statkit/cli.rb', line 20

def do_parse(argv)
  argv = argv.dup
  @parser.parse!(argv)
  @args = argv

  if @args.size < 1
    $stderr.puts("[ERROR] STAT_SPEC is not specified.")
    $stderr.puts("")
    puts(@parser.help)
    exit(false)
  end

  @stat_spec = @args.first

  true
end

#run(argv) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/statkit/cli.rb', line 37

def run(argv)
  do_parse(argv)

  exps = @stat_spec.split().map do |spec|
    case spec
    when "avg"
      ::Statkit::Spec::AvgFuncExp.new
    when "stdev"
      ::Statkit::Spec::StdevFuncExp.new
    when "min"
      ::Statkit::Spec::MinFuncExp.new
    when "max"
      ::Statkit::Spec::MaxFuncExp.new
    else
      raise RuntimeError.new("Unknown function: #{spec}")
    end
  end

  $stdin.each_line do |line|
    val = Float(line)

    exps.each do |exp|
      exp.add_input(val)
    end
  end

  puts(exps.map(&:evaluate).join("\t"))

  true
end

#setup_parserObject



10
11
12
13
14
15
16
17
18
# File 'lib/statkit/cli.rb', line 10

def setup_parser()
  @parser = OptionParser.new
  @parser.banner = "Usage: statkit [options] STAT_SPEC"

  @parser.on('-h', '--help', 'Show this help.') do
    puts(@parser.help)
    exit(true)
  end
end