Permalink
Browse files

Stop multiple switches from trashing arguments

  • Loading branch information...
1 parent d50a2d8 commit 76d09b2dc9f346e58e6ddb09e4452102bc8e952f @ConradIrwin ConradIrwin committed May 31, 2012
Showing with 14 additions and 4 deletions.
  1. +8 −4 lib/slop.rb
  2. +6 −0 test/slop_test.rb
View
12 lib/slop.rb
@@ -466,8 +466,12 @@ def execute_option(option, argument, index, item = nil)
return
end
- @trash << index + 1 unless item && item.end_with?("=#{argument}")
- option.value = argument
+ if argument
+ @trash << index + 1 unless item && item.end_with?("=#{argument}")
+ option.value = argument
+ else
+ option.value = option.count > 0
+ end
if option.match? && !argument.match(option.config[:match])
raise InvalidArgumentError, "#{argument} is an invalid argument"
@@ -485,11 +489,11 @@ def execute_option(option, argument, index, item = nil)
#
# Returns nothing.
def execute_multiple_switches(option, argument, index)
- execute_option(option, argument, index)
+ execute_option(option, nil, index)
argument.split('').each do |key|
opt = fetch_option(key)
opt.count += 1
- execute_option(opt, argument, index, key)
+ execute_option(opt, nil, index, key)
end
end
View
6 test/slop_test.rb
@@ -218,6 +218,12 @@ def temp_stderr
assert_equal 'abc123', opts[:f]
end
+ test "muiltiple_switches should not trash arguments" do
+ opts = Slop.new{ on :f; on :b }
+ args = opts.parse!(%w'-fb foo')
+ assert_equal %w'foo', args
+ end
+
test "setting/getting the banner" do
opts = Slop.new :banner => 'foo'
assert_equal 'foo', opts.banner

0 comments on commit 76d09b2

Please sign in to comment.