diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-11-09 05:27:58 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-11-09 05:27:58 +0000 |
commit | 4c718c753e4fa121c76fa5d73055f2d92c0fce8f (patch) | |
tree | 93e3fcb187c0aca80f4be00e700cf1d36a75e042 /lib/optparse.rb | |
parent | 7c5c394f097819c30bb7955704a0f5030d0b1743 (diff) | |
download | ruby-4c718c753e4fa121c76fa5d73055f2d92c0fce8f.tar.gz |
* lib/optparse.rb (OptionParser::Officious): moved from DefaultList.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/optparse.rb')
-rw-r--r-- | lib/optparse.rb | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index 95ed84884f..01facfe02d 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -718,30 +718,38 @@ class OptionParser DefaultList.long[''] = Switch::NoArgument.new {throw :terminate} # - # Default options, which never appear in option summary. + # OptionParser::Officious + # Default options for ARGV, which never appear in option summary. + # + Officious = {} + # --help # Shows option summary. + Officious['help'] = proc do |parser| + Switch::NoArgument.new do + puts parser.help + exit + end + end + # --version # Shows version string if (({::Version})) is defined. - # - DefaultList.long['help'] = Switch::NoArgument.new do - puts ARGV.options - exit - end - DefaultList.long['version'] = Switch::OptionalArgument.new do |pkg| - if pkg - begin - require 'optparse/version' - rescue LoadError - pkg = nil - else - show_version(*pkg.split(/,/)) + Officious['version'] = proc do |parser| + Switch::OptionalArgument.new do |pkg| + if pkg + begin + require 'optparse/version' + rescue LoadError + else + show_version(*pkg.split(/,/)) or + abort("#{parser.program_name}: no version found in package #{pkg}") + exit + end end + v = parser.ver or abort("#{parser.program_name}: version unknown") + puts v + exit end - unless pkg - v = ARGV.options.ver and puts v - end - exit end # :startdoc: @@ -809,9 +817,18 @@ class OptionParser @banner = banner @summary_width = width @summary_indent = indent + add_officious yield self if block_given? end + # :nodoc: + def add_officious + list = base() + Officious.each_pair do |opt, block| + list.long[opt] ||= block.call(self) + end + end + =begin --- OptionParser.terminate([arg]) Terminates option parsing. Optional parameter ((|arg|)) would be @@ -907,7 +924,7 @@ class OptionParser attr_writer :version, :release def version - @version || (defined?(::Version) && ::Version) || (defined?(::VERSION) && ::VERSION) + @version || (defined?(::Version) && ::Version) end def release @@ -1562,6 +1579,21 @@ class OptionParser s end +=begin +: Regexp + Regular expression with option. +=end + accept(Regexp, %r"\A/((?:\\.|[^\\])*)/([[:alpha:]]+)?\z|.*") do |all, s, o| + f = 0 + if o + f |= Regexp::IGNORECASE if /i/ =~ o + f |= Regexp::MULTILINE if /m/ =~ o + f |= Regexp::EXTENDED if /x/ =~ o + k = o.delete("^imx") + end + Regexp.new(s || all, f, k) + end + =begin = Exceptions @@ -1644,7 +1676,7 @@ argument. ((<OptionParser::ParseError>)) =end #'#"#`# class NeedlessArgument < ParseError - const_set(:Reason, 'needles argument'.freeze) + const_set(:Reason, 'needless argument'.freeze) end =begin |