Class: Equalshares::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/equalshares/cli.rb,
lib/equalshares/cli/formatter.rb

Overview

Command-line interface: parse a .pb file and print the Method of Equal Shares outcome.

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

RULE_RUNNERS =

Each rule name maps to a runner (instance, params, progress) -> Result.

{
  "mes" => ->(instance, params, progress) { Compute.equal_shares(instance, params, progress: progress) },
  "phragmen" => ->(instance, params, progress) { Phragmen.sequential(instance, params, progress: progress) },
  "greedy" => ->(instance, params, _progress) { Greedy.utilitarian_welfare(instance, params) },
  "maximin" => ->(instance, params, progress) { Maximin.support(instance, params, progress: progress) }
}.freeze
RULES =
RULE_RUNNERS.keys.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv) ⇒ Object



10
11
12
# File 'lib/equalshares/cli.rb', line 10

def self.start(argv)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/equalshares/cli.rb', line 23

def run(argv)
  options = { format: "human", progress: false, rule: "mes" }
  param_opts = {}
  parser = build_parser(options, param_opts)
  files = parser.parse(argv)

  if files.length != 1
    warn parser.help
    return 2
  end

  params = Params.new(**param_opts)
  progress = options[:progress] ? ->(pct) { warn "\rComputing... #{pct}%" } : nil

  instance = Pabulib.parse_file(files.first)
  result = RULE_RUNNERS.fetch(options[:rule]).call(instance, params, progress)
  warn "" if options[:progress]

  print Formatter.new(instance, result).render(options[:format])
  0
rescue OptionParser::ParseError => e
  warn "Error: #{e.message}"
  2
rescue ParseError, ComputeError, Errno::ENOENT => e
  warn "Error: #{e.message}"
  1
end