Skip to content

Commit

Permalink
added support for yielding non-options to 'parse' (thanks banisterfiend)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Jarvis committed Mar 17, 2011
1 parent 041cc6b commit cf84f76
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/slop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def initialize(&block)
@options = Options.new
@banner = nil
@longest_flag = 0
@items = []

if block_given?
block.arity == 1 ? yield(self) : instance_eval(&block)
Expand All @@ -59,7 +60,13 @@ def banner(text=nil)
#
# @param items
def parse(items=ARGV)
parse_items items
@items = parse_items items, block_given?

if block_given?
@items.each { |item| yield item }
end

@items
end

# Parse a list of options, removing parsed options from the original Array.
Expand Down
9 changes: 9 additions & 0 deletions test/slop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def parse(items, &block)
assert_kind_of Slop, slop
end

test 'yielding non-options when a block is passed to "parse"' do
opts = Slop.new do
on :name, true
end
opts.parse(%w/--name lee a/) do |v|
assert_equal 'a', v
end
end

test 'setting the banner' do
slop = Slop.new
slop.banner = "foo bar"
Expand Down

0 comments on commit cf84f76

Please sign in to comment.