summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Jarvis <lee@jarvis.co>2011-04-16 11:29:42 +0100
committerLee Jarvis <lee@jarvis.co>2011-04-16 11:29:42 +0100
commit37561c7dc1e370fdacef7642935a7929acadf380 (patch)
treeee90882b9506ab4b069ddcb4df151a079d2d5147
parent2b4340133e908fbfbbf43a3cc8e284475db5986a (diff)
downloadslop-37561c7dc1e370fdacef7642935a7929acadf380.tar.gz
allow Slop#[] to grab a command object if it exists
-rw-r--r--lib/slop.rb2
-rw-r--r--test/slop_test.rb12
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/slop.rb b/lib/slop.rb
index 005a3ff..02047b4 100644
--- a/lib/slop.rb
+++ b/lib/slop.rb
@@ -130,7 +130,7 @@ class Slop
# @return [Object] Returns the value associated with that option.
def [](key)
option = @options[key]
- option.argument_value if option
+ option ? option.argument_value : @commands[key]
end
# Specify an option with a short or long version, description and type.
diff --git a/test/slop_test.rb b/test/slop_test.rb
index afab0b8..5648ffc 100644
--- a/test/slop_test.rb
+++ b/test/slop_test.rb
@@ -178,13 +178,21 @@ class SlopTest < TestCase
assert_equal(['c', nil, nil, false], clean_options(:c, false))
end
- test '[] returns an options argument value or nil' do
+ test '[] returns an options argument value or a command or nil (in that order)' do
slop = Slop.new
slop.opt :n, :name, true
- slop.parse %w/--name lee/
+ slop.opt :foo
+ slop.command(:foo) { }
+ slop.command(:bar) { }
+ slop.parse %w/--name lee --foo/
assert_equal 'lee', slop[:name]
assert_equal 'lee', slop[:n]
+
+ assert_equal true, slop[:foo]
+ assert_kind_of Slop, slop[:bar]
+
+ assert_nil slop[:baz]
end
test 'arguments ending ? test for option existance' do