summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-04 11:04:17 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-04 13:32:04 -0700
commit5a0b547f3289cb8e13b197d021322e03d05bee8e (patch)
treed73d52ff19d327025d993ec88922d857dd5c22e5 /lib/puppet/application
parent8b37d7038c89bd830b076e838686419ff0068b56 (diff)
downloadpuppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.tar.gz
puppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.tar.xz
puppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.zip
(#6749) Fix optional vs mandatory argument handling.
optparse will treat '--foo --bar' as "foo with the argument --bar" when foo takes a mandatory argument. We need to emulate that behaviour in our pre-parse of the command line. Incidentally, fix up a bug in boolean options, and improve our testing. Reviewed-By: Nick Lewis <nick@puppetlabs.com>
Diffstat (limited to 'lib/puppet/application')
-rw-r--r--lib/puppet/application/string_base.rb11
1 files changed, 5 insertions, 6 deletions
diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb
index 6032e32f8..a082ba0e2 100644
--- a/lib/puppet/application/string_base.rb
+++ b/lib/puppet/application/string_base.rb
@@ -65,16 +65,15 @@ class Puppet::Application::StringBase < Puppet::Application
# non-option word to use as the action.
action = nil
index = -1
- while (index += 1) < command_line.args.length do
+ until @action or (index += 1) >= command_line.args.length do
item = command_line.args[index]
if item =~ /^-/ then
option = @string.options.find { |a| item =~ /^-+#{a}\b/ }
if option then
- if @string.get_option(option).takes_argument? then
- # We don't validate if the argument is optional or mandatory,
- # because it doesn't matter here. We just assume that errors will
- # be caught later. --daniel 2011-03-30
- index += 1 unless command_line.args[index + 1] =~ /^-/
+ option = @string.get_option(option)
+ if option.takes_argument? then
+ index += 1 unless
+ (option.optional_argument? and command_line.args[index + 1] =~ /^-/)
end
else
raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}"