Class: YouPlot::Command::Parser
- Inherits:
-
Object
- Object
- YouPlot::Command::Parser
- Defined in:
- lib/youplot/command/parser.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#main_parser ⇒ Object
readonly
Returns the value of attribute main_parser.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#sub_parser ⇒ Object
readonly
Returns the value of attribute sub_parser.
Instance Method Summary collapse
- #create_default_parser ⇒ Object
- #create_main_parser ⇒ Object
- #create_sub_parser ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse_options(argv = ARGV) ⇒ Object
- #sub_parser_add_canvas ⇒ Object
- #sub_parser_add_grid ⇒ Object
- #sub_parser_add_symbol ⇒ Object
- #sub_parser_add_xlim ⇒ Object
- #sub_parser_add_xscale ⇒ Object
- #sub_parser_add_ylim ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/youplot/command/parser.rb', line 15 def initialize @command = nil = Options.new( delimiter: "\t", transpose: false, headers: nil, pass: false, output: $stderr, fmt: 'xyy', encoding: nil, color_names: false, debug: false ) @params = PlotParams.new end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
12 13 14 |
# File 'lib/youplot/command/parser.rb', line 12 def command @command end |
#main_parser ⇒ Object (readonly)
Returns the value of attribute main_parser.
12 13 14 |
# File 'lib/youplot/command/parser.rb', line 12 def main_parser @main_parser end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/youplot/command/parser.rb', line 12 def end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'lib/youplot/command/parser.rb', line 12 def params @params end |
#sub_parser ⇒ Object (readonly)
Returns the value of attribute sub_parser.
12 13 14 |
# File 'lib/youplot/command/parser.rb', line 12 def sub_parser @sub_parser end |
Instance Method Details
#create_default_parser ⇒ Object
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 97 98 99 100 101 102 103 104 105 |
# File 'lib/youplot/command/parser.rb', line 33 def create_default_parser 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| [:pass] = v || $stdout end parser.on('-o', '--output [FILE]', 'file to output plots to [stdout]', 'If no option is specified, plot will print to stderr') do |v| [:output] = v || $stdout end parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v| [:delimiter] = v end parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v| [:headers] = v end parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v| [:transpose] = v end parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v| params.title = v end parser.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v| params.xlabel = v end parser.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v| params.ylabel = v end parser.on('-w', '--width INT', Integer, 'number of characters per row') do |v| params.width = v end parser.on('-h', '--height INT', Numeric, 'number of rows') do |v| params.height = v end parser.on('-b', '--border STR', String, 'specify the style of the bounding box') do |v| params.border = v.to_sym end parser.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v| params.margin = v end parser.on('--padding INT', Numeric, 'space of the left and right of the plot') do |v| params.padding = v end parser.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 parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v| params.labels = v end parser.on('-p', '--progress', TrueClass, 'progressive mode [experimental]') do |v| [:progressive] = v end parser.on('--encoding STR', 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. parser.on('--help', 'print sub-command help menu') do puts parser.help exit if YouPlot.run_as_executable? end parser.on('--debug', TrueClass, 'print preprocessed data') do |v| [:debug] = v end # yield opt if block_given? end end |
#create_main_parser ⇒ Object
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 140 141 142 143 |
# File 'lib/youplot/command/parser.rb', line 107 def create_main_parser @main_parser = create_default_parser main_parser. = \ "\n Program: YouPlot (Tools for plotting on the terminal)\n Version: \#{YouPlot::VERSION} (using UnicodePlot \#{UnicodePlot::VERSION})\n Source: https://github.com/kojix2/youplot\n\n Usage: uplot <command> [options] <in.tsv>\n\n Commands:\n barplot bar draw a horizontal barplot\n histogram hist draw a horizontal histogram\n lineplot line draw a line chart\n lineplots lines draw a line chart with multiple series\n scatter s draw a scatter plot\n density d draw a density plot\n boxplot box draw a horizontal boxplot\n colors color show the list of available colors\n\n count c draw a baplot based on the number of\n occurrences (slow)\n\n General options:\n --help print command specific help menu\n --version print the version of YouPlot\n MSG\n\n # Help for the main parser is simple.\n # Simply show the banner above.\n main_parser.on('--help', 'print sub-command help menu') do\n puts main_parser.banner\n puts\n exit if YouPlot.run_as_executable?\n end\nend\n" |
#create_sub_parser ⇒ Object
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/youplot/command/parser.rb', line 182 def create_sub_parser @sub_parser = create_default_parser sub_parser. = \ "\n Usage: YouPlot \#{command} [options] <in.tsv>\n\n Options for \#{command}:\n MSG\n\n case command\n\n # If you type only `uplot` in the terminal.\n when nil\n warn main_parser.banner\n warn \"\\n\"\n exit 1 if YouPlot.run_as_executable?\n\n when :barplot, :bar\n sub_parser_add_symbol\n sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|\n options[:fmt] = v\n end\n sub_parser_add_xscale\n\n when :count, :c\n sub_parser_add_symbol\n sub_parser_add_xscale\n\n when :histogram, :hist\n sub_parser_add_symbol\n sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|\n params.closed = v\n end\n sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|\n params.nbins = v\n end\n\n when :lineplot, :line\n sub_parser_add_canvas\n sub_parser_add_grid\n sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|\n options[:fmt] = v\n end\n sub_parser_add_ylim\n sub_parser_add_xlim\n\n when :lineplots, :lines\n sub_parser_add_canvas\n sub_parser_add_grid\n sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',\n 'xyy : header is like x, y1, y2, y2, y3...') do |v|\n options[:fmt] = v\n end\n sub_parser_add_ylim\n sub_parser_add_xlim\n\n when :scatter, :s\n sub_parser_add_canvas\n sub_parser_add_grid\n sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',\n 'xyy : header is like x, y1, y2, y2, y3...') do |v|\n options[:fmt] = v\n end\n sub_parser_add_ylim\n sub_parser_add_xlim\n\n when :density, :d\n sub_parser_add_canvas\n sub_parser_add_grid\n sub_parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',\n 'xyy : header is like x, y1, y2, y2, y3...') do |v|\n options[:fmt] = v\n end\n sub_parser_add_ylim\n sub_parser_add_xlim\n\n when :boxplot, :box\n sub_parser_add_xlim\n\n when :colors, :color, :colours, :colour\n sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|\n options[:color_names] = v\n end\n\n else\n error_message = \"uplot: unrecognized command '\#{command}'\"\n if YouPlot.run_as_executable?\n warn error_message\n exit 1\n else\n raise Error, error_message\n end\n end\nend\n" |
#parse_options(argv = ARGV) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/youplot/command/parser.rb', line 278 def (argv = ARGV) begin create_main_parser.order!(argv) rescue OptionParser::ParseError => e warn "uplot: #{e.message}" exit 1 if YouPlot.run_as_executable? end @command = argv.shift&.to_sym begin create_sub_parser&.parse!(argv) rescue OptionParser::ParseError => e warn "uplot: #{e.message}" exit 1 if YouPlot.run_as_executable? end end |
#sub_parser_add_canvas ⇒ Object
158 159 160 161 162 |
# File 'lib/youplot/command/parser.rb', line 158 def sub_parser_add_canvas sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v| params.canvas = v.to_sym end end |
#sub_parser_add_grid ⇒ Object
176 177 178 179 180 |
# File 'lib/youplot/command/parser.rb', line 176 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 |
#sub_parser_add_symbol ⇒ Object
145 146 147 148 149 |
# File 'lib/youplot/command/parser.rb', line 145 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 |
#sub_parser_add_xlim ⇒ Object
164 165 166 167 168 |
# File 'lib/youplot/command/parser.rb', line 164 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 |
#sub_parser_add_xscale ⇒ Object
151 152 153 154 155 156 |
# File 'lib/youplot/command/parser.rb', line 151 def sub_parser_add_xscale = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ') sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v| params.xscale = v end end |
#sub_parser_add_ylim ⇒ Object
170 171 172 173 174 |
# File 'lib/youplot/command/parser.rb', line 170 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 |