Class: Ronin::UI::CLI::Command

Inherits:
Thor::Group
  • Object
show all
Includes:
Output::Helpers, Thor::Actions
Defined in:
lib/ronin/ui/cli/command.rb

Overview

The Command class inherits Thor::Group to provide a base-class for defining commands for the Ronin::UI::CLI.

Extending

To create a new command one can inherit the Command class. The new command can define multiple class_options and arguments which Thor::Group will use to parse command-line arguments.

require 'ronin/ui/cli/command'

module Ronin
  module UI
    module CLI
      module Commands
        class MyCommand < Command

          desc 'My command'

          # command options
          class_option :stuff, :type => :boolean
          class_option :syntax, :type => :string
          class_option :includes, :type => :array

          # command arguments
          argument :path

          #
          # Executes the command.
          #
          def execute
            print_info "Stuff enabled" if options.stuff?

            if options[:syntax]
              print_info "Using syntax #{options[:syntax]}"
            end

            if options[:includes]
              print_info "Including:"
              print_array options[:includes]
            end
          end

        end
      end
    end
  end
end

Running

To run the command from Ruby, one can call the Command.run class method with the options and arguments to run the command with:

MyCommand.run(
  {:stuff => true, :syntax => 'bla', :includes => ['other']},
  ['some/file.txt']
)

To run the command from Ruby, with raw command-line options, one can call the start class method:

MyCommand.start([
  '--stuff', 'true', '--syntax', 'bla', '--includes', 'other',
  'some/file.txt'
])

Note: If MyCommand.start is not given any arguments, it will use ARGV instead.

To ensure that your command is accessible to the ronin command, make sure that the ruby file the command is defined within is in the ronin/ui/cli/commands directory of a Ronin library. If the command class is named 'MyCommand' it's ruby file must also be named 'my_command.rb'.

To run the command using the ronin command, simply specify it's underscored name:

ronin my_command some/file.txt --stuff --syntax bla \
  --includes one two

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = [], opts = {}, config = {}) ⇒ Command

Creates a new Command object.

Parameters:

  • arguments (Array) (defaults to: [])

    Command-line arguments.

  • opts (Array) (defaults to: {})

    Additional command-line options.

  • config (Hash) (defaults to: {})

    Additional configuration.



188
189
190
191
192
193
# File 'lib/ronin/ui/cli/command.rb', line 188

def initialize(arguments=[],opts={},config={})
  @indent = 0

  super(arguments,opts,config)
  setup
end

Class Method Details

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The banner for the command.

Returns:

  • (String)

    The banner string.

Since:

  • 1.0.0



215
216
217
# File 'lib/ronin/ui/cli/command.rb', line 215

def self.banner
  "ronin #{self_task.formatted_usage(self,false,true)}"
end

.command_nameObject

Returns the name of the command.



147
148
149
# File 'lib/ronin/ui/cli/command.rb', line 147

def self.command_name
  Support::Inflector.underscore(self.name.split('::').last)
end

.inherited(super_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the namespace of a new Ronin::UI::CLI::Command class.

Parameters:



138
139
140
# File 'lib/ronin/ui/cli/command.rb', line 138

def self.inherited(super_class)
  super_class.namespace(super_class.command_name)
end

.run(options = {}, arguments = []) ⇒ Command

Runs the command.

Parameters:

  • options (Hash{String,Symbol => Object}) (defaults to: {})

    Option values for the command.

  • arguments (Array<String>) (defaults to: [])

    Additional arguments for the command.

Returns:

  • (Command)

    The executed command.

Since:

  • 1.0.0



167
168
169
170
171
172
# File 'lib/ronin/ui/cli/command.rb', line 167

def self.run(options={},arguments=[])
  command = self.new(arguments,options)

  command.invoke_all()
  return command
end

Instance Method Details

#executeObject

Default method to call after the options have been parsed.



200
201
# File 'lib/ronin/ui/cli/command.rb', line 200

def execute
end

#indent(n = 2) { ... } ⇒ nil (protected)

Increases the indentation out output temporarily.

Parameters:

  • n (Integer) (defaults to: 2)

    The number of spaces to increase the indentation by.

Yields:

  • [] The block will be called after the indentation has been increased. After the block has returned, the indentation will be returned to normal.

Returns:

  • (nil)


253
254
255
256
257
258
259
260
# File 'lib/ronin/ui/cli/command.rb', line 253

def indent(n=2)
  @indent += n

  yield

  @indent -= n
  return nil
end

Prints a given Array.

Parameters:

  • array (Array)

    The Array to print.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :title (String)

    The optional title to print before the contents of the Array.

Returns:

  • (nil)


318
319
320
321
322
323
324
325
326
327
# File 'lib/ronin/ui/cli/command.rb', line 318

def print_array(array,options={})
  print_title(options[:title]) if options[:title]

  indent do
    array.each { |value| puts value }
  end

  puts if options[:title]
  return nil
end

Prints an exception and a shortened backtrace.

Parameters:

  • exception (Exception)

    The exception to print.

Since:

  • 1.0.0



373
374
375
376
377
378
379
# File 'lib/ronin/ui/cli/command.rb', line 373

def print_exception(exception)
  print_error exception.message

  (0..5).each do |i|
    print_error '  ' + exception.backtrace[i]
  end
end

Prints a given Hash.

Parameters:

  • hash (Hash)

    The Hash to print.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :title (String)

    The optional title to print before the contents of the Hash.

Returns:

  • (nil)


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/ronin/ui/cli/command.rb', line 345

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 if options[:title]
  return nil
end

Prints a section with a title.

Yields:

  • [] The block will be called after the title has been printed and indentation increased.

Since:

  • 1.0.0



297
298
299
300
# File 'lib/ronin/ui/cli/command.rb', line 297

def print_section(title,&block)
  print_title(title)
  indent(&block)
end

Prints a given title.

Parameters:

  • title (String)

    The title to print.



282
283
284
# File 'lib/ronin/ui/cli/command.rb', line 282

def print_title(title)
  puts "[ #{title} ]\n"
end

#puts(*messages) ⇒ Object (protected)

Print the given messages with indentation.

Parameters:

  • messages (Array)

    The messages to print, one per-line.



270
271
272
# File 'lib/ronin/ui/cli/command.rb', line 270

def puts(*messages)
  super(*(messages.map { |mesg| (' ' * @indent) + mesg.to_s }))
end

#setupObject (protected)

Default method to call before #execute.

Since:

  • 1.1.0



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ronin/ui/cli/command.rb', line 226

def setup
  Output.verbose! if self.options.verbose?
  Output.quiet! if self.options.quiet?
  Output.silent! if self.options.silent?

  Output.handler = if self.options.color?
                     Output::Terminal::Color
                   else
                     Output::Terminal::Raw
                   end
end