From ddfeb867df425a196ddb6b3ca4209a822296ba32 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sat, 31 Dec 2011 00:55:22 +0000 Subject: [PATCH] Add a command_class --- lib/pry/command_set.rb | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index bf465503..a5d421ec 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -70,6 +70,33 @@ class Pry end end + class ClassCommand < Command + attr_accessor :opts + attr_accessor :args + + def call(*args) + self.opts = slop + self.args = self.opts.parse!(args) + + if opts.present?(:help) + output.puts slop.help + else + run + end + end + + def options(opt); end + + def slop + Slop.new do |opt| + options(opt) + opt.on(:h, :help, "Show this message.") + end + end + + def run; raise CommandError, "command '#{name}' not implemented" end + end + include Enumerable include Pry::Helpers::BaseHelpers @@ -161,6 +188,25 @@ class Pry end end + def command_class(name, description="No description.", options={}, &block) + options = { + :requires_gem => [], + :keep_retval => false, + :argument_required => false, + :interpolate => true, + :shellwords => true, + :listing => name, + :use_prefix => true + }.merge!(options) + + if command_dependencies_met? options + commands[name] = ClassCommand.subclass(name, description, options) + commands[name].class_eval(&block) + else + commands[name] = StubCommand.subclass(name, description, options) + end + end + # 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 # is evaluated in the same context.