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

Fix subcommands

A few things were missing. I had to add a new method and slightly adjust
ClassCommand#slop method. Without these changes subcommands doesn't work
properly.

Add some unit tests for subcommands.

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
Kyrylo Silin 2012-11-26 18:07:32 +02:00
parent d39af3358c
commit 8a39813708
2 changed files with 70 additions and 25 deletions

View file

@ -552,7 +552,7 @@ class Pry
self.get(key) || default.get(key)
end
# Check for a default options presence.
# Check for default options presence.
#
# @param [String, Symbol] keys The list of keys to check.
# @return [Boolean] Whether all of the +keys+ are present in the parsed
@ -561,6 +561,22 @@ class Pry
default.present?(*keys)
end
# Check for a command presence.
#
# @example
# opts.parse %w'install'
# opts.command?(:install)
# # => true
# opts.command?(:list)
# # => false
#
# @param [Symbol, String] name The name of the command to be checked
# @return [Boolean] `true` if the given +name+ is present in the parsed
# arguments
def command?(name)
__getobj__.present?(name)
end
# Convenience method for {#present?}.
#
# @example
@ -622,15 +638,14 @@ class Pry
# Return an instance of Slop::Commands that can parse either subcommands
# or the options that this command accepts.
def slop
opts = proc do |opt|
Slop::Commands.new do |cmd|
subcommands(cmd)
cmd.default do |opt|
opt.banner(unindent(self.class.banner))
options(opt)
opt.on(:h, :help, "Show this message.")
end
Slop::Commands.new do |cmd|
subcommands(cmd)
cmd.default { |opt| opts.call(opt) }
end
end

View file

@ -301,23 +301,6 @@ describe "Pry::Command" do
mock_command(cmd, %w(--four 4 four))
end
it 'should provide cmds and args as provided by slop' do
cmd = @set.create_command 'dichlorvos', 'Kill insects' do
def subcommands(cmd)
cmd.on :kill do
on :i, :insect, "An insect."
end
end
def process
args.should == ["ant"]
opts[:kill][:insect].should == true
end
end
mock_command(cmd, %w(kill --insect ant))
end
it 'should allow overriding options after definition' do
cmd = @set.create_command /number-(one|two)/, "Lieutenants of the Golgafrinchan Captain", :shellwords => false do
@ -327,6 +310,53 @@ describe "Pry::Command" do
cmd.command_options[:shellwords].should == false
cmd.command_options[:listing].should == 'number-one'
end
it "should create subcommands" do
cmd = @set.create_command 'mum', 'Your mum' do
def subcommands(cmd)
cmd.on :yell
end
def process
opts.command?(:blahblah).should == false
opts.command?(:yell).should == true
end
end
mock_command(cmd, ['yell'])
end
it "should create subcommand options" do
cmd = @set.create_command 'mum', 'Your mum' do
def subcommands(cmd)
cmd.on :yell do |opt|
opt.on :p, :person
end
end
def process
args.should == ['papa']
opts[:yell][:person].should == true
opts[:yell].present? :person
end
end
mock_command(cmd, %w|yell --person papa|)
end
it "should accept top-level arguments" do
cmd = @set.create_command 'mum', 'Your mum' do
def subcommands(cmd)
cmd.on :yell
end
def process
opts.arguments.should == ['papa', 'sonny', 'daughter']
end
end
mock_command(cmd, %w|yell papa sonny daughter|)
end
end
describe 'tokenize' do