Class: YouPlot::Parser
- Inherits:
-
Object
- Object
- YouPlot::Parser
- Defined in:
- lib/youplot/parser.rb
Overview
Class for parsing command line options
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#main_parser ⇒ Object
readonly
Returns the value of attribute main_parser.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#sub_parser ⇒ Object
readonly
Returns the value of attribute sub_parser.
Instance Method Summary collapse
- #apply_config_file ⇒ Object
- #config_file_candidate_paths ⇒ Object
- #configure ⇒ Object
- #create_base_parser ⇒ Object
- #create_main_parser ⇒ Object
- #create_sub_parser ⇒ Object
- #find_config_file ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse_options(argv = ARGV) ⇒ Object
- #read_config_file ⇒ Object
- #show_config_info ⇒ Object
- #show_main_help(out = $stdout) ⇒ Object
- #sub_parser_add_canvas ⇒ Object
- #sub_parser_add_fmt_xyxy ⇒ Object
- #sub_parser_add_fmt_yx ⇒ Object
- #sub_parser_add_grid ⇒ Object
- #sub_parser_add_symbol ⇒ Object
- #sub_parser_add_xlim ⇒ Object
- #sub_parser_add_xscale ⇒ Object
- #sub_parser_add_ylim ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/youplot/parser.rb', line 15 def initialize @command = nil @options = Options.new( "\t", # elimiter: false, # transpose: nil, # headers: false, # pass: $stderr, # output: 'xyy', # fmt: false, # progressive: nil, # encoding: false, # color_names: false # debug: ) @params = Parameters.new end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def command @command end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def config @config end |
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def config_file @config_file end |
#main_parser ⇒ Object (readonly)
Returns the value of attribute main_parser.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def main_parser @main_parser end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def params @params end |
#sub_parser ⇒ Object (readonly)
Returns the value of attribute sub_parser.
11 12 13 |
# File 'lib/youplot/parser.rb', line 11 def sub_parser @sub_parser end |
Instance Method Details
#apply_config_file ⇒ Object
34 35 36 37 38 39 |
# File 'lib/youplot/parser.rb', line 34 def apply_config_file return if !config_file && find_config_file.nil? read_config_file configure end |
#config_file_candidate_paths ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/youplot/parser.rb', line 41 def config_file_candidate_paths # keep the order of the paths paths = [] paths << ENV['MYYOUPLOTRC'] if ENV['MYYOUPLOTRC'] paths << '.youplot.yml' paths << '.youplotrc' if ENV['HOME'] paths << File.join(ENV['HOME'], '.youplotrc') paths << File.join(ENV['HOME'], '.youplot.yml') paths << File.join(ENV['HOME'], '.config', 'youplot', 'youplotrc') paths << File.join(ENV['HOME'], '.config', 'youplot', 'youplot.yml') end paths end |
#configure ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/youplot/parser.rb', line 73 def configure option_members = @options.members param_members = @params.members # It would be more useful to be able to configure by plot type config.each do |k, v| k = k.to_sym if option_members.include?(k) @options[k] ||= v elsif param_members.include?(k) @params[k] ||= v else raise Error, "Unknown option/param in config file: #{k}" end end end |
#create_base_parser ⇒ Object
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 |
# File 'lib/youplot/parser.rb', line 89 def create_base_parser OptionParser.new do |parser| parser.program_name = 'YouPlot' parser.version = YouPlot::VERSION parser.summary_width = 23 parser.on_tail('') # Add a blank line at the end parser.separator('') parser.on('Common options:') parser.on('-O', '--pass [FILE]', 'file to output input data to [stdout]', 'for inserting YouPlot in the middle of Unix pipes') do |v| [:pass] = v || $stdout end parser.on('-o', '--output [FILE]', 'file to output plots to [stdout]', 'If no option is specified, plot will print to stderr') do |v| [:output] = v || $stdout end parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v| [:delimiter] = v end parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v| [:headers] = v end parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v| [:transpose] = v end parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v| params.title = v end parser.on('--xlabel STR', String, 'print string on the bottom of the plot') do |v| params.xlabel = v end parser.on('--ylabel STR', String, 'print string on the far left of the plot') do |v| params.ylabel = v end parser.on('-w', '--width INT', Numeric, 'number of characters per row') do |v| params.width = v end parser.on('-h', '--height INT', Numeric, 'number of rows') do |v| params.height = v end = UnicodePlot::BORDER_MAP.keys.join(', ') parser.on('-b', '--border STR', String, 'specify the style of the bounding box', "(#{})") do |v| params.border = v.to_sym end parser.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v| params.margin = v end parser.on('--padding INT', Numeric, 'space of the left and right of the plot') do |v| params.padding = v end parser.on('-c', '--color VAL', String, 'color of the drawing') do |v| params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym end parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v| params.labels = v end parser.on('-p', '--progress', TrueClass, 'progressive mode [experimental]') do |v| [:progressive] = v end parser.on('-C', '--color-output', TrueClass, 'colorize even if writing to a pipe') do |_v| UnicodePlot::IOContext.define_method(:color?) { true } # FIXME end parser.on('-M', '--monochrome', TrueClass, 'no colouring even if writing to a tty') do |_v| UnicodePlot::IOContext.define_method(:color?) { false } # FIXME end parser.on('--encoding STR', String, 'specify the input encoding') do |v| [:encoding] = v end # Optparse adds the help option, but it doesn't show up in usage. # This is why you need the code below. parser.on('--help', 'print sub-command help menu') do puts parser.help exit if YouPlot.run_as_executable? end parser.on('--config FILE', 'specify a config file') do |v| @config_file = v end parser.on('--debug', TrueClass, 'print preprocessed data') do |v| [:debug] = v end # yield opt if block_given? end end |
#create_main_parser ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/youplot/parser.rb', line 173 def create_main_parser @main_parser = create_base_parser main_parser. = \ <<~MSG Program: YouPlot (Tools for plotting on the terminal) Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION}) Source: https://github.com/red-data-tools/YouPlot Usage: uplot <command> [options] <in.tsv> Commands: barplot bar draw a horizontal barplot histogram hist draw a horizontal histogram lineplot line draw a line chart lineplots lines draw a line chart with multiple series scatter s draw a scatter plot density d draw a density plot boxplot box draw a horizontal boxplot count c draw a barplot based on the number of occurrences (slow) colors color show the list of available colors General options: --config print config file info --help print command specific help menu --version print the version of YouPlot MSG # Help for the main parser is simple. # Simply show the banner above. main_parser.on('--help', 'print sub-command help menu') do show_main_help end main_parser.on('--config', 'show config file info') do show_config_info end end |
#create_sub_parser ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/youplot/parser.rb', line 291 def create_sub_parser @sub_parser = create_base_parser sub_parser. = \ <<~MSG Usage: YouPlot #{command} [options] <in.tsv> Options for #{command}: MSG case command # If you type only `uplot` in the terminal. # Output help to standard error output. when nil show_main_help($stderr) # Output help to standard output. when :help show_main_help when :barplot, :bar sub_parser_add_symbol sub_parser_add_fmt_yx sub_parser_add_xscale when :count, :c sub_parser.on_head('-r', '--reverse', TrueClass, 'reverse the result of comparisons') do |v| .reverse = v end sub_parser_add_symbol sub_parser_add_xscale when :histogram, :hist sub_parser_add_symbol sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v| params.closed = v end sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v| params.nbins = v end when :lineplot, :line, :l sub_parser_add_canvas sub_parser_add_grid sub_parser_add_fmt_yx sub_parser_add_ylim sub_parser_add_xlim when :lineplots, :lines, :ls sub_parser_add_canvas sub_parser_add_grid sub_parser_add_fmt_xyxy sub_parser_add_ylim sub_parser_add_xlim when :scatter, :s sub_parser_add_canvas sub_parser_add_grid sub_parser_add_fmt_xyxy sub_parser_add_ylim sub_parser_add_xlim when :density, :d sub_parser_add_canvas sub_parser_add_grid sub_parser_add_fmt_xyxy sub_parser_add_ylim sub_parser_add_xlim when :boxplot, :box sub_parser_add_xlim when :colors, :color, :colours, :colour sub_parser.on_head('-n', '--names', TrueClass, 'show color names only') do |v| [:color_names] = v end # Currently it simply displays the configuration file, # but in the future this may be changed to open a text editor like Vim # to edit the configuration file. when :config show_config_info else = "YouPlot: unrecognized command '#{command}'" raise Error, unless YouPlot.run_as_executable? warn exit 1 end end |
#find_config_file ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/youplot/parser.rb', line 56 def find_config_file config_file_candidate_paths.each do |file| path = File.(file) next unless File.exist?(path) @config_file = path ENV['MYYOUPLOTRC'] = path return @config_file end nil end |
#parse_options(argv = ARGV) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/youplot/parser.rb', line 385 def (argv = ARGV) begin create_main_parser.order!(argv) rescue OptionParser::ParseError => e warn "YouPlot: #{e.}" exit 1 if YouPlot.run_as_executable? end @command = argv.shift&.to_sym begin create_sub_parser&.parse!(argv) rescue OptionParser::ParseError => e warn "YouPlot: #{e.}" exit 1 if YouPlot.run_as_executable? end begin apply_config_file rescue StandardError => e warn "YouPlot: #{e.}" exit 1 if YouPlot.run_as_executable? end end |
#read_config_file ⇒ Object
68 69 70 71 |
# File 'lib/youplot/parser.rb', line 68 def read_config_file require 'yaml' @config = YAML.load_file(config_file) end |
#show_config_info ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/youplot/parser.rb', line 219 def show_config_info if ENV['MYYOUPLOTRC'] puts "config file : #{ENV['MYYOUPLOTRC']}" puts config.inspect else puts <<~EOS Configuration file not found. It should be a YAML file, like this example: width : 40 height : 20 By default, YouPlot will look for the configuration file in these locations: #{config_file_candidate_paths.map { |s| ' ' + s }.join("\n")} If you have the file elsewhere, you can specify its location with the `MYYOUPLOTRC` environment variable. EOS end exit if YouPlot.run_as_executable? end |
#show_main_help(out = $stdout) ⇒ Object
213 214 215 216 217 |
# File 'lib/youplot/parser.rb', line 213 def show_main_help(out = $stdout) out.puts main_parser. out.puts exit if YouPlot.run_as_executable? end |
#sub_parser_add_canvas ⇒ Object
250 251 252 253 254 255 |
# File 'lib/youplot/parser.rb', line 250 def sub_parser_add_canvas canvas_types = UnicodePlot::Canvas::CANVAS_CLASS_MAP.keys.join(', ') sub_parser.on_head('--canvas STR', String, 'type of canvas', "(#{canvas_types})") do |v| params.canvas = v.to_sym end end |
#sub_parser_add_fmt_xyxy ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/youplot/parser.rb', line 275 def sub_parser_add_fmt_xyxy sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| [:fmt] = v end end |
#sub_parser_add_fmt_yx ⇒ Object
283 284 285 286 287 288 289 |
# File 'lib/youplot/parser.rb', line 283 def sub_parser_add_fmt_yx sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| [:fmt] = v end end |
#sub_parser_add_grid ⇒ Object
269 270 271 272 273 |
# File 'lib/youplot/parser.rb', line 269 def sub_parser_add_grid sub_parser.on_head('--[no-]grid', TrueClass, 'draws grid-lines at the origin') do |v| params.grid = v end end |
#sub_parser_add_symbol ⇒ Object
237 238 239 240 241 |
# File 'lib/youplot/parser.rb', line 237 def sub_parser_add_symbol sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v| params.symbol = v end end |
#sub_parser_add_xlim ⇒ Object
257 258 259 260 261 |
# File 'lib/youplot/parser.rb', line 257 def sub_parser_add_xlim sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.map(&:to_f) end end |
#sub_parser_add_xscale ⇒ Object
243 244 245 246 247 248 |
# File 'lib/youplot/parser.rb', line 243 def sub_parser_add_xscale = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ') sub_parser.on_head('--xscale STR', String, "axis scaling (#{})") do |v| params.xscale = v.to_sym end end |
#sub_parser_add_ylim ⇒ Object
263 264 265 266 267 |
# File 'lib/youplot/parser.rb', line 263 def sub_parser_add_ylim sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.map(&:to_f) end end |