mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Add a command_class
This commit is contained in:
parent
fd4506f88c
commit
ddfeb867df
1 changed files with 46 additions and 0 deletions
|
@ -70,6 +70,33 @@ class Pry
|
||||||
end
|
end
|
||||||
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 Enumerable
|
||||||
include Pry::Helpers::BaseHelpers
|
include Pry::Helpers::BaseHelpers
|
||||||
|
|
||||||
|
@ -161,6 +188,25 @@ class Pry
|
||||||
end
|
end
|
||||||
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
|
# 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
|
# gets access to parameters that will be passed to the command and
|
||||||
# is evaluated in the same context.
|
# is evaluated in the same context.
|
||||||
|
|
Loading…
Reference in a new issue