From 4cad680bc25460dd53035c0af589816baa0b3fa4 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 30 Jan 2003 08:11:21 +0000 Subject: * lib/optparse.rb (OptionParser::Switch::PlacedArgument): added. if the next argument doesn't start with '-', use it as the value. * lib/optparse.rb (OptionParser::make_switch): fixed a bug of pattern. * lib/optparse.rb (Array): no need to guard. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3421 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/optparse.rb | 65 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 24 deletions(-) (limited to 'lib/optparse.rb') diff --git a/lib/optparse.rb b/lib/optparse.rb index ad47d686e6..fd0e1394f2 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -142,12 +142,20 @@ Individual switch class. def self.guess(arg) case arg when "" - self - when /\A[=\s]?\[/ - Switch::OptionalArgument + t = self + when /\A=?\[/ + t = Switch::OptionalArgument + when /\A\s+\[/ + t = Switch::PlacedArgument else - Switch::RequiredArgument + t = Switch::RequiredArgument end + self >= t or incompatible_argument_styles(arg, t) + t + end + + def self.incompatible_argument_styles(arg, t) + raise ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}" end =begin private @@ -284,6 +292,8 @@ Switch that takes no arguments. yield(NeedlessArgument, arg) if arg super(arg) end + def self.incompatible_argument_styles(*) + end end =begin private @@ -303,11 +313,6 @@ Switch that takes an argument. end super(*parse_arg(arg, &error)) end - def self.guess(arg) - self >= (t = super) or - raise ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}" - t - end end =begin private @@ -327,10 +332,14 @@ Switch that can omit argument. super(arg) end end - def self.guess(arg) - self >= (t = super) or - raise ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}" - t + end + + class PlacedArgument < self + def parse(arg, argv, &error) + unless arg or argv.empty? or /\A-/ =~ argv[0] + arg = argv.shift + end + super(*parse_arg(arg, &error)) end end end @@ -954,15 +963,17 @@ Default options, which never appear in option summary. not_pattern, not_conv = search(:atype, o) unless not_style not_style = (not_style || default_style).guess(arg = a) if a default_style = Switch::NoArgument - default_pattern, conv = search(:atype, FalseClass) + default_pattern, conv = search(:atype, FalseClass) unless default_pattern ldesc << "--no-#{q}" long << 'no-' + (q = q.downcase) nolong << q when /^--\[no-\]([^][=\s]*)(.+)?/ q, a = $1, $2 o = notwice(a ? Object : TrueClass, klass, 'type') - default_style = default_style.guess(arg = a) if a - default_pattern, conv = search(:atype, o) + if a + default_style = default_style.guess(arg = a) + default_pattern, conv = search(:atype, o) unless default_pattern + end ldesc << "--#{q}" long << (o = q.downcase) not_pattern, not_conv = search(:atype, FalseClass) unless not_style @@ -971,32 +982,39 @@ Default options, which never appear in option summary. when /^--([^][=\s]*)(.+)?/ q, a = $1, $2 o = notwice(a ? NilClass : TrueClass, klass, 'type') - default_style = default_style.guess(arg = a) if a - default_pattern, conv = search(:atype, o) + if a + default_style = default_style.guess(arg = a) + default_pattern, conv = search(:atype, o) unless default_pattern + end ldesc << "--#{q}" long << (o = q.downcase) when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/ q, a = $1, $2 o = notwice(Object, klass, 'type') - default_style = default_style.guess(arg = a) if a - default_pattern, conv = search(:atype, o) + if a + default_style = default_style.guess(arg = a) + default_pattern, conv = search(:atype, o) unless default_pattern + end sdesc << "-#{q}" short << Regexp.new(q) when /^-(.)(.+)?/ q, a = $1, $2 o = notwice((a ? Object : TrueClass), klass, 'type') - default_style = default_style.guess(arg = a) if a - default_pattern, conv = search(:atype, o) + if a + default_style = default_style.guess(arg = a) + default_pattern, conv = search(:atype, o) unless default_pattern + end sdesc << "-#{q}" short << q when /^=/ style = notwice(default_style.guess(arg = o), style, 'style') - default_pattern, conv = search(:atype, Object) + default_pattern, conv = search(:atype, Object) unless default_pattern else desc.push(o) end end + default_pattern, conv = search(:atype, Object) unless default_pattern s = if short.empty? and long.empty? raise ArgumentError, "no switch given" if style or pattern or block desc @@ -1382,7 +1400,6 @@ Default options, which never appear in option summary. accept(Array) do |s| if s s = s.split(',').collect {|s| s unless s.empty?} - s.size > 1 or s = [s] # guard empty or single. end s end -- cgit v1.2.3