mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-07 23:51:12 +08:00
Refactoring of sub_parser
This commit is contained in:
parent
2e7a7c8851
commit
93195713e6
@ -7,7 +7,8 @@ require_relative 'plot_params'
|
|||||||
module YouPlot
|
module YouPlot
|
||||||
class Command
|
class Command
|
||||||
class Parser
|
class Parser
|
||||||
attr_reader :command, :options, :params
|
attr_reader :command, :options, :params,
|
||||||
|
:main_parser, :sub_parser
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@command = nil
|
@command = nil
|
||||||
@ -97,15 +98,12 @@ module YouPlot
|
|||||||
opt.on('--debug', TrueClass, 'print preprocessed data') do |v|
|
opt.on('--debug', TrueClass, 'print preprocessed data') do |v|
|
||||||
@options[:debug] = v
|
@options[:debug] = v
|
||||||
end
|
end
|
||||||
yield opt if block_given?
|
# yield opt if block_given?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def main_parser
|
def create_main_parser
|
||||||
@main_parser ||= create_default_parser do |main_parser|
|
@main_parser = create_default_parser
|
||||||
# Here, help message is stored in the banner.
|
|
||||||
# Because help of main_parser may be referred by `sub_parser`.
|
|
||||||
|
|
||||||
main_parser.banner = \
|
main_parser.banner = \
|
||||||
<<~MSG
|
<<~MSG
|
||||||
|
|
||||||
@ -133,28 +131,56 @@ module YouPlot
|
|||||||
--version print the version of YouPlot
|
--version print the version of YouPlot
|
||||||
MSG
|
MSG
|
||||||
|
|
||||||
# Actually, main_parser can take common optional arguments.
|
# Help for the main parser is simple.
|
||||||
# However, these options dose not be shown in the help menu.
|
# Simply show the banner above.
|
||||||
# I think the main help should be simple.
|
|
||||||
main_parser.on('--help', 'print sub-command help menu') do
|
main_parser.on('--help', 'print sub-command help menu') do
|
||||||
puts main_parser.banner
|
puts main_parser.banner
|
||||||
puts
|
puts
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sub_parser_add_symbol
|
||||||
|
@sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
||||||
|
params.symbol = v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sub_parser
|
def sub_parser_add_xscale
|
||||||
@sub_parser ||= create_default_parser do |parser|
|
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
|
||||||
parser.banner = <<~MSG
|
@sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
|
||||||
|
params.xscale = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sub_parser_add_canvas
|
||||||
|
@sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
|
||||||
|
params.canvas = v.to_sym
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sub_parser_add_xlim
|
||||||
|
@sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
||||||
|
params.xlim = v.take(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sub_parser_add_ylim
|
||||||
|
@sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
|
||||||
|
params.ylim = v.take(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_sub_parser
|
||||||
|
@sub_parser = create_default_parser
|
||||||
|
sub_parser.banner = \
|
||||||
|
<<~MSG
|
||||||
|
|
||||||
Usage: YouPlot #{command} [options] <in.tsv>
|
Usage: YouPlot #{command} [options] <in.tsv>
|
||||||
|
|
||||||
Options for #{command}:
|
Options for #{command}:
|
||||||
MSG
|
MSG
|
||||||
|
|
||||||
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
|
|
||||||
|
|
||||||
case command
|
case command
|
||||||
|
|
||||||
# If you type only `uplot` in the terminal.
|
# If you type only `uplot` in the terminal.
|
||||||
@ -164,98 +190,66 @@ module YouPlot
|
|||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
when :barplot, :bar
|
when :barplot, :bar
|
||||||
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
sub_parser_add_symbol
|
||||||
params.symbol = v
|
sub_parser_add_xscale
|
||||||
end
|
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
|
||||||
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|
|
|
||||||
@options[:fmt] = v
|
@options[:fmt] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :count, :c
|
when :count, :c
|
||||||
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
sub_parser_add_symbol
|
||||||
params.symbol = v
|
sub_parser_add_xscale
|
||||||
end
|
|
||||||
|
|
||||||
when :histogram, :hist
|
when :histogram, :hist
|
||||||
parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
|
sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
|
||||||
params.nbins = v
|
params.nbins = v
|
||||||
end
|
end
|
||||||
parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
|
sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
|
||||||
params.closed = v
|
params.closed = v
|
||||||
end
|
end
|
||||||
parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
sub_parser_add_symbol
|
||||||
params.symbol = v
|
|
||||||
end
|
|
||||||
|
|
||||||
when :lineplot, :line
|
when :lineplot, :line
|
||||||
parser.on_head('--canvas STR', String, 'type of canvas') do |v|
|
sub_parser_add_canvas
|
||||||
params.canvas = v.to_sym
|
sub_parser_add_xlim
|
||||||
end
|
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
|
||||||
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|
|
|
||||||
@options[:fmt] = v
|
@options[:fmt] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :lineplots, :lines
|
when :lineplots, :lines
|
||||||
parser.on_head('--canvas STR', String) do |v|
|
sub_parser_add_canvas
|
||||||
params.canvas = v.to_sym
|
sub_parser_add_xlim
|
||||||
end
|
sub_parser_add_ylim
|
||||||
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||||
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...',
|
|
||||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||||
@options[:fmt] = v
|
@options[:fmt] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :scatter, :s
|
when :scatter, :s
|
||||||
parser.on_head('--canvas STR', String) do |v|
|
sub_parser_add_canvas
|
||||||
params.canvas = v.to_sym
|
sub_parser_add_xlim
|
||||||
end
|
sub_parser_add_ylim
|
||||||
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||||
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...',
|
|
||||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||||
@options[:fmt] = v
|
@options[:fmt] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :density, :d
|
when :density, :d
|
||||||
parser.on_head('--grid', TrueClass) do |v|
|
sub_parser.on_head('--grid', TrueClass) do |v|
|
||||||
params.grid = v
|
params.grid = v
|
||||||
end
|
end
|
||||||
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
sub_parser_add_xlim
|
||||||
params.xlim = v.take(2)
|
sub_parser_add_ylim
|
||||||
end
|
sub_parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||||
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...',
|
|
||||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||||
@options[:fmt] = v
|
@options[:fmt] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :boxplot, :box
|
when :boxplot, :box
|
||||||
parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
sub_parser_add_xlim
|
||||||
params.xlim = v.take(2)
|
|
||||||
end
|
|
||||||
|
|
||||||
when :colors, :color, :colours, :colour
|
when :colors, :color, :colours, :colour
|
||||||
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
|
sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
|
||||||
@options[:color_names] = v
|
@options[:color_names] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -264,11 +258,10 @@ module YouPlot
|
|||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def parse_options(argv = ARGV)
|
def parse_options(argv = ARGV)
|
||||||
begin
|
begin
|
||||||
main_parser.order!(argv)
|
create_main_parser.order!(argv)
|
||||||
rescue OptionParser::ParseError => e
|
rescue OptionParser::ParseError => e
|
||||||
warn "uplot: #{e.message}"
|
warn "uplot: #{e.message}"
|
||||||
exit 1
|
exit 1
|
||||||
@ -277,7 +270,7 @@ module YouPlot
|
|||||||
@command = argv.shift&.to_sym
|
@command = argv.shift&.to_sym
|
||||||
|
|
||||||
begin
|
begin
|
||||||
sub_parser.parse!(argv)
|
create_sub_parser.parse!(argv)
|
||||||
rescue OptionParser::ParseError => e
|
rescue OptionParser::ParseError => e
|
||||||
warn "uplot: #{e.message}"
|
warn "uplot: #{e.message}"
|
||||||
exit 1
|
exit 1
|
||||||
|
Loading…
Reference in New Issue
Block a user