From 6bcd94726df72d36337a44969fc929e1b776a1fa Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 10 Jun 2006 23:31:32 +0000 Subject: * lib/optparse.rb (OptionParser#getopts): new methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10248 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 ++++ lib/optparse.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 7c79dc684d..4f3462e876 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Jun %-2d 08:30:33 2006 U-HUDIE\nobu,S-1-5-21-3746871489-166115513-3294629105-1005 + + * lib/optparse.rb (OptionParser#getopts): new methods. + Sat Jun 10 18:02:40 2006 Yukihiro Matsumoto * ext/bigdecimal/lib/bigdecimal/newton.rb (Newton::nlsolve): typo diff --git a/lib/optparse.rb b/lib/optparse.rb index b6ee6c896c..c00cda4506 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1379,6 +1379,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} @@ -1482,7 +1518,6 @@ class OptionParser env = ENV[env] || ENV[env.upcase] or return parse(*Shellwords.shellwords(env)) end - =begin = Acceptable argument classes @@ -1786,6 +1821,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 #'#"#`# -- cgit v1.2.3