A super-class ofr Commands with structure.
This class implements the bare-minimum functionality that a command should have, namely a --help switch, and then delegates actual processing to its subclasses.
Create subclasses using {Pry::CommandSet#create_command}, and override the {options(opt)} method to set up an instance of Slop, and the {process} method to actually run the command. If necessary, you can also override {setup} which will be called before {options}, for example to require any gems your command needs to run, or to set up state.
Set up {opts} and {args}, and then call {process}
This function will display help if necessary.
@param *String the arguments passed @return Object the return value of {process} or VOID_VALUE
# File lib/pry/command.rb, line 439 def call(*args) setup self.opts = slop self.args = self.opts.parse!(args) if opts.present?(:help) output.puts slop.help void else process(*correct_arg_arity(method(:process).arity, args)) end end
Return the help generated by Slop for this command.
# File lib/pry/command.rb, line 454 def help slop.help end
A function to setup Slop so it can parse the options your command expects.
NOTE: please don't do anything side-effecty in the main part of this method, as it may be called by Pry at any time for introspection reasons. If you need to set up default values, use {setup} instead.
@example
def options(opt)
opt.banner "Gists methods or classes"
opt.on(:c, :class, "gist a class") do
@action = :class
end
end
# File lib/pry/command.rb, line 492 def options(opt); end
The actual body of your command should go here.
The {opts} mehod can be called to get the options that Slop has passed, and {args} gives the remaining, unparsed arguments.
The return value of this method is discarded unless the command was created with :keep_retval => true, in which case it is returned to the repl.
@example
def process
if opts.present?(:class)
gist_class
else
gist_method
end
end
# File lib/pry/command.rb, line 510 def process; raise CommandError, "command '#{name}' not implemented" end
A function called just before {options(opt)} as part of {call}.
This function can be used to set up any context your command needs to run, for example requiring gems, or setting default values for options.
@example
def setup; require 'gist' @action = :method end
# File lib/pry/command.rb, line 477 def setup; end
Generated with the Darkfish Rdoc Generator 2.