class Pry::ClassCommand

A super-class of 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 Class Methods

doc() click to toggle source
# File lib/pry/command.rb, line 526
def doc
  new.help
end
file() click to toggle source
Alias for: source_file
inherited(klass) click to toggle source

Ensure that subclasses inherit the options, description and match from a ClassCommand super class.

# File lib/pry/command.rb, line 516
def inherited(klass)
  klass.match  match
  klass.description  description
  klass.command_options options
end
line() click to toggle source
Alias for: source_line
source() click to toggle source
# File lib/pry/command.rb, line 522
def source
  source_object.source
end
source_file() click to toggle source
# File lib/pry/command.rb, line 534
def source_file
  source_object.source_file
end
Also aliased as: file
source_line() click to toggle source
# File lib/pry/command.rb, line 539
def source_line
  source_object.source_line
end
Also aliased as: line
source_location() click to toggle source
# File lib/pry/command.rb, line 530
def source_location
  source_object.source_location
end

Public Instance Methods

call(*args) click to toggle source

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

This method 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 569
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
complete(search) click to toggle source

Generate shell completions @param [String] search The line typed so far @return [Array<String>] the words to complete

# File lib/pry/command.rb, line 602
def complete(search)
  slop.map do |opt|
    [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
  end.flatten(1).compact + super
end
help() click to toggle source

Return the help generated by Slop for this command.

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

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

A method called just before `options(opt)` as part of `call`.

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

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

# File lib/pry/command.rb, line 590
def slop
  Slop.new do |opt|
    opt.banner(unindent(self.class.banner))
    subcommands(opt)
    options(opt)
    opt.on :h, :help, 'Show this message.'
  end
end
subcommands(cmd) click to toggle source

A method to setup Slop commands so it can parse the subcommands your command expects. If you need to set up default values, use `setup` instead.

@example A minimal example

def subcommands(cmd)
  cmd.command :download do |opt|
    description 'Downloads a content from a server'

    opt.on :verbose, 'Use verbose output'

    run do |options, arguments|
      ContentDownloader.download(options, arguments)
    end
  end
end

@example Define the invokation block anywhere you want

def subcommands(cmd)
  cmd.command :download do |opt|
    description 'Downloads a content from a server'

    opt.on :verbose, 'Use verbose output'
  end
end

def process
  # Perform calculations...
  opts.fetch_command(:download).run do |options, arguments|
    ContentDownloader.download(options, arguments)
  end
  # More calculations...
end
# File lib/pry/command.rb, line 653
def subcommands(cmd); end