lib/ronin/ui/cli/script_command.rb in ronin-1.3.0 vs lib/ronin/ui/cli/script_command.rb in ronin-1.4.0.rc1
- old
+ new
@@ -19,47 +19,108 @@
require 'ronin/ui/cli/model_command'
require 'ronin/ui/console'
require 'ronin/script'
+require 'parameters/options'
+
module Ronin
module UI
module CLI
#
# A base-command for querying and loading {Script}s.
#
class ScriptCommand < ModelCommand
- query_option :named, :type => :string, :aliases => '-n'
- query_option :describing, :type => :string, :aliases => '-d'
- query_option :revision, :type => :string, :aliases => '-V'
- query_option :licensed_under, :type => :string, :aliases => '-L'
+ usage '[options] --'
- class_option :file, :type => :string, :aliases => '-f'
- class_option :params, :type => :hash,
- :default => {},
- :banner => 'NAME:VALUE ...',
- :aliases => '-p'
- class_option :console, :type => :boolean, :default => false
+ query_option :named, :type => String,
+ :flag => '-n',
+ :usage => 'NAME'
+ query_option :describing, :type => String,
+ :flag => '-d',
+ :usage => 'DESC'
+
+ query_option :revision, :type => String,
+ :flag => '-V',
+ :usage => 'REV'
+
+ query_option :licensed_under, :type => String,
+ :flag => '-L',
+ :usage => 'LICENSE'
+
+ option :file, :type => String,
+ :flag => '-f',
+ :usage => 'FILE'
+
+ option :console, :type => true
+
+ argument :param_options, :type => Array,
+ :default => [],
+ :description => 'Additional options'
+
#
+ # Initializes the Script command.
+ #
+ # @param [Hash{Symbol => Object}] options
+ # Options for the script command.
+ #
+ # @api semipublic
+ #
+ def initialize(options={})
+ super(options)
+
+ @script_options = []
+ end
+
+ #
+ # Starts the script command.
+ #
+ # @param [Array<String>] argv
+ # Command-line arguments for the script command.
+ #
+ # @since 1.4.0
+ #
+ # @api semipublic
+ #
+ def start(argv=ARGV)
+ # collect the script options, upto the -- separator
+ @script_options = argv[0,argv.index('--') || argv.length]
+
+ super(argv)
+ end
+
+ #
+ # Sets up the script command, loads the script and parses additional
+ # options for the script.
+ #
+ # @since 1.4.0
+ #
+ # @api semipublic
+ #
+ def setup
+ super
+
+ load!
+ param_option_parser.parse(@param_options)
+ end
+
+ #
# Loads the script, sets its parameters and runs the script.
#
# @since 1.1.0.
#
# @api semipublic
#
def execute
- script = load_script
- script.params = options[:params]
-
- if options.console?
+ if @console
print_info "Starting the console with @script set ..."
- UI::Console.start(:script => script)
+ UI::Console.start(:script => @script)
else
- script.run
+ @script.run
end
end
protected
@@ -77,16 +138,16 @@
#
# @since 1.1.0
#
# @api semipublic
#
- def self.script_class(script)
- unless script.included_modules.include?(Script)
+ def self.script_class(script=nil)
+ if (script && !script.included_modules.include?(Script))
raise(ArgumentError,"#{script} does not include Ronin::Script")
end
- model(script)
+ return model(script)
end
#
# Loads an script using the commands options.
#
@@ -99,14 +160,55 @@
#
# @since 1.1.0
#
# @api semipublic
#
- def load_script
- if options[:file]
- self.class.model.load_from(options[:file])
- else
- query.load_first
+ def load!
+ @script = if @file
+ self.class.model.load_from(@file)
+ else
+ query.load_first
+ end
+
+ unless @script
+ print_error "Could not find or load the #{self.class.script_class.short_name}"
+ exit -1
+ end
+ end
+
+ #
+ # Creates an OptionParser based on the parameters of one or more
+ # objects.
+ #
+ # @param [Array<Parameters>] objects
+ # The objects that have parameters.
+ #
+ # @yield [opts]
+ # If a block is given, it will be passed the newly created
+ # OptionParser.
+ #
+ # @yieldparam [OptionParser] opts
+ # The newly created OptionParser for the parameters.
+ #
+ # @return [OptionParser]
+ # The configured OptionParser for the parameters.
+ #
+ # @since 1.4.0
+ #
+ # @api semipublic
+ #
+ def param_option_parser
+ OptionParser.new do |opts|
+ opts.banner = "usage: #{self.class.command_name} #{@script_options.join(' ')} -- [script_options]"
+
+ opts.separator ''
+ opts.separator "#{self.class.script_class.short_name} Options:"
+
+ @script.each_param do |param|
+ Parameters::Options.define(opts,param)
+ end
+
+ yield opts if block_given?
end
end
end
end