lib/quickl/command.rb in quickl-0.2.2 vs lib/quickl/command.rb in quickl-0.3.0

- old
+ new

@@ -37,11 +37,11 @@ ############################################### Textual information about the command # Returns name of the program under execution def program_name - File.basename($0) + Quickl.program_name end # Returns command name def command_name module2command(self) @@ -83,11 +83,14 @@ doc.find{|s| !s.strip.empty?} || "no overview available" end # Runs the command def run(argv = [], requester = nil) - self.new.run(argv, requester) + cmd = self.new + cmd.run(argv, requester) + rescue Quickl::Error => ex + handle_error(ex, cmd) end ############################################### Error handling # Bypass reaction to some exceptions @@ -96,20 +99,34 @@ @no_react_to += args end # Should I bypass reaction to a given error? def no_react_to?(ex) - @no_react_to && @no_react_to.find{|c| + defined?(@no_react_to) && Array(@no_react_to).find{|c| ex.is_a?(c) } end # Should I react to a given error? def react_to?(ex) !no_react_to?(ex) end + # Handles a command error + def handle_error(ex, cmd = self) + if react_to?(ex) + begin + ex.command = cmd + ex.react! + rescue Quickl::Error => ex2 + handle_error(ex2, cmd) + end + else + raise ex + end + end + end # module ClassMethods # Methods installed on all command instances module InstanceMethods @@ -118,43 +135,16 @@ # Delegate unrecognized calls to the command class # (gives access to options, help, usage, ...) def method_missing(name, *args, &block) if self.class.respond_to?(name) + Quickl.deprecated(name, "self.class.#{name}", caller) self.class.send(name, *args, &block) else super end end - - # Handles a command error - def handle_error(ex) - if react_to?(ex) - begin - ex.command = self - ex.react! - rescue Quickl::Error => ex2 - handle_error(ex2) - end - else - raise ex - end - end - - # - # Runs the command from a requester file with command-line - # arguments. - # - # This method is intended to be overriden and does nothing - # by default. - # - def run(argv, requester = nil) - @requester = requester - _run(argv) - rescue Quickl::Error => ex - handle_error(ex) - end - + end # module InstanceMethods # Tracks child classes def self.inherited(command) Quickl.build_command(command)