Class: Charts::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/charts/bin/opt_parser.rb

Constant Summary collapse

FORMATS =
[:txt, :svg, :png, :jpg, :gif].freeze
STYLES =
[:circle, :cross, :manikin, :bar, :pie].freeze
DATA_EXAMPLE_ARGS =
'-d 8,7'.freeze
COLOR_EXAMPLE_ARGS =
'--colors red,gold'.freeze
FLOAT_INTEGER_REGEX =
/^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ OptParser

Returns a new instance of OptParser.



12
13
14
15
16
17
18
19
20
21
# File 'lib/charts/bin/opt_parser.rb', line 12

def initialize(args)
  @args = args.empty? ? ['--help'] : args
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  @options = {
    type:  :svg,
    style: :circle
  }
  @parser = opt_parser
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/charts/bin/opt_parser.rb', line 4

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/charts/bin/opt_parser.rb', line 4

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



4
5
6
# File 'lib/charts/bin/opt_parser.rb', line 4

def parser
  @parser
end

Instance Method Details

#infer_type_from_filenameObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/charts/bin/opt_parser.rb', line 40

def infer_type_from_filename
  return if options[:help]
  return unless options[:filename]
  type = options[:filename].match(/.*\.(#{FORMATS.join("|")})/)
  if type
    options[:type] = type[1].to_sym
  else
    options[:type] = false
  end
end

#opt_parserObject



61
62
63
64
65
66
67
68
69
70
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
116
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/charts/bin/opt_parser.rb', line 61

def opt_parser
  OptionParser.new do |opts|
    opts.banner = 'Usage: bin/charts [options]'
    opts.on(
      '-d DATA',
      '--data DATA',
      Array,
      "Provide multiple data points, ie: 'bin/charts #{DATA_EXAMPLE_ARGS}'"
    ) do |data|
      data = data.map { |d| d.match(FLOAT_INTEGER_REGEX) ? Float(d) : nil }
      # if multiple --data arguments are provided,
      # the data option becomes a two dimensional array
      if !options[:data]
        options[:data] = data
      else
        options[:data] = [options[:data]] unless options[:data].first.is_a? Array
        options[:data].push(data)
      end
    end
    opts.on(
      '-f FILENAME',
      '--filename FILENAME',
      "Set the filename the result is stored in. Supported formats are: : #{FORMATS.join(', ')}"
    ) do |filename|
      options[:filename] = filename
    end
    opts.on(
      '-s STYLE',
      '--style STYLE',
      STYLES,
      "Choose the chart style: #{STYLES.join(', ')} (circle, cross and manikin are count charts)"
    ) do |style|
      options[:style] = style
    end
    opts.on(
      '-t TITLE',
      '--title TITLE',
      "Set the title"
    ) do |title|
      options[:title] = title
    end
    opts.on(
      '--labels LABELS',
      Array,
      "Set the labels to be used, ie: 'bin/charts #{DATA_EXAMPLE_ARGS} --labels Failures,Successes' "
    ) do |labels|
      options[:labels] = labels
    end
    opts.on(
      '--group-labels GROUP_LABELS',
      Array,
      "Set the group-labels to be used, ie: 'bin/charts #{DATA_EXAMPLE_ARGS} --style bar --group-labels Summer,Winter' "
    ) do |group_labels|
      options[:group_labels] = group_labels
    end
    opts.on(
      '--colors COLORS',
      Array,
      "Set the colors to be used, ie: 'bin/charts #{DATA_EXAMPLE_ARGS} #{COLOR_EXAMPLE_ARGS}' "
    ) do |colors|
      options[:colors] = colors
    end
    opts.on(
      '--columns COLUMNS',
      Integer,
      'Set number of columns'
    ) do |columns|
      options[:columns] = columns
    end
    opts.on(
      '-w WIDTH',
      '--width WIDTH (not for count charts)',
      Integer,
      'Sets the image width'
    ) do |width|
      options[:width] = width
    end
    opts.on(
      '-h HEIGHT',
      '--height HEIGHT (not for count charts)',
      Integer,
      'Sets the image height'
    ) do |height|
      options[:height] = height
    end
    opts.on(
      '--item-width WIDTH',
      Integer,
      'Sets the width of the individual item (count charts only)'
    ) do |item_width|
      options[:item_width] = item_width
    end
    opts.on(
      '--item-height HEIGHT',
      Integer,
      'Sets the height of the individual item (count charts only)'
    ) do |item_height|
      options[:item_height] = item_height
    end
    opts.on(
      '--type TYPE',
      FORMATS,
      "If no filename is provided, output is sent to STDOUT, choose the format: #{FORMATS.join(', ')}"
    ) do |type|
      options[:type] = type
    end
    opts.on(
      '--help',
      'Prints this help'
    ) do
      puts opts
      options[:help] = true
    end
    opts.on(
      '--background_color BACKGROUNDCOLOR',
      Array,
      "Set the backgroundcolor to be used, ie: 'bin/charts #{DATA_EXAMPLE_ARGS} --background_color Silver"
    ) do |background_color|
      options[:background_color] = background_color
    end
  end
end

#parseObject

Return a hash describing the options.



24
25
26
27
28
29
30
31
# File 'lib/charts/bin/opt_parser.rb', line 24

def parse
  parser.parse!(args) # this sets the options
  post_process_options
  infer_type_from_filename
  validate_options

  options
end

#post_process_optionsObject



33
34
35
36
37
38
# File 'lib/charts/bin/opt_parser.rb', line 33

def post_process_options
  # force 2-dimensional array for bar chart if only one data set is provided
  if options[:style] == :bar and !options[:data].first.is_a? Array
    options[:data] = [options[:data]]
  end
end

#validate_optionsObject



51
52
53
54
55
56
57
58
59
# File 'lib/charts/bin/opt_parser.rb', line 51

def validate_options
  return if options[:help]
  unless options[:type]
    raise 'No type provided. Set a type with --type flag or by setting a valid --filename'
  end
  unless options[:data]
    raise "No data provided. Please pass in data using the --data flag: 'bin/charts #{DATA_EXAMPLE_ARGS}'"
  end
end