Allows you to specify the output file

This commit is contained in:
kojix2 2020-09-29 18:13:03 +09:00
parent 3aceae9279
commit ccfbaa7bde
2 changed files with 43 additions and 24 deletions

View File

@ -25,6 +25,7 @@ module Uplot
delimiter = parser.delimiter delimiter = parser.delimiter
transpose = parser.transpose transpose = parser.transpose
headers = parser.headers headers = parser.headers
pass = parser.pass
output = parser.output output = parser.output
count = parser.count count = parser.count
fmt = parser.fmt fmt = parser.fmt
@ -41,28 +42,42 @@ module Uplot
@raw_inputs << input @raw_inputs << input
@data = Preprocessing.input(input, delimiter, headers, transpose) @data = Preprocessing.input(input, delimiter, headers, transpose)
pp @data if @debug pp @data if @debug
case command plot = case command
when :bar, :barplot when :bar, :barplot
Plot.barplot(data, params, @count) Plot.barplot(data, params, @count)
when :count, :c when :count, :c
Plot.barplot(data, params, count = true) Plot.barplot(data, params, count = true)
when :hist, :histogram when :hist, :histogram
Plot.histogram(data, params) Plot.histogram(data, params)
when :line, :lineplot when :line, :lineplot
Plot.line(data, params) Plot.line(data, params)
when :lines, :lineplots when :lines, :lineplots
Plot.lines(data, params, fmt) Plot.lines(data, params, fmt)
when :scatter, :s when :scatter, :s
Plot.scatter(data, params, fmt) Plot.scatter(data, params, fmt)
when :density, :d when :density, :d
Plot.density(data, params, fmt) Plot.density(data, params, fmt)
when :box, :boxplot when :box, :boxplot
Plot.boxplot(data, params) Plot.boxplot(data, params)
else else
raise "unrecognized plot_type: #{command}" raise "unrecognized plot_type: #{command}"
end.render($stderr) end
print input if output if output.is_a?(IO)
plot.render(output)
else
File.open(output, 'w') do |f|
plot.render(f)
end
end
if pass.is_a?(IO)
print input
elsif pass
File.open(pass, 'w') do |f|
f.print(input)
end
end
end end
end end
end end

View File

@ -7,7 +7,7 @@ module Uplot
class Command class Command
class Parser class Parser
attr_reader :command, :params, attr_reader :command, :params,
:delimiter, :transpose, :headers, :output, :count, :fmt, :debug :delimiter, :transpose, :headers, :pass, :output, :count, :fmt, :debug
def initialize def initialize
@command = nil @command = nil
@ -16,7 +16,8 @@ module Uplot
@delimiter = "\t" @delimiter = "\t"
@transpose = false @transpose = false
@headers = nil @headers = nil
@output = false @pass = false
@output = $stderr
@count = false @count = false
@fmt = 'xyy' @fmt = 'xyy'
@debug = false @debug = false
@ -26,7 +27,10 @@ module Uplot
OptionParser.new do |opt| OptionParser.new do |opt|
opt.program_name = 'uplot' opt.program_name = 'uplot'
opt.version = Uplot::VERSION opt.version = Uplot::VERSION
opt.on('-O', 'outputs the standard input data to the standard output', TrueClass) do |v| opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]') do |v|
@pass = v || $stdout
end
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v|
@output = v @output = v
end end
opt.on('-d', '--delimiter VAL', 'use DELIM instead of TAB for field delimiter', String) do |v| opt.on('-d', '--delimiter VAL', 'use DELIM instead of TAB for field delimiter', String) do |v|