diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-04-12 10:30:45 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-04-12 10:30:45 +0000 |
commit | bea190a1fce429ebf080d59faa637f796227dc50 (patch) | |
tree | 65cbfc7b8f9d24d6a5670496180705b1341c65bc | |
parent | c594a95a57345d2edb68b74546d5953d61d81972 (diff) | |
download | ruby-bea190a1fce429ebf080d59faa637f796227dc50.tar.gz |
* lib/optparse.rb (OptionParser#make_switch, OptionParser#order!):
added non-option and end-of-args handler. [ruby-talk:136878]
(EXPERIMENTAL
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8313 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | lib/optparse.rb | 57 |
2 files changed, 59 insertions, 12 deletions
@@ -1,21 +1,27 @@ +Tue Apr 12 19:30:36 2005 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * lib/optparse.rb (OptionParser#make_switch, OptionParser#order!): + added non-option and end-of-args handler. [ruby-talk:136878] + (EXPERIMENTAL + Tue Apr 12 15:33:09 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> - * ext/tk/tcltklib.c (ip_finalize): better modification than the + * ext/tk/tcltklib.c (ip_finalize): better modification than the previous commit [ruby-dev:26029]. Tue Apr 12 12:38:06 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> * ext/tk/tcltklib.c (ip_finalize): fix SEGV when Tcl_GlobalEval() - modifies the argument string to eval. + modifies the argument string to eval. Tue Apr 12 02:21:55 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> * ext/tk/tcltklib.c (ip_finalize): add existence check of - Tcl commands before calling Tcl_GlobalEval(). + Tcl commands before calling Tcl_GlobalEval(). Mon Apr 11 23:36:04 2005 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp> - * lib/drb/drb.r: [druby-ja:123] fix: When reference of my object is + * lib/drb/drb.r: [druby-ja:123] fix: When reference of my object is loaded, the object is tainted. * test/drb/test_drb.rb: ditto. diff --git a/lib/optparse.rb b/lib/optparse.rb index 79dde295cf..f17baaf103 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -416,6 +416,14 @@ class OptionParser self end + def add_banner(to) + if @short and @short.empty? and @long and @long.empty? + s = desc.join + to << " [" + s + "]..." unless s.empty? + end + to + end + # # Switch that takes no arguments. # @@ -635,6 +643,15 @@ class OptionParser end # + # OptionParser::List#each_option + # + # Iterates for each options. + # + def each_option(&block) + list.each(&block) + end + + # # OptionParser::List#summarize(*args) {...} # # Making summary table, yields the (({block})) with each lines. @@ -656,6 +673,15 @@ class OptionParser end end end + + def add_banner(to) + list.each do |opt| + if opt.respond_to?(:add_banner) + opt.add_banner(to) + end + end + to + end end # @@ -890,7 +916,11 @@ class OptionParser attr_accessor :summary_width, :summary_indent def banner - @banner ||= "Usage: #{program_name} [options]" + unless @banner + @banner = "Usage: #{program_name} [options]" + @stack.reverse_each {|el|el.add_banner(@banner)} + end + @banner end def program_name @@ -1178,13 +1208,17 @@ class OptionParser end default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern - s = if short.empty? and long.empty? - raise ArgumentError, "no switch given" if style or pattern or block - desc - else - (style || default_style).new(pattern || default_pattern, + if !(short.empty? and long.empty?) + s = (style || default_style).new(pattern || default_pattern, conv, sdesc, ldesc, arg, desc, block) - end + elsif !block + raise ArgumentError, "no switch given" if style or pattern + s = desc + else + short << pattern + s = (style || default_style).new(pattern, + conv, sdesc, ldesc, arg, desc, block) + end return s, short, long, (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style), nolong @@ -1312,13 +1346,20 @@ class OptionParser # non-option argument else - nonopt.call(arg) + catch(:prune) do + visit(:each_option) do |sw| + sw.block.call(arg) if sw.pattern and sw.pattern =~ arg + end + nonopt.call(arg) + end end end nil } + visit(:search, :short, nil) {|sw| sw.block.call(argv) if !sw.pattern} + argv end |