Class: Charts::OptParser
- Inherits:
-
Object
- Object
- Charts::OptParser
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
- #infer_type_from_filename ⇒ Object
-
#initialize(args) ⇒ OptParser
constructor
A new instance of OptParser.
- #opt_parser ⇒ Object
-
#parse ⇒ Object
Return a hash describing the options.
- #post_process_options ⇒ Object
- #validate_options ⇒ Object
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. = { type: :svg, style: :circle } @parser = opt_parser end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/charts/bin/opt_parser.rb', line 4 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/charts/bin/opt_parser.rb', line 4 def end |
#parser ⇒ Object (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_filename ⇒ Object
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 [:help] return unless [:filename] type = [:filename].match(/.*\.(#{FORMATS.join("|")})/) if type [:type] = type[1].to_sym else [:type] = false end end |
#opt_parser ⇒ Object
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. = '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 ![:data] [:data] = data else [:data] = [[:data]] unless [:data].first.is_a? Array [: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| [: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| [:style] = style end opts.on( '-t TITLE', '--title TITLE', "Set the title" ) do |title| [: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| [: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| [: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| [:colors] = colors end opts.on( '--columns COLUMNS', Integer, 'Set number of columns' ) do |columns| [:columns] = columns end opts.on( '-w WIDTH', '--width WIDTH (not for count charts)', Integer, 'Sets the image width' ) do |width| [:width] = width end opts.on( '-h HEIGHT', '--height HEIGHT (not for count charts)', Integer, 'Sets the image height' ) do |height| [:height] = height end opts.on( '--item-width WIDTH', Integer, 'Sets the width of the individual item (count charts only)' ) do |item_width| [:item_width] = item_width end opts.on( '--item-height HEIGHT', Integer, 'Sets the height of the individual item (count charts only)' ) do |item_height| [: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| [:type] = type end opts.on( '--help', 'Prints this help' ) do puts opts [: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| [:background_color] = background_color end end end |
#parse ⇒ Object
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 infer_type_from_filename end |
#post_process_options ⇒ Object
33 34 35 36 37 38 |
# File 'lib/charts/bin/opt_parser.rb', line 33 def # force 2-dimensional array for bar chart if only one data set is provided if [:style] == :bar and ![:data].first.is_a? Array [:data] = [[:data]] end end |
#validate_options ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/charts/bin/opt_parser.rb', line 51 def return if [:help] unless [:type] raise 'No type provided. Set a type with --type flag or by setting a valid --filename' end unless [:data] raise "No data provided. Please pass in data using the --data flag: 'bin/charts #{DATA_EXAMPLE_ARGS}'" end end |