From 9bcde10851a407b17a681bd189f2ad9d3856b4ae Mon Sep 17 00:00:00 2001 From: "Ryan S. Northrup" Date: Mon, 17 Aug 2015 18:13:06 -0700 Subject: [PATCH] Arg/opt-related errors now print usage Also, exception handling moved from `parse_and_run` to `parse`. --- lib/bales/application.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/bales/application.rb b/lib/bales/application.rb index 49917cd..2d6a171 100644 --- a/lib/bales/application.rb +++ b/lib/bales/application.rb @@ -130,21 +130,15 @@ def self.parse(argv=ARGV) command ||= default_command opts, args = command.parse_opts result return command, args, opts - end - - ## - # Parses ARGV (or some other array if you specify one) for a - # command to run and its arguments/options, then runs the command. - def self.parse_and_run(argv=ARGV) - command, args, opts = parse argv - run command, *args, **opts rescue OptionParser::MissingArgument flag = $!.message.gsub("missing argument: ", '') puts "#{$0}: error: option needs an argument (#{flag})" + puts "Usage: #{command.usage}" exit! rescue OptionParser::InvalidOption flag = $!.message.gsub("invalid option: ", '') puts "#{$0}: error: unknown option (#{flag})" + puts "Usage: #{command.usage}" exit! rescue ArgumentError raise unless $!.message.match(/wrong number of arguments/) @@ -154,9 +148,18 @@ def self.parse_and_run(argv=ARGV) .gsub(")", '') .split(" for ") puts "#{$0}: error: expected #{expected} args but got #{received}" + puts "Usage: #{command.usage}" exit! end + ## + # Parses ARGV (or some other array if you specify one) for a + # command to run and its arguments/options, then runs the command. + def self.parse_and_run(argv=ARGV) + command, args, opts = parse argv + run command, *args, **opts + end + private def self.parse_command_name(argv)