1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Class command API polish

This commit is contained in:
Conrad Irwin 2012-01-08 12:48:54 -08:00
parent 9bbc29955c
commit 5b533fe4f1
2 changed files with 38 additions and 11 deletions

View file

@ -14,12 +14,34 @@ class Pry
# Properties of the command itself (as passed as arguments to
# {CommandSet#command} or {CommandSet#command_class}).
class << self
attr_accessor :block
attr_accessor :name
attr_accessor :description
attr_accessor :options
attr_accessor :block
attr_accessor :command_options
# Define or get the command's description
def description(arg=nil)
@description = arg if arg
@description
end
# Define or get the command's options
def command_options(arg=nil)
@command_options = arg if arg
@command_options
end
# backward compatibility
alias_method :options, :command_options
alias_method :options=, :command_options=
# Define or get the command's banner
def banner(arg=nil)
@banner = arg if arg
@banner || description
end
end
# Make those properties accessible to instances
def name; self.class.name; end
def description; self.class.description; end
@ -47,7 +69,7 @@ class Pry
klass.send(:include, helpers)
klass.name = name
klass.description = description
klass.options = options
klass.command_options = options
klass.block = block
klass
end
@ -276,10 +298,19 @@ class Pry
# backwards compatibility
alias_method :opts, :context
# Call the block that was registered with this command.
#
# @param *String the arguments passed
# @return Object the return value of the block
def call(*args)
instance_exec(*correct_arg_arity(block.arity, args), &block)
end
# Fix the number of arguments we pass to a block to avoid arity warnings.
#
# @param Number the arity of the block
# @param Array the arguments to pass
# @return Array a (possibly shorter) array of the arguments to pass
def correct_arg_arity(arity, args)
case
when arity < 0
@ -304,12 +335,6 @@ class Pry
# 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.
class ClassCommand < Command
class << self
def banner(arg=nil)
@banner = arg if arg
@banner || description
end
end
attr_accessor :opts
attr_accessor :args

View file

@ -81,11 +81,12 @@ class Pry
# # hello john, nice number: 10
# # pry(main)> help number
# # number-N regex command
def command(name, description="No description.", options={}, &block)
def block_command(name, description="No description.", options={}, &block)
options = default_options(name).merge!(options)
commands[name] = Pry::BlockCommand.subclass(name, description, options, helper_module, &block)
end
alias_method :command, :block_command
# Defines a new Pry command class.
#
@ -112,13 +113,14 @@ class Pry
# end
# end
#
def command_class(name, description="No description.", options={}, &block)
def new_command(name, description="No description.", options={}, &block)
options = default_options(name).merge!(options)
commands[name] = Pry::ClassCommand.subclass(name, description, options, helper_module, &block)
commands[name].class_eval(&block)
commands[name]
end
alias_method :command_class, :new_command
# Execute a block of code before a command is invoked. The block also
# gets access to parameters that will be passed to the command and