Class: Dogfood::CLI

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

Constant Summary collapse

UsageError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

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
112
113
114
115
# 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"
      val = @argv[i + 1]
      if val && !val.start_with?("-")
        @options[:explain] = val
        i += 1
      else
        @options[:explain] = true
      end
    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 /^--stories-per-day(?:=(.+))?$/
      @options[:stories_per_day] = parse_stories_per_day($1 || @argv[i + 1])
      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
    else
      @options[:scenario] ||= arg
    end
    i += 1
  end
end


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dogfood/cli.rb', line 117

def print_help
  puts <<~HELP
    Usage: dogfood [options] [scenario_name]

      --pack PATH              Ruby file that registers a Dogfood::Pack
                               (default: config/dogfood.rb in the current directory)
      --scenario NAME          Run a specific named scenario (otherwise: random mix)
      --days N                 Number of simulated days (default: 1)
      --stories-per-day N or M-N  Stories per day (default: 3-6)
      --seed N                 Set random seed for reproducibility
      --random                 Use a random seed (prints it)
      --output PATH, -o PATH   Output file path (default: tmp/dogfood/<timestamp>.md)
      --list, -l               List available scenarios
      --explain [NAME|FILE]    Print the stage/decision tree for a scenario name
                               or YAML file, no RNG
      --dry-run --scenario NAME  Same as --explain NAME
      --help, -h               Show help

    Examples:
      dogfood --list
      dogfood --explain supplemental_authorization
      dogfood supplemental_authorization --explain
      dogfood --scenario brake_repair_with_parts_order --days 3
  HELP
end

#runObject



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 22

def run
  parse!

  if @options[:help]
    print_help
    return
  end

  @options[:pack] ||= default_pack_path
  unless @options[:pack] && File.exist?(@options[:pack])
    raise UsageError, "no --pack given and no config/dogfood.rb found in the current directory"
  end

  load_pack(@options[:pack])

  if @options[:list]
    list_scenarios
    return
  end

  if @options[:explain]
    target = @options[:explain] == true ? @options[:scenario] : @options[:explain]
    explain(target)
    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],
    stories_per_day: @options[:stories_per_day],
    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