YouPlot/lib/uplot/command/parser.rb

225 lines
6.7 KiB
Ruby
Raw Normal View History

2020-09-18 23:08:09 +08:00
# frozen_string_literal: true
2020-09-17 15:48:03 +08:00
require 'optparse'
require_relative 'params'
2020-09-17 09:06:31 +08:00
module Uplot
class Command
class Parser
attr_reader :command, :params,
2020-10-10 22:07:57 +08:00
:delimiter, :transpose, :headers, :pass, :output, :fmt, :debug
2020-09-17 09:06:31 +08:00
def initialize
@command = nil
@params = Params.new
2020-09-17 09:28:01 +08:00
@delimiter = "\t"
@transpose = false
@headers = nil
2020-09-29 17:13:03 +08:00
@pass = false
@output = $stderr
2020-09-17 09:38:59 +08:00
@fmt = 'xyy'
@debug = false
2020-09-17 09:45:44 +08:00
end
2020-09-17 09:06:31 +08:00
def create_default_parser
OptionParser.new do |opt|
opt.program_name = 'uplot'
opt.version = Uplot::VERSION
2020-09-29 17:13:03 +08:00
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|
2020-09-17 09:06:31 +08:00
@output = v
end
opt.on('-d', '--delimiter VAL', 'use DELIM instead of TAB for field delimiter', String) do |v|
@delimiter = v
end
opt.on('-H', '--headers', 'specify that the input has header row', TrueClass) do |v|
@headers = v
end
opt.on('-T', '--transpose', TrueClass) do |v|
@transpose = v
end
opt.on('-t', '--title VAL', 'print string on the top of plot', String) do |v|
params.title = v
end
opt.on('-x', '--xlabel VAL', 'print string on the bottom of the plot', String) do |v|
params.xlabel = v
end
opt.on('-y', '--ylabel VAL', 'print string on the far left of the plot', String) do |v|
params.ylabel = v
end
opt.on('-w', '--width VAL', 'number of characters per row', Integer) do |v|
params.width = v
end
opt.on('-h', '--height VAL', 'number of rows', Numeric) do |v|
params.height = v
end
opt.on('-b', '--border VAL', 'specify the style of the bounding box', String) do |v|
params.border = v.to_sym
end
opt.on('-m', '--margin VAL', 'number of spaces to the left of the plot', Numeric) do |v|
params.margin = v
end
opt.on('-p', '--padding VAL', 'space of the left and right of the plot', Numeric) do |v|
params.padding = v
end
opt.on('-c', '--color VAL', 'color of the drawing', String) do |v|
2020-09-17 09:45:57 +08:00
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
2020-09-17 09:06:31 +08:00
end
opt.on('--[no-]labels', 'hide the labels', TrueClass) do |v|
params.labels = v
end
2020-10-10 22:58:27 +08:00
opt.on('--fmt VAL', 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...', String) do |v|
2020-09-17 09:06:31 +08:00
@fmt = v
end
opt.on('--debug', TrueClass) do |v|
@debug = v
end
yield opt if block_given?
end
end
def main_parser
@main_parser ||= create_default_parser do |main_parser|
2020-09-17 09:06:31 +08:00
# Usage and help messages
main_parser.banner = \
<<~MSG
Program: uplot (Tools for plotting on the terminal)
Version: #{Uplot::VERSION} (using unicode_plot #{UnicodePlot::VERSION})
Usage: uplot <command> [options]
2020-09-24 23:52:33 +08:00
Command: barplot bar
histogram hist
lineplot line
scatter s
density d
boxplot box
colors
2020-09-17 09:06:31 +08:00
Options:
MSG
end
end
def sub_parser
@sub_parser ||= create_default_parser do |parser|
parser.banner = <<~MSG
Usage: uplot #{command} [options]
Options:
MSG
case command
when nil
warn main_parser.help
exit 1
when :barplot, :bar
parser.on('--symbol VAL', String) do |v|
params.symbol = v
end
parser.on('--xscale VAL', String) do |v|
params.xscale = v
end
when :count, :c
parser.on('--symbol VAL', String) do |v|
params.symbol = v
end
when :histogram, :hist
parser.on('-n', '--nbins VAL', Numeric) do |v|
params.nbins = v
end
parser.on('--closed VAL', String) do |v|
params.closed = v
end
parser.on('--symbol VAL', String) do |v|
params.symbol = v
end
when :lineplot, :line
parser.on('--canvas VAL', String) do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
params.ylim = v.take(2)
end
when :lineplots, :lines
parser.on('--canvas VAL', String) do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
params.ylim = v.take(2)
end
when :scatter, :s
parser.on('--canvas VAL', String) do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
params.ylim = v.take(2)
end
when :density, :d
parser.on('--grid', TrueClass) do |v|
params.grid = v
end
parser.on('--xlim VAL', Array) do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
params.ylim = v.take(2)
end
when :boxplot, :box
parser.on('--xlim VAL', Array) do |v|
params.xlim = v.take(2)
end
when :colors
parser.on('-n', '--names', TrueClass) do |v|
@color_names = v
end
else
warn "uplot: unrecognized command '#{command}'"
exit 1
end
end
end
2020-09-17 09:06:31 +08:00
def parse_options(argv = ARGV)
begin
main_parser.order!(argv)
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
exit 1
end
@command = argv.shift&.to_sym
begin
sub_parser.parse!(argv)
2020-09-17 09:06:31 +08:00
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
exit 1
end
end
end
end
2020-10-10 22:07:57 +08:00
end