lib/ronin/ui/command_line/command_line.rb in ronin-0.2.0 vs lib/ronin/ui/command_line/command_line.rb in ronin-0.2.1

- old
+ new

@@ -19,39 +19,45 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #++ # -require 'ronin/ui/command_line/commands/default' require 'ronin/ui/command_line/exceptions/unknown_command' +require 'ronin/ui/command_line/commands/console' require 'ronin/ui/console' -require 'ronin/version' +require 'reverse_require' + module Ronin module UI module CommandLine # # Returns the commands registered with the command-line utility. # def CommandLine.commands - @@ronin_commands ||= [] - end + unless class_variable_defined?('@@ronin_commands') + paths = Gem.find_resources('bin/ronin-*') + + @@ronin_commands = {} + + paths.each do |path| + next unless File.executable?(path) + name = File.basename(path).gsub(/^ronin-/,'') - # - # Returns the Hash of the Command names and their Command objects - # registered with the command-line utility. - # - def CommandLine.commands_by_name - @@ronin_commands_by_name ||= {} + @@ronin_commands[name] ||= path + end + end + + return @@ronin_commands end # # Returns +true+ if the a Command with the specified _name_ was # registered with the command-line utility. # def CommandLine.has_command?(name) - CommandLine.commands_by_name.has_key?(name.to_s) + CommandLine.commands.has_key?(name.to_s) end # # Returns the Command registered with the command-line utility # with the specified _name_. @@ -61,58 +67,30 @@ unless CommandLine.has_command?(name) raise(UnknownCommand,"unknown command #{name.dump}",caller) end - return CommandLine.commands_by_name[name] + return CommandLine.commands[name] end # - # Prints the specified error _message_. - # - def CommandLine.error(message) - STDERR.puts "ronin: #{message}" - return false - end - - # - # Prints the given error _message_ and exits unseccessfully from the - # command-line utility. If a _block_ is given, it will be called before - # any error _message_ are printed. - # - def CommandLine.fail(message,&block) - block.call(self) if block - - CommandLine.error(message) - exit -1 - end - - # # Runs the command-line utility with the given _argv_ Array. If the # first argument is a sub-command name, the command-line utility will # attempt to find and execute the Command with the same name. # def CommandLine.run(*argv) - begin - if (argv.empty? || argv[0][0..0]=='-') - DefaultCommand.run(*argv) - else - cmd = argv.first - argv = argv[1..-1] + if (argv.empty? || argv[0][0..0]=='-') + Commands::Console.run(*argv) + else + cmd = argv.first + argv = argv[1..-1] - if CommandLine.has_command?(cmd) - begin - CommandLine.commands_by_name[cmd].run(*argv) - rescue => excp - STDERR.puts excp - exit -1 - end - else - CommandLine.fail("unknown command #{cmd.dump}") - end + begin + exec(CommandLine.get_command(cmd),*argv) + rescue UnknownCommand => e + STDERR.puts e + exit -1 end - rescue OptionParser::MissingArgument, OptionParser::InvalidOption => e - CommandLine.fail(e) end return true end end