lib/ronin/ui/command_line/command.rb in ronin-0.2.4 vs lib/ronin/ui/command_line/command.rb in ronin-0.3.0

- old
+ new

@@ -1,9 +1,7 @@ # -#-- -# Ronin - A Ruby platform designed for information security and data -# exploration tasks. +# Ronin - A Ruby platform for exploit development and security research. # # Copyright (c) 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,130 +14,201 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # 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/options' +require 'ronin/ui/output' +require 'thor' +require 'extlib' + module Ronin module UI module CommandLine - class Command + class Command < Thor - # The name of the command - attr_reader :name + include Thor::Actions + include Output::Helpers - # The options for the command - attr_reader :options + default_task :default + map '-h' => :help # # Creates a new Command object. # - def initialize(name,&block) - @name = name.to_s + # @param [Array] arguments + # Command-line arguments. + # + # @param [Hash] options + # Additional command-line options. + # + # @param [Hash] config + # Additional configuration. + # + def initialize(arguments=[],options={},config={}) + @indent = 0 - defaults() + super(arguments,options,config) + end - Options.new(@name) do |opts| - define_options(opts) - - @options = opts + def self.start(arguments=ARGV,config={}) + unless map[arguments.first.to_s] + arguments = [default_task] + arguments end - block.call(self) if block + super(arguments,config) end - # - # Creates a new command object and runs it with the given _args_. - # - def self.run(*args) - name = File.basename($0) - name.gsub!(/^ronin-/,'') + no_tasks do + def invoke(task,arguments) + UI::Output.verbose = options.verbose? + UI::Output.quiet = options.quiet? + UI::Output.silent = options.silent? - cmd = self.new(name) + if options.nocolor? + UI::Output::Handler.color = false + end - begin - cmd.arguments(*(cmd.options.parse(args))) - rescue OptionParser::MissingArgument, - OptionParser::InvalidOption => e - STDERR.puts e - exit -1 + super(task,arguments) end - - return true end + desc "command [ARGS...]", "default task to run" + method_option :verbose, :type => :boolean, :default => false, :aliases => '-v' + method_option :quiet, :type => :boolean, :default => true, :aliases => '-q' + method_option :silent, :type => :boolean, :default => true, :aliases => '-Q' + method_option :color, :type => :boolean, :default => true + method_option :nocolor, :type => :boolean, :default => false + # - # Prints the help information for the command. + # Default method to call after the options have been parsed. # - def self.help - self.new.help + def default(*arguments) end + desc "help", "displays the help for the command" + # - # Prints the help information for the command. + # Prints the help information for the command and exists. # def help - @options.help - return self + self.class.help( + shell, + self.class.default_task, + :short => false, + :ident => 2, + :namespace => false + ) end - # - # Returns the String form of the command. - # - def to_s - @name.to_s - end - protected # - # Prints the specified error _message_. + # Returns the name of the command. # - def error(message) - STDERR.puts "ronin: #{@name}: #{message}" - return false + def name + self.class.name.split('::').last.snake_case end # - # Calls the specified _block_, then exists with the status code of 0. + # Increases the indentation out output temporarily. # - def success(&block) - block.call - exit 0 + # @param [Integer] n + # The number of spaces to increase the indentation by. + # + # @yield [] + # The block will be called after the indentation has been + # increased. After the block has returned, the indentation will + # be returned to normal. + # + # @return [nil] + # + def indent(n=2,&block) + @indent += n + + block.call() + + @indent -= n + return nil 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. + # Print the given messages with indentation. # - def fail(message,&block) - block.call() if block - - error(message) - exit -1 + # @param [Array] messages + # The messages to print, one per-line. + # + def puts(*messages) + super(*(messages.map { |mesg| (' ' * @indent) + mesg.to_s })) end # - # Setup the command default values. + # Prints a given title. # - def defaults + # @param [String] title + # The title to print. + # + def print_title(title) + puts "[ #{title} ]" end # - # Define the command-line options for the command. + # Prints a given Array. # - def define_options(opts) + # @param [Array] array + # The Array to print. + # + # @param [Hash] options + # Additional options. + # + # @option options [String] :title + # The optional title to print before the contents of the Array. + # + # @return [nil] + # + def print_array(array,options={}) + print_title(options[:title]) if options[:title] + + indent do + array.each { |value| puts value } + end + + puts "\n" if options[:title] + return nil end # - # Processes the additional arguments specified by _args_. + # Prints a given Hash. # - def arguments(*args) + # @param [Hash] hash + # The Hash to print. + # + # @param [Hash] options + # Additional options. + # + # @option options [String] :title + # The optional title to print before the contents of the Hash. + # + # @return [nil] + # + def print_hash(hash,options={}) + align = hash.keys.map { |name| + name.to_s.length + }.max + + print_title(options[:title]) if options[:title] + + indent do + hash.each do |name,value| + name = "#{name}:".ljust(align) + puts "#{name}\t#{value}" + end + end + + puts "\n" if options[:title] + return nil end end end end