From 5b533fe4f15c645040ee353db080eec2dccdafc5 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 8 Jan 2012 12:48:54 -0800 Subject: [PATCH] Class command API polish --- lib/pry/command.rb | 43 +++++++++++++++++++++++++++++++++--------- lib/pry/command_set.rb | 6 ++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index c5289833..dd0bc61b 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -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 diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index 3aa18ded..71924bd1 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -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