From 3c6a4b46b1f7fcfaa18104aa01b40f16b40ff893 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Fri, 14 Aug 2020 11:45:47 +0900 Subject: [PATCH] Improved parser error messages. --- lib/uplot/command.rb | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/uplot/command.rb b/lib/uplot/command.rb index 4282feb..1f082b7 100644 --- a/lib/uplot/command.rb +++ b/lib/uplot/command.rb @@ -71,24 +71,41 @@ module Uplot parsers.default = nil main_parser.banner = <<~MSG - Program: Uplot (Tools for plotting on the terminal) + Program: uplot (Tools for plotting on the terminal) Version: #{Uplot::VERSION} (using unicode_plot #{UnicodePlot::VERSION}) Usage: uplot [options] Command: #{parsers.keys.join(' ')} + Options: MSG - main_parser.order!(argv) + + begin + main_parser.order!(argv) + rescue OptionParser::ParseError => e + warn "uplot: #{e.message}" + exit 1 + end + @ptype = argv.shift unless parsers.has_key?(@ptype) - puts main_parser.help - warn "unrecognized command '#{@ptype}'" + if @ptype.nil? + warn main_parser.help + else + warn "uplot: unrecognized command '#{@ptype}'" + end exit 1 end parser = parsers[@ptype] - parser.parse!(argv) unless argv.empty? + + begin + parser.parse!(argv) unless argv.empty? + rescue OptionParser::ParseError => e + warn "uplot: #{e.message}" + exit 1 + end end def run