YouPlot/lib/youplot/command/parser.rb

289 lines
11 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'
2020-12-21 13:39:22 +08:00
require_relative 'options'
2020-12-14 14:02:32 +08:00
require_relative 'plot_params'
2020-09-17 15:48:03 +08:00
2020-11-23 12:09:16 +08:00
module YouPlot
2020-09-17 09:06:31 +08:00
class Command
class Parser
2020-12-14 14:54:54 +08:00
attr_reader :command, :options, :params
2020-09-17 09:06:31 +08:00
def initialize
2020-12-14 14:54:54 +08:00
@command = nil
2020-11-06 09:21:36 +08:00
2020-12-21 13:39:22 +08:00
@options = Options.new(
2020-12-14 14:54:54 +08:00
delimiter: "\t",
transpose: false,
headers: nil,
pass: false,
output: $stderr,
fmt: 'xyy',
encoding: nil,
color_names: false,
debug: false
)
2020-12-14 21:15:45 +08:00
2020-12-14 14:54:54 +08:00
@params = PlotParams.new
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|
2020-11-23 12:09:16 +08:00
opt.program_name = 'YouPlot'
opt.version = YouPlot::VERSION
2020-10-12 15:14:51 +08:00
opt.summary_width = 24
opt.on_tail('') # Add a blank line at the end
2020-10-12 15:45:28 +08:00
opt.separator('')
2020-11-14 23:29:20 +08:00
opt.on('Common options:')
opt.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
2020-11-23 12:09:16 +08:00
'for inserting YouPlot in the middle of Unix pipes') do |v|
2020-12-14 14:54:54 +08:00
@options[:pass] = v || $stdout
2020-09-29 17:13:03 +08:00
end
opt.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
'If no option is specified, plot will print to stderr') do |v|
@options[:output] = v || $stdout
2020-09-17 09:06:31 +08:00
end
2021-01-19 15:16:03 +08:00
opt.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v|
2020-12-14 14:54:54 +08:00
@options[:delimiter] = v
2020-09-17 09:06:31 +08:00
end
2020-10-10 23:11:54 +08:00
opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
2020-12-14 14:54:54 +08:00
@options[:headers] = v
2020-09-17 09:06:31 +08:00
end
2020-11-06 08:56:35 +08:00
opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
2020-12-14 14:54:54 +08:00
@options[:transpose] = v
2020-09-17 09:06:31 +08:00
end
opt.on('-t', '--title STR', String, 'print string on the top of plot') do |v|
2020-09-17 09:06:31 +08:00
params.title = v
end
opt.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v|
2020-09-17 09:06:31 +08:00
params.xlabel = v
end
opt.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v|
2020-09-17 09:06:31 +08:00
params.ylabel = v
end
opt.on('-w', '--width INT', Integer, 'number of characters per row') do |v|
2020-09-17 09:06:31 +08:00
params.width = v
end
opt.on('-h', '--height INT', Numeric, 'number of rows') do |v|
2020-09-17 09:06:31 +08:00
params.height = v
end
opt.on('-b', '--border STR', String, 'specify the style of the bounding box') do |v|
2020-09-17 09:06:31 +08:00
params.border = v.to_sym
end
opt.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v|
2020-09-17 09:06:31 +08:00
params.margin = v
end
opt.on('-p', '--padding INT', Numeric, 'space of the left and right of the plot') do |v|
2020-09-17 09:06:31 +08:00
params.padding = v
end
2020-10-10 23:11:54 +08:00
opt.on('-c', '--color VAL', String, 'color of the drawing') 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
2020-10-10 23:11:54 +08:00
opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
2020-09-17 09:06:31 +08:00
params.labels = v
end
2020-12-21 16:09:47 +08:00
opt.on('--progress', TrueClass, 'progressive mode [experimental]') do |v|
2020-12-14 14:54:54 +08:00
@options[:progressive] = v
2020-12-13 22:46:25 +08:00
end
opt.on('--encoding STR', String, 'Specify the input encoding') do |v|
2020-12-14 14:54:54 +08:00
@options[:encoding] = v
2020-11-23 22:52:14 +08:00
end
2020-11-14 22:41:35 +08:00
# Optparse adds the help option, but it doesn't show up in usage.
# This is why you need the code below.
opt.on('--help', 'print sub-command help menu') do
puts opt.help
exit
end
2020-11-14 22:09:49 +08:00
opt.on('--debug', TrueClass, 'print preprocessed data') do |v|
2020-12-14 14:54:54 +08:00
@options[:debug] = v
2020-09-17 09:06:31 +08:00
end
yield opt if block_given?
end
end
def main_parser
@main_parser ||= create_default_parser do |main_parser|
2020-11-14 23:43:13 +08:00
# Here, help message is stored in the banner.
# Because help of main_parser may be referred by `sub_parser`.
2021-01-19 13:56:28 +08:00
2020-09-17 09:06:31 +08:00
main_parser.banner = \
<<~MSG
2021-01-19 13:56:28 +08:00
2020-11-23 12:09:16 +08:00
Program: YouPlot (Tools for plotting on the terminal)
2021-01-19 15:00:29 +08:00
Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
Source: https://github.com/kojix2/youplot
2021-01-19 13:56:28 +08:00
2021-01-19 15:00:29 +08:00
Usage: uplot <command> [options] <in.tsv>
2020-09-17 09:06:31 +08:00
2021-01-19 15:00:29 +08:00
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
colors color show the list of available colors
2020-09-17 09:06:31 +08:00
2021-01-19 15:00:29 +08:00
count c draw a baplot based on the number of
occurrences (slow)
2020-11-06 08:56:35 +08:00
2021-01-19 15:00:29 +08:00
General options:
--help print command specific help menu
--version print the version of YouPlot
2020-09-17 09:06:31 +08:00
MSG
2020-11-14 23:29:20 +08:00
# Actually, main_parser can take common optional arguments.
# However, these options dose not be shown in the help menu.
# I think the main help should be simple.
main_parser.on('--help', 'print sub-command help menu') do
puts main_parser.banner
puts
exit
end
2020-09-17 09:06:31 +08:00
end
end
def sub_parser
@sub_parser ||= create_default_parser do |parser|
parser.banner = <<~MSG
2020-10-12 15:14:51 +08:00
2020-11-23 12:09:16 +08:00
Usage: YouPlot #{command} [options] <in.tsv>
2020-10-12 15:45:28 +08:00
Options for #{command}:
MSG
2021-01-19 13:40:36 +08:00
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
case command
2020-11-14 23:43:00 +08:00
2020-11-14 23:29:20 +08:00
# If you type only `uplot` in the terminal.
when nil
2020-11-14 23:29:20 +08:00
warn main_parser.banner
warn "\n"
exit 1
when :barplot, :bar
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
params.xscale = v
end
parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
2020-12-14 14:54:54 +08:00
@options[:fmt] = v
end
when :count, :c
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
when :histogram, :hist
parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
params.nbins = v
end
parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
params.closed = v
end
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
when :lineplot, :line
parser.on_head('--canvas STR', String, 'type of canvas') do |v|
2021-01-19 15:16:21 +08:00
params.canvas = v.to_sym
end
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
2020-12-14 14:54:54 +08:00
@options[:fmt] = v
end
when :lineplots, :lines
parser.on_head('--canvas STR', String) do |v|
2021-01-19 15:16:21 +08:00
params.canvas = v.to_sym
end
2021-01-19 15:16:03 +08:00
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
2020-12-14 21:15:45 +08:00
'xyy : header is like x, y1, y2, y2, y3...') do |v|
2020-12-14 14:54:54 +08:00
@options[:fmt] = v
end
when :scatter, :s
parser.on_head('--canvas STR', String) do |v|
2021-01-19 15:16:21 +08:00
params.canvas = v.to_sym
end
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
2020-12-14 21:15:45 +08:00
'xyy : header is like x, y1, y2, y2, y3...') do |v|
2020-12-14 14:54:54 +08:00
@options[:fmt] = v
end
when :density, :d
2020-10-12 15:45:28 +08:00
parser.on_head('--grid', TrueClass) do |v|
params.grid = v
end
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
2020-12-14 21:15:45 +08:00
'xyy : header is like x, y1, y2, y2, y3...') do |v|
2020-12-14 14:54:54 +08:00
@options[:fmt] = v
end
when :boxplot, :box
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
2020-12-17 11:37:27 +08:00
when :colors, :color, :colours, :colour
2020-11-06 09:21:36 +08:00
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
2020-12-14 14:54:54 +08:00
@options[: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