Method: Pry::CommandSet#create_command

Defined in:
lib/pry/command_set.rb

#create_command(match, description = "No description.", options = {}) { ... } ⇒ Object

Defines a new Pry command class.

Examples:

Pry::Commands.create_command "echo", "echo's the input", :shellwords => false do
  def options(opt)
    opt.banner "Usage: echo [-u | -d] <string to echo>"
    opt.on :u, :upcase, "ensure the output is all upper-case"
    opt.on :d, :downcase, "ensure the output is all lower-case"
  end

  def process
    if opts.present?(:u) && opts.present?(:d)
      raise Pry::CommandError, "-u and -d makes no sense"
    end
    result = args.join(" ")
    result.downcase! if opts.present?(:downcase)
    result.upcase! if opts.present?(:upcase)
    output.puts result
  end
end

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, see #command

Yields:

  • The class body's definition.


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pry/command_set.rb', line 117

def create_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::ClassCommand.subclass(
    match, description, options, helper_module, &block
  )
  @commands[match].class_eval(&block)
  @commands[match]
end