Skip to content

Commit

Permalink
Arg/opt-related errors now print usage
Browse files Browse the repository at this point in the history
Also, exception handling moved from `parse_and_run` to `parse`.
  • Loading branch information
Ryan S. Northrup committed Aug 18, 2015
1 parent 86b2499 commit 9bcde10
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/bales/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -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)
Expand Down

0 comments on commit 9bcde10

Please sign in to comment.