Added CmdOptions struct

This commit is contained in:
kojix2 2020-12-14 15:54:54 +09:00
parent 5d1a4fdf3c
commit 0aa6de0de5
3 changed files with 72 additions and 57 deletions

View File

@ -10,27 +10,23 @@ module YouPlot
Data = Struct.new(:headers, :series) Data = Struct.new(:headers, :series)
class Command class Command
attr_accessor :params attr_accessor :command, :params, :options
attr_reader :data, :fmt, :parser attr_reader :data, :parser
def initialize(argv = ARGV) def initialize(argv = ARGV)
@argv = argv @argv = argv
@parser = Parser.new @parser = Parser.new
@command = nil
@params = nil
@options = nil
@backend = YouPlot::Backends::UnicodePlotBackend @backend = YouPlot::Backends::UnicodePlotBackend
end end
def run def run
parser.parse_options(@argv) parser.parse_options(@argv)
command = parser.command @command ||= parser.command
@params = parser.params @options ||= parser.options
delimiter = parser.delimiter @params ||= parser.params
transpose = parser.transpose
headers = parser.headers
pass = parser.pass
output = parser.output
fmt = parser.fmt
@encoding = parser.encoding
@debug = parser.debug
if command == :colors if command == :colors
@backend.colors(parser.color_names) @backend.colors(parser.color_names)
@ -41,52 +37,52 @@ module YouPlot
while (input = Kernel.gets(nil)) while (input = Kernel.gets(nil))
# Pass the input to subsequent pipelines # Pass the input to subsequent pipelines
case pass case @options[:pass]
when IO when IO
pass.print(input) @options[:pass].print(input)
else else
if pass if @options[:pass]
File.open(pass, 'w') do |f| File.open(@options[:pass], 'w') do |f|
f.print(input) f.print(input)
end end
end end
end end
@data = if @encoding @data = if @options[:encoding]
input2 = input.dup.force_encoding(@encoding).encode('utf-8') input2 = input.dup.force_encoding(@options[:encoding]).encode('utf-8')
DSVReader.input(input2, delimiter, headers, transpose) DSVReader.input(input2, @options[:delimiter], @options[:headers], @options[:transpose])
else else
DSVReader.input(input, delimiter, headers, transpose) DSVReader.input(input, @options[:delimiter], @options[:headers], @options[:transpose])
end end
pp @data if @debug pp @data if @options[:debug]
plot = case command plot = case command
when :bar, :barplot when :bar, :barplot
@backend.barplot(data, params, fmt) @backend.barplot(data, params, @options[:fmt])
when :count, :c when :count, :c
@backend.barplot(data, params, count: true) @backend.barplot(data, params, count: true)
when :hist, :histogram when :hist, :histogram
@backend.histogram(data, params) @backend.histogram(data, params)
when :line, :lineplot when :line, :lineplot
@backend.line(data, params, fmt) @backend.line(data, params, @options[:fmt])
when :lines, :lineplots when :lines, :lineplots
@backend.lines(data, params, fmt) @backend.lines(data, params, @options[:fmt])
when :scatter, :s when :scatter, :s
@backend.scatter(data, params, fmt) @backend.scatter(data, params, @options[:fmt])
when :density, :d when :density, :d
@backend.density(data, params, fmt) @backend.density(data, params, @options[:fmt])
when :box, :boxplot when :box, :boxplot
@backend.boxplot(data, params) @backend.boxplot(data, params)
else else
raise "unrecognized plot_type: #{command}" raise "unrecognized plot_type: #{command}"
end end
case output case @options[:output]
when IO when IO
plot.render(output) plot.render(@options[:output])
else else
File.open(output, 'w') do |f| File.open(@options[:output], 'w') do |f|
plot.render(f) plot.render(f)
end end
end end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
module YouPlot
class Command
CmdOptions = Struct.new(
:delimiter,
:transpose,
:headers,
:pass,
:output,
:fmt,
:encoding,
:color_names,
:debug,
keyword_init: true
)
end
end

View File

@ -1,29 +1,30 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'optparse' require 'optparse'
require_relative 'cmd_options'
require_relative 'plot_params' require_relative 'plot_params'
module YouPlot module YouPlot
class Command class Command
class Parser class Parser
attr_reader :command, :params, attr_reader :command, :options, :params
:delimiter, :transpose, :headers, :pass, :output, :fmt,
:color_names, :encoding, :debug
def initialize def initialize
@command = nil @command = nil
@options = CmdOptions.new(
delimiter: "\t",
transpose: false,
headers: nil,
pass: false,
output: $stderr,
fmt: 'xyy',
encoding: nil,
color_names: false,
debug: false
)
@params = PlotParams.new @params = PlotParams.new
@delimiter = "\t"
@transpose = false
@headers = nil
@pass = false
@output = $stderr
@fmt = 'xyy'
@encoding = nil
@color_names = false
@debug = false
end end
def create_default_parser def create_default_parser
@ -36,19 +37,19 @@ module YouPlot
opt.on('Common options:') opt.on('Common options:')
opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]', opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]',
'for inserting YouPlot in the middle of Unix pipes') do |v| 'for inserting YouPlot in the middle of Unix pipes') do |v|
@pass = v || $stdout @options[:pass] = v || $stdout
end end
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v| opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v|
@output = v @options[:output] = v
end end
opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v| opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
@delimiter = v @options[:delimiter] = v
end end
opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v| opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
@headers = v @options[:headers] = v
end end
opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v| opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
@transpose = v @options[:transpose] = v
end end
opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v| opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v|
params.title = v params.title = v
@ -81,10 +82,10 @@ module YouPlot
params.labels = v params.labels = v
end end
opt.on('--progress', TrueClass, 'progressive') do |v| opt.on('--progress', TrueClass, 'progressive') do |v|
@progressive = v @options[:progressive] = v
end end
opt.on('--encoding VAL', String, 'Specify the input encoding') do |v| opt.on('--encoding VAL', String, 'Specify the input encoding') do |v|
@encoding = v @options[:encoding] = v
end end
# Optparse adds the help option, but it doesn't show up in usage. # Optparse adds the help option, but it doesn't show up in usage.
# This is why you need the code below. # This is why you need the code below.
@ -93,7 +94,7 @@ module YouPlot
exit exit
end end
opt.on('--debug', TrueClass, 'print preprocessed data') do |v| opt.on('--debug', TrueClass, 'print preprocessed data') do |v|
@debug = v @options[:debug] = v
end end
yield opt if block_given? yield opt if block_given?
end end
@ -167,7 +168,7 @@ module YouPlot
params.xscale = v params.xscale = v
end end
parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
@fmt = v @options[:fmt] = v
end end
when :count, :c when :count, :c
@ -197,7 +198,7 @@ module YouPlot
params.ylim = v.take(2) params.ylim = v.take(2)
end end
parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
@fmt = v @options[:fmt] = v
end end
when :lineplots, :lines when :lineplots, :lines
@ -211,7 +212,7 @@ module YouPlot
params.ylim = v.take(2) params.ylim = v.take(2)
end end
parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
@fmt = v @options[:fmt] = v
end end
when :scatter, :s when :scatter, :s
@ -225,7 +226,7 @@ module YouPlot
params.ylim = v.take(2) params.ylim = v.take(2)
end end
parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
@fmt = v @options[:fmt] = v
end end
when :density, :d when :density, :d
@ -239,7 +240,7 @@ module YouPlot
params.ylim = v.take(2) params.ylim = v.take(2)
end end
parser.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| parser.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
@fmt = v @options[:fmt] = v
end end
when :boxplot, :box when :boxplot, :box
@ -249,7 +250,7 @@ module YouPlot
when :colors when :colors
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v| parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
@color_names = v @options[:color_names] = v
end end
else else