Class: Ronin::UI::CLI::Command
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Ronin::UI::CLI::Command
- 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 .stuff?
if [:syntax]
print_info "Using syntax #{[:syntax]}"
end
if [:includes]
print_info "Including:"
print_array [: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
Direct Known Subclasses
Ronin::UI::CLI::Commands::Console, Ronin::UI::CLI::Commands::Database, Ronin::UI::CLI::Commands::Help, ModelCommand
Class Method Summary collapse
-
.banner ⇒ String
protected
private
The banner for the command.
-
.command_name ⇒ Object
Returns the name of the command.
-
.inherited(super_class) ⇒ Object
private
Sets the namespace of a new Command class.
-
.run(options = {}, arguments = []) ⇒ Command
Runs the command.
Instance Method Summary collapse
-
#execute ⇒ Object
Default method to call after the options have been parsed.
-
#indent(n = 2) { ... } ⇒ nil
protected
Increases the indentation out output temporarily.
-
#initialize(arguments = [], opts = {}, config = {}) ⇒ Command
constructor
Creates a new Command object.
-
#print_array(array, options = {}) ⇒ nil
protected
Prints a given Array.
-
#print_exception(exception) ⇒ Object
protected
Prints an exception and a shortened backtrace.
-
#print_hash(hash, options = {}) ⇒ nil
protected
Prints a given Hash.
-
#print_section(title) { ... } ⇒ Object
protected
Prints a section with a title.
-
#print_title(title) ⇒ Object
protected
Prints a given title.
-
#puts(*messages) ⇒ Object
protected
Print the given messages with indentation.
-
#setup ⇒ Object
protected
Default method to call before #execute.
Constructor Details
#initialize(arguments = [], opts = {}, config = {}) ⇒ Command
Creates a new Command object.
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
.banner ⇒ String (protected)
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.
215 216 217 |
# File 'lib/ronin/ui/cli/command.rb', line 215 def self. "ronin #{self_task.formatted_usage(self,false,true)}" end |
.command_name ⇒ Object
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.
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.
167 168 169 170 171 172 |
# File 'lib/ronin/ui/cli/command.rb', line 167 def self.run(={},arguments=[]) command = self.new(arguments,) command.invoke_all() return command end |
Instance Method Details
#execute ⇒ Object
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.
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 |
#print_array(array, options = {}) ⇒ nil (protected)
Prints a given Array.
318 319 320 321 322 323 324 325 326 327 |
# File 'lib/ronin/ui/cli/command.rb', line 318 def print_array(array,={}) print_title([:title]) if [:title] indent do array.each { |value| puts value } end puts if [:title] return nil end |
#print_exception(exception) ⇒ Object (protected)
Prints an exception and a shortened backtrace.
373 374 375 376 377 378 379 |
# File 'lib/ronin/ui/cli/command.rb', line 373 def print_exception(exception) print_error exception. (0..5).each do |i| print_error ' ' + exception.backtrace[i] end end |
#print_hash(hash, options = {}) ⇒ nil (protected)
Prints a given Hash.
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,={}) align = hash.keys.map { |name| name.to_s.length }.max print_title([:title]) if [:title] indent do hash.each do |name,value| name = "#{name}:".ljust(align) puts "#{name}\t#{value}" end end puts if [:title] return nil end |
#print_section(title) { ... } ⇒ Object (protected)
Prints a section with a title.
297 298 299 300 |
# File 'lib/ronin/ui/cli/command.rb', line 297 def print_section(title,&block) print_title(title) indent(&block) end |
#print_title(title) ⇒ Object (protected)
Prints a given title.
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.
270 271 272 |
# File 'lib/ronin/ui/cli/command.rb', line 270 def puts(*) super(*(.map { |mesg| (' ' * @indent) + mesg.to_s })) end |
#setup ⇒ Object (protected)
Default method to call before #execute.
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..verbose? Output.quiet! if self..quiet? Output.silent! if self..silent? Output.handler = if self..color? Output::Terminal::Color else Output::Terminal::Raw end end |