Class: Bipm::Data::Importer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bipm/data/importer/cli.rb

Overview

Argument parsing and dispatch. The exe/bipm-fetch shim delegates here so the executable stays a one-liner and the CLI is fully testable.

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



13
14
15
# File 'lib/bipm/data/importer/cli.rb', line 13

def initialize(argv)
  @argv = argv
end

Instance Method Details

#parse(argv) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bipm/data/importer/cli.rb', line 34

def parse(argv)
  opts = Options.new(nil, nil, "data", false)
  parser = OptionParser.new do |p|
    p.banner = "Usage: bipm-fetch [options]"
    p.on("--body=ID", "Fetch a single body (e.g. cgpm, cipm)") { |v| opts.body_id = v.to_sym }
    p.on("--language=LANG", "Restrict to one language (en or fr)") do |v|
      opts.languages = [Language.find(v)]
    end
    p.on("--base-dir=DIR", "Output directory (default: data)") { |v| opts.base_dir = v }
    p.on("--fork", "Fork one process per body") { opts.fork = true }
  end
  parser.parse!(argv.dup)
  opts
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bipm/data/importer/cli.rb', line 17

def run
  opts = parse(@argv)
  if opts.body_id
    run_one(opts)
  else
    run_all(opts)
  end
  0
rescue Bodies::UnknownBodyError => e
  warn "error: #{e.message}"
  2
rescue => e
  warn "fatal: #{e.class}: #{e.message}"
  warn e.backtrace.first(5).join("\n")
  1
end