Method: Pry::CommandSet#block_command

Defined in:
lib/pry/command_set.rb

#block_command(match, description = "No description.", options = {}) { ... } ⇒ Object Also known as: command

Defines a new Pry command.

Examples:

MyCommands = Pry::CommandSet.new do
  command "greet", "Greet somebody" do |name|
    puts "Good afternoon #{name.capitalize}!"
  end
end

# From pry:
# pry(main)> pry_instance.commands = MyCommands
# pry(main)> greet john
# Good afternoon John!
# pry(main)> help greet
# Greet somebody

Regexp command

MyCommands = Pry::CommandSet.new do
  command(
    /number-(\d+)/, "number-N regex command", :listing => "number"
  ) do |num, name|
    puts "hello #{name}, nice number: #{num}"
  end
end

# From pry:
# pry(main)> pry_instance.commands = MyCommands
# pry(main)> number-10 john
# hello john, nice number: 10
# pry(main)> help number
# number-N regex command

Parameters:

  • match (String, Regexp)

    The start of invocations of this command.

  • description (String) (defaults to: "No description.")

    A description of the command.

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

    The optional configuration parameters.

Options Hash (options):

  • :keep_retval (Boolean)

    Whether or not to use return value of the block for return of `command` or just to return `nil` (the default).

  • :interpolate (Boolean)

    Whether string #{} based interpolation is applied to the command arguments before executing the command. Defaults to true.

  • :listing (String)

    The listing name of the command. That is the name by which the command is looked up by help and by show-source. Necessary for commands with regex matches.

  • :use_prefix (Boolean)

    Whether the command uses `Pry.config.command_prefix` prefix (if one is defined). Defaults to true.

  • :shellwords (Boolean)

    Whether the command's arguments should be split using Shellwords instead of just split on spaces. Defaults to true.

Yields:

  • The action to perform. The parameters in the block determines the parameters the command will receive. All parameters passed into the block will be strings. Successive command parameters are separated by whitespace at the Pry prompt.


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pry/command_set.rb', line 78

def block_command(match, description = "No description.", options = {}, &block)
  if description.is_a?(Hash)
    options = description
    description = "No description."
  end
  options = Pry::Command.default_options(match).merge!(options)

  @commands[match] = Pry::BlockCommand.subclass(
    match, description, options, helper_module, &block
  )
end