YouPlot/lib/youplot/command/parser.rb

298 lines
10 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
2021-01-19 23:57:56 +08:00
class Error < StandardError; end
2021-01-19 16:34:18 +08:00
attr_reader :command, :options, :params,
:main_parser, :sub_parser
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
2021-01-19 22:05:17 +08:00
OptionParser.new do |parser|
parser.program_name = 'YouPlot'
parser.version = YouPlot::VERSION
parser.summary_width = 24
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|
options[:pass] = v || $stdout
2020-09-29 17:13:03 +08:00
end
2021-01-19 22:05:17 +08:00
parser.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 22:05:17 +08:00
parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v|
options[:delimiter] = v
2020-09-17 09:06:31 +08:00
end
2021-01-19 22:05:17 +08:00
parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
options[:headers] = v
2020-09-17 09:06:31 +08:00
end
2021-01-19 22:05:17 +08:00
parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
options[:transpose] = v
2020-09-17 09:06:31 +08:00
end
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.on('-w', '--width INT', Integer, 'number of characters per row') do |v|
2020-09-17 09:06:31 +08:00
params.width = v
end
2021-01-19 22:05:17 +08:00
parser.on('-h', '--height INT', Numeric, 'number of rows') do |v|
2020-09-17 09:06:31 +08:00
params.height = v
end
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.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
2021-01-19 22:05:17 +08:00
parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
2020-09-17 09:06:31 +08:00
params.labels = v
end
2021-01-19 22:05:17 +08:00
parser.on('--progress', TrueClass, 'progressive mode [experimental]') do |v|
options[:progressive] = v
2020-12-13 22:46:25 +08:00
end
2021-01-19 22:05:17 +08:00
parser.on('--encoding STR', String, 'Specify the input encoding') do |v|
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.
2021-01-19 22:05:17 +08:00
parser.on('--help', 'print sub-command help menu') do
puts parser.help
2021-01-19 23:07:23 +08:00
exit if YouPlot.run_as_executable?
2020-11-14 22:41:35 +08:00
end
2021-01-19 22:05:17 +08:00
parser.on('--debug', TrueClass, 'print preprocessed data') do |v|
options[:debug] = v
2020-09-17 09:06:31 +08:00
end
2021-01-19 16:34:18 +08:00
# yield opt if block_given?
2020-09-17 09:06:31 +08:00
end
end
2021-01-19 16:34:18 +08:00
def create_main_parser
@main_parser = create_default_parser
main_parser.banner = \
<<~MSG
Program: YouPlot (Tools for plotting on the terminal)
Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
Source: https://github.com/kojix2/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
colors color show the list of available colors
count c draw a baplot based on the number of
occurrences (slow)
General options:
--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
puts main_parser.banner
puts
2021-01-19 23:07:23 +08:00
exit if YouPlot.run_as_executable?
2021-01-19 16:34:18 +08:00
end
end
def sub_parser_add_symbol
2021-01-19 17:09:09 +08:00
sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
2021-01-19 16:34:18 +08:00
params.symbol = v
end
end
def sub_parser_add_xscale
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
2021-01-19 17:09:09 +08:00
sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
2021-01-19 16:34:18 +08:00
params.xscale = v
2020-09-17 09:06:31 +08:00
end
end
2021-01-19 16:34:18 +08:00
def sub_parser_add_canvas
2021-01-19 17:09:09 +08:00
sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
2021-01-19 16:34:18 +08:00
params.canvas = v.to_sym
end
end
def sub_parser_add_xlim
2021-01-19 17:09:09 +08:00
sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
2021-01-19 16:34:18 +08:00
params.xlim = v.take(2)
end
end
def sub_parser_add_ylim
2021-01-19 17:09:09 +08:00
sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
2021-01-19 16:34:18 +08:00
params.ylim = v.take(2)
end
end
2021-01-19 18:04:08 +08:00
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
2021-01-19 16:34:18 +08:00
def create_sub_parser
@sub_parser = create_default_parser
sub_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 16:34:18 +08:00
case command
# If you type only `uplot` in the terminal.
when nil
warn main_parser.banner
warn "\n"
2021-01-19 23:57:56 +08:00
exit 1 if YouPlot.run_as_executable?
2021-01-19 16:34:18 +08:00
when :barplot, :bar
sub_parser_add_symbol
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
2021-01-19 22:05:17 +08:00
options[:fmt] = v
2021-01-19 16:34:18 +08:00
end
2021-01-19 18:04:08 +08:00
sub_parser_add_xscale
2021-01-19 16:34:18 +08:00
when :count, :c
sub_parser_add_symbol
sub_parser_add_xscale
when :histogram, :hist
2021-01-19 18:04:08 +08:00
sub_parser_add_symbol
2021-01-19 16:34:18 +08:00
sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
params.closed = v
end
2021-01-19 18:04:08 +08:00
sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
params.nbins = v
end
2021-01-19 16:34:18 +08:00
when :lineplot, :line
sub_parser_add_canvas
2021-01-19 18:04:08 +08:00
sub_parser_add_grid
2021-01-19 16:34:18 +08:00
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
2021-01-19 22:05:17 +08:00
options[:fmt] = v
2021-01-19 16:34:18 +08:00
end
2021-01-19 18:35:31 +08:00
sub_parser_add_ylim
2021-01-19 18:04:08 +08:00
sub_parser_add_xlim
2021-01-19 16:34:18 +08:00
when :lineplots, :lines
sub_parser_add_canvas
2021-01-19 18:04:08 +08:00
sub_parser_add_grid
2021-01-19 16:34:18 +08:00
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|
2021-01-19 22:05:17 +08:00
options[:fmt] = v
end
2021-01-19 18:04:08 +08:00
sub_parser_add_ylim
sub_parser_add_xlim
2021-01-19 16:34:18 +08:00
when :scatter, :s
sub_parser_add_canvas
2021-01-19 18:04:08 +08:00
sub_parser_add_grid
2021-01-19 16:34:18 +08:00
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|
2021-01-19 22:05:17 +08:00
options[:fmt] = v
2021-01-19 16:34:18 +08:00
end
2021-01-19 18:04:08 +08:00
sub_parser_add_ylim
sub_parser_add_xlim
2021-01-19 16:34:18 +08:00
when :density, :d
2021-01-19 18:04:08 +08:00
sub_parser_add_canvas
sub_parser_add_grid
2021-01-19 16:34:18 +08:00
sub_parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
'xyy : header is like x, y1, y2, y2, y3...') do |v|
2021-01-19 22:05:17 +08:00
options[:fmt] = v
2021-01-19 16:34:18 +08:00
end
2021-01-19 18:04:08 +08:00
sub_parser_add_ylim
sub_parser_add_xlim
2021-01-19 16:34:18 +08:00
when :boxplot, :box
sub_parser_add_xlim
when :colors, :color, :colours, :colour
sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
2021-01-19 22:05:17 +08:00
options[:color_names] = v
2021-01-19 16:34:18 +08:00
end
else
2021-01-19 23:57:56 +08:00
error_message = "uplot: unrecognized command '#{command}'"
if YouPlot.run_as_executable?
warn error_message
exit 1
else
raise Error, error_message
end
end
end
2020-09-17 09:06:31 +08:00
def parse_options(argv = ARGV)
begin
2021-01-19 16:34:18 +08:00
create_main_parser.order!(argv)
2020-09-17 09:06:31 +08:00
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
2021-01-19 23:57:56 +08:00
exit 1 if YouPlot.run_as_executable?
2020-09-17 09:06:31 +08:00
end
@command = argv.shift&.to_sym
begin
2021-01-19 23:57:56 +08:00
create_sub_parser&.parse!(argv)
2020-09-17 09:06:31 +08:00
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
2021-01-19 23:57:56 +08:00
exit 1 if YouPlot.run_as_executable?
2020-09-17 09:06:31 +08:00
end
end
end
end
2020-10-10 22:07:57 +08:00
end