summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-10 23:31:32 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-10 23:31:32 +0000
commitb55f60b59ff56a608df134df7b753b8a352a0750 (patch)
tree42ccc7677080ee4dc4b2bc57377d72e4d0fb0012 /lib
parent85c86a4e294aeee92b78c075628ec88cdf54d1ea (diff)
* lib/optparse.rb (OptionParser#getopts): new methods.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10248 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb52
1 files changed, 51 insertions, 1 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index 13528f98dc..f9f068b4c8 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1425,6 +1425,42 @@ class OptionParser
end
end
+=begin
+--- OptionParser#getopts(argv, single_options, *long_options)
+--- OptionParser.getopts(argv, single_options, *long_options)
+ Wrapper methods for getopts.rb.
+=end #'#"#`#
+ def getopts(argv, single_options, *long_options)
+ result = {}
+
+ single_options.scan(/(.)(:)?/) do |opt, val|
+ if val
+ result[opt] = nil
+ define("-#{opt} VAL") {|val| result[opt] = val}
+ else
+ result[opt] = false
+ define("-#{opt}") {result[opt] = true}
+ end
+ end if single_options
+
+ long_options.each do |arg|
+ opt, val = arg.split(':', 2)
+ if val
+ result[opt] = val.empty? ? nil : val
+ define("--#{opt} VAL") {|val| result[opt] = val}
+ else
+ result[opt] = false
+ define("--#{opt}") {result[opt] = true}
+ end
+ end
+
+ order!(argv)
+ result
+ end
+
+ def self.getopts(*args)
+ new.getopts(*args)
+ end
=begin private
--- OptionParser#visit(id, *args) {block}
@@ -1528,7 +1564,6 @@ class OptionParser
env = ENV[env] || ENV[env.upcase] or return
parse(*Shellwords.shellwords(env))
end
-
=begin
= Acceptable argument classes
@@ -1830,6 +1865,21 @@ Extends command line arguments array to parse itself.
def permute!() options.permute!(self) end
def parse!() options.parse!(self) end
+=begin
+--- OptionParser::Arguable#getopts(single_options, *long_options)
+ Substitution of getopts is possible as follows.
+
+ def getopts(*args)
+ ($OPT = ARGV.getopts(*args)).each do |opt, val|
+ eval "$OPT_#{opt.gsub(/[^A-Za-z0-9_]/, '_')} = val"
+ end
+ rescue OptionParser::ParseError
+ end
+=end #'#"#`#
+ def getopts(*args)
+ options.getopts(*args)
+ end
+
=begin private
Initializes instance variable.
=end #'#"#`#