From bea190a1fce429ebf080d59faa637f796227dc50 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 12 Apr 2005 10:30:45 +0000 Subject: [PATCH] * 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 --- ChangeLog | 14 ++++++++---- lib/optparse.rb | 57 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1a37606dd6..a12808bfaa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,21 +1,27 @@ +Tue Apr 12 19:30:36 2005 Nobuyoshi Nakada + + * 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 - * 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 * 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 * 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 - * 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. # @@ -634,6 +642,15 @@ class OptionParser __send__(id).complete(opt, icase, *pat, &block) end + # + # OptionParser::List#each_option + # + # Iterates for each options. + # + def each_option(&block) + list.each(&block) + end + # # OptionParser::List#summarize(*args) {...} # @@ -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