Class: Dogfood::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dogfood/cli.rb', line 7

def initialize(argv)
  @argv = argv
  @options = {
    output: default_output_path,
    days: 1,
    wo_per_day: (3..6),
    fleet_ratio: 0.25,
    seed: nil,
    scenario: nil
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/dogfood/cli.rb', line 19

def options
  @options
end

Instance Method Details

#parse!Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dogfood/cli.rb', line 71

def parse!
  i = 0
  while i < @argv.length
    arg = @argv[i]
    case arg
    when "--pack"
      @options[:pack] = @argv[i + 1]
      i += 1
    when "--scenario"
      @options[:scenario] = @argv[i + 1]
      i += 1
    when "--list", "-l"
      @options[:list] = true
    when "--explain"
      @options[:explain] = @argv[i + 1]
      i += 1
    when "--dry-run"
      @options[:dry_run] = true
    when "--random", "-r"
      @options[:seed] = Random.new_seed
    when /^--days(?:=(.+))?$/
      @options[:days] = ($1 || @argv[i + 1]).to_i
      i += 1 unless $1
    when /^--wo-per-day(?:=(.+))?$/
      @options[:wo_per_day] = parse_wo_per_day($1 || @argv[i + 1])
      i += 1 unless $1
    when /^--fleet-ratio(?:=(.+))?$/
      @options[:fleet_ratio] = ($1 || @argv[i + 1]).to_f
      i += 1 unless $1
    when /^--seed(?:=(.+))?$/
      @options[:seed] = ($1 || @argv[i + 1]).to_i
      i += 1 unless $1
    when /^--output(?:=(.+))?$/, "-o"
      @options[:output] = ($1 || @argv[i + 1])
      i += 1 unless $1
    when "--help", "-h"
      @options[:help] = true
    end
    i += 1
  end
end

#runObject



21
22
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dogfood/cli.rb', line 21

def run
  parse!

  if @options[:help]
    print_help
    return
  end

  unless @options[:pack]
    warn "Error: --pack PATH is required"
    print_help
    exit 1
  end

  load_pack(@options[:pack])

  if @options[:list]
    list_scenarios
    return
  end

  if @options[:explain]
    explain_file(@options[:explain])
    return
  end

  if @options[:dry_run] && @options[:scenario]
    explain_scenario(@options[:scenario])
    return
  end

  generator = Dogfood::DayGenerator.new(
    pack: Dogfood::Pack.current,
    days: @options[:days],
    wo_per_day: @options[:wo_per_day],
    fleet_ratio: @options[:fleet_ratio],
    seed: @options[:seed],
    scenario: @options[:scenario]
  )
  result = generator.generate

  output = Dogfood::Renderer.new(result).render
  FileUtils.mkdir_p(File.dirname(@options[:output]))
  File.write(@options[:output], output)

  puts "Generated dogfood: #{@options[:output]}"
  puts "Days: #{result[:days].length}"
  puts "Seed: #{result[:seed]}"
end