Parent

Pry::ClassCommand

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.

Attributes

args[RW]
opts[RW]

Public Instance Methods

call(*args) click to toggle source

Set up `opts` and `args`, and then call `process`.

This function will display help if necessary.

@param [Array<String>] args The arguments passed @return [Object] The return value of `process` or VOID_VALUE

# File lib/pry/command.rb, line 462
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
help() click to toggle source

Return the help generated by Slop for this command.

# File lib/pry/command.rb, line 477
def help
  slop.help
end
options(opt) click to toggle source

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 515
def options(opt); end
process() click to toggle source

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 534
def process; raise CommandError, "command '#{command_name}' not implemented" end
setup() click to toggle source

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 500
def setup; end
slop() click to toggle source

Return an instance of Slop that can parse the options that this command accepts.

# File lib/pry/command.rb, line 482
def slop
  Slop.new do |opt|
    opt.banner(unindent(self.class.banner))
    options(opt)
    opt.on(:h, :help, "Show this message.")
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.