Created
April 14, 2011 13:21
-
-
Save wjessop/919463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
index 57b442a..8264475 100644 | |
--- a/lib/trollop.rb | |
+++ b/lib/trollop.rb | |
@@ -362,7 +362,7 @@ class Parser | |
params = given_data[:params] | |
opts = @specs[sym] | |
- raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag | |
+ raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag and not opts[:allow_blank] | |
vals["#{sym}_given".intern] = true # mark argument as specified on the commandline | |
@@ -383,7 +383,7 @@ class Parser | |
if SINGLE_ARG_TYPES.include?(opts[:type]) | |
unless opts[:multi] # single parameter | |
- vals[sym] = vals[sym][0][0] | |
+ vals[sym] = vals[sym][0][0] rescue nil | |
else # multiple options, each with a single parameter | |
vals[sym] = vals[sym].map { |p| p[0] } | |
end | |
diff --git a/test/test_trollop.rb b/test/test_trollop.rb | |
index 687c209..78d554b 100644 | |
--- a/test/test_trollop.rb | |
+++ b/test/test_trollop.rb | |
@@ -42,14 +42,16 @@ class Trollop < ::Test::Unit::TestCase | |
assert_raise(CommandlineError) { @p.parse(%w(--arg2 --arg3)) } | |
end | |
- ## flags that take an argument error unless given one | |
+ ## flags that take an argument error unless given one unless the argument is optional | |
def test_argflags_demand_args | |
@p.opt "goodarg", "desc", :type => String | |
@p.opt "goodarg2", "desc", :type => String | |
- | |
+ @p.opt "goodarg3", "desc", :type => String, :allow_blank => true | |
+ | |
assert_nothing_raised { @p.parse(%w(--goodarg goat)) } | |
assert_raise(CommandlineError) { @p.parse(%w(--goodarg --goodarg2 goat)) } | |
assert_raise(CommandlineError) { @p.parse(%w(--goodarg)) } | |
+ assert_nothing_raised { @p.parse(%w(--goodarg3)) } | |
end | |
## flags that don't take arguments ignore them |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment