Class: YouPlot::Command::Parser
- Inherits:
-
Object
- Object
- YouPlot::Command::Parser
- Defined in:
- lib/youplot/command/parser.rb
Instance Attribute Summary collapse
-
#color_names ⇒ Object
readonly
Returns the value of attribute color_names.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#fmt ⇒ Object
readonly
Returns the value of attribute fmt.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#transpose ⇒ Object
readonly
Returns the value of attribute transpose.
Instance Method Summary collapse
- #create_default_parser ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #main_parser ⇒ Object
- #parse_options(argv = ARGV) ⇒ Object
- #sub_parser ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/youplot/command/parser.rb', line 13 def initialize @command = nil @params = Params.new @delimiter = "\t" @transpose = false @headers = nil @pass = false @output = $stderr @fmt = 'xyy' @encoding = nil @debug = false @color_names = false end |
Instance Attribute Details
#color_names ⇒ Object (readonly)
Returns the value of attribute color_names.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def color_names @color_names end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def command @command end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def debug @debug end |
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def delimiter @delimiter end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def encoding @encoding end |
#fmt ⇒ Object (readonly)
Returns the value of attribute fmt.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def fmt @fmt end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def headers @headers end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def output @output end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def params @params end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def pass @pass end |
#transpose ⇒ Object (readonly)
Returns the value of attribute transpose.
9 10 11 |
# File 'lib/youplot/command/parser.rb', line 9 def transpose @transpose end |
Instance Method Details
#create_default_parser ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/youplot/command/parser.rb', line 28 def create_default_parser OptionParser.new do |opt| opt.program_name = 'YouPlot' opt.version = YouPlot::VERSION opt.summary_width = 24 opt.on_tail('') # Add a blank line at the end opt.separator('') opt.on('Common options:') opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]', 'for inserting YouPlot in the middle of Unix pipes') do |v| @pass = v || $stdout end opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v| @output = v end opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v| @delimiter = v end opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v| @headers = v end opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v| @transpose = v end opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v| params.title = v end opt.on('-x', '--xlabel VAL', String, 'print string on the bottom of the plot') do |v| params.xlabel = v end opt.on('-y', '--ylabel VAL', String, 'print string on the far left of the plot') do |v| params.ylabel = v end opt.on('-w', '--width VAL', Integer, 'number of characters per row') do |v| params.width = v end opt.on('-h', '--height VAL', Numeric, 'number of rows') do |v| params.height = v end opt.on('-b', '--border VAL', String, 'specify the style of the bounding box') do |v| params.border = v.to_sym end opt.on('-m', '--margin VAL', Numeric, 'number of spaces to the left of the plot') do |v| params.margin = v end opt.on('-p', '--padding VAL', Numeric, 'space of the left and right of the plot') do |v| params.padding = v end opt.on('-c', '--color VAL', String, 'color of the drawing') do |v| params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym end opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v| params.labels = v end opt.on('--encoding VAL', String, 'Specify the input encoding') do |v| @encoding = v end # 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 opt.on('--debug', TrueClass, 'print preprocessed data') do |v| @debug = v end yield opt if block_given? end end |
#main_parser ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/youplot/command/parser.rb', line 98 def main_parser @main_parser ||= create_default_parser do |main_parser| # Here, help message is stored in the banner. # Because help of main_parser may be referred by `sub_parser`. main_parser. = \ <<~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 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 # 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. puts exit end end end |
#parse_options(argv = ARGV) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/youplot/command/parser.rb', line 258 def (argv = ARGV) begin main_parser.order!(argv) rescue OptionParser::ParseError => e warn "uplot: #{e.}" exit 1 end @command = argv.shift&.to_sym begin sub_parser.parse!(argv) rescue OptionParser::ParseError => e warn "uplot: #{e.}" exit 1 end end |
#sub_parser ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/youplot/command/parser.rb', line 141 def sub_parser @sub_parser ||= create_default_parser do |parser| parser. = <<~MSG Usage: YouPlot #{command} [options] <in.tsv> Options for #{command}: MSG case command # If you type only `uplot` in the terminal. when nil warn main_parser. warn "\n" exit 1 when :barplot, :bar parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v| params.symbol = v end parser.on_head('--xscale VAL', String, 'axis scaling') do |v| params.xscale = v end parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| @fmt = v end when :count, :c parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v| params.symbol = v end when :histogram, :hist parser.on_head('-n', '--nbins VAL', Numeric, 'approximate number of bins') do |v| params.nbins = v end parser.on_head('--closed VAL', String) do |v| params.closed = v end parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v| params.symbol = v end when :lineplot, :line parser.on_head('--canvas VAL', String, 'type of canvas') do |v| params.canvas = v end parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.take(2) end parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| @fmt = v end when :lineplots, :lines parser.on_head('--canvas VAL', String) do |v| params.canvas = v end parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.take(2) end parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| @fmt = v end when :scatter, :s parser.on_head('--canvas VAL', String) do |v| params.canvas = v end parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.take(2) end parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| @fmt = v end when :density, :d parser.on_head('--grid', TrueClass) do |v| params.grid = v end parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.take(2) end parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end parser.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| @fmt = v end when :boxplot, :box parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v| params.xlim = v.take(2) end when :colors parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v| @color_names = v end else warn "uplot: unrecognized command '#{command}'" exit 1 end end end |