From 76d09b2dc9f346e58e6ddb09e4452102bc8e952f Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 31 May 2012 00:32:41 -0700 Subject: [PATCH] Stop multiple switches from trashing arguments --- lib/slop.rb | 12 ++++++++---- test/slop_test.rb | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/slop.rb b/lib/slop.rb index 8dbd400b..6dad6877 100644 --- a/lib/slop.rb +++ b/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 diff --git a/test/slop_test.rb b/test/slop_test.rb index 0516a237..e8f03d0f 100644 --- a/test/slop_test.rb +++ b/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