mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* lib/getopts.rb: Rewrite to fix some bugs and complete features.
- Accept options with the colon in the first argument;
    getopts("a:bcd:") is equivalent to getopts("bc", "a:", "d:").
  - Do not discard the argument that caused an error.
  - Do not discard '-', which commonly stands for stdin.
  - Allow specifying a long option with a value using '='.
    (command --long-option=value)
  - Stop reading options when it meets a non-option argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2163 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
			
			
This commit is contained in:
		
							parent
							
								
									ab1b4b5869
								
							
						
					
					
						commit
						f2dfae7ea0
					
				
					 2 changed files with 80 additions and 80 deletions
				
			
		
							
								
								
									
										11
									
								
								ChangeLog
									
										
									
									
									
								
							
							
						
						
									
										11
									
								
								ChangeLog
									
										
									
									
									
								
							|  | @ -1,3 +1,14 @@ | ||||||
|  | Tue Mar  5 05:56:29 2002  Akinori MUSHA  <knu@iDaemons.org> | ||||||
|  | 
 | ||||||
|  | 	* lib/getopts.rb: Rewrite to fix some bugs and complete features. | ||||||
|  | 	  - Accept options with the colon in the first argument; | ||||||
|  | 	    getopts("a:bcd:") is equivalent to getopts("bc", "a:", "d:"). | ||||||
|  | 	  - Do not discard the argument that caused an error. | ||||||
|  | 	  - Do not discard '-', which commonly stands for stdin. | ||||||
|  | 	  - Allow specifying a long option with a value using '='. | ||||||
|  | 	    (command --long-option=value) | ||||||
|  | 	  - Stop reading options when it meets a non-option argument. | ||||||
|  | 
 | ||||||
| Mon Mar  4 13:19:18 2002  Akinori MUSHA  <knu@iDaemons.org> | Mon Mar  4 13:19:18 2002  Akinori MUSHA  <knu@iDaemons.org> | ||||||
| 
 | 
 | ||||||
| 	* ext/extmk.rb.in (dir_config): Sync with mkmf.rb: Fix a bug where | 	* ext/extmk.rb.in (dir_config): Sync with mkmf.rb: Fix a bug where | ||||||
|  |  | ||||||
							
								
								
									
										149
									
								
								lib/getopts.rb
									
										
									
									
									
								
							
							
						
						
									
										149
									
								
								lib/getopts.rb
									
										
									
									
									
								
							|  | @ -11,117 +11,106 @@ | ||||||
| # 2000-03-21 | # 2000-03-21 | ||||||
| # modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp> | # modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp> | ||||||
| # | # | ||||||
|  | # 2002-03-05 | ||||||
|  | # rewritten by Akinori MUSHA <knu@ruby-lang.org> | ||||||
|  | # | ||||||
| 
 | 
 | ||||||
| $RCS_ID=%q$Header$ | $RCS_ID=%q$Header$ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def getopts( single_opts, *options ) | def getopts(single_options = '', *options) | ||||||
|   single_opts_exp = (single_opts && !single_opts.empty?) ? |  | ||||||
|                         /[#{single_opts}]/ : nil |  | ||||||
|   single_colon_exp = nil |  | ||||||
|   single_colon = nil |  | ||||||
|   opt = arg = val = nil |  | ||||||
|   boolopts = {} |   boolopts = {} | ||||||
|   valopts = {} |   valopts = {} | ||||||
|   argv = ARGV |  | ||||||
|   newargv = [] |  | ||||||
| 
 | 
 | ||||||
|   # |   # | ||||||
|   # set default |   # set defaults | ||||||
|   # |   # | ||||||
|   if single_opts then |   single_options.scan(/.:?/) do |opt| | ||||||
|     single_opts.each_byte do |byte| |     if opt.size == 1 | ||||||
|       boolopts[ byte.chr ] = false |       boolopts[opt] = false | ||||||
|   end |     else | ||||||
| end |       valopts[opt[0, 1]] = nil | ||||||
|   unless options.empty? then |  | ||||||
|     single_colon = '' |  | ||||||
| 
 |  | ||||||
|     options.each do |opt| |  | ||||||
|       m = /\A([^:]+):(.*)\z/.match( opt ) |  | ||||||
|       if m then |  | ||||||
|         valopts[ m[1] ] = m[2].empty? ? 0 : m[2] |  | ||||||
|       else |  | ||||||
|         boolopts[ opt ] = false |  | ||||||
| end |  | ||||||
|   end |  | ||||||
|     valopts.each do |opt, dflt| |  | ||||||
|       if opt.size == 1 then |  | ||||||
|         single_colon << opt |  | ||||||
| end |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|     if single_colon.empty? then |  | ||||||
|       single_colon = single_colon_exp = nil |  | ||||||
|       else |  | ||||||
|       single_colon_exp = /[#{single_colon}]/ |  | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|    | 
 | ||||||
|  |   options.each do |arg| | ||||||
|  |     opt, val = arg.split(':', 2) | ||||||
|  | 
 | ||||||
|  |     if val | ||||||
|  |       valopts[opt] = val.empty? ? nil : val | ||||||
|  |     else | ||||||
|  |       boolopts[opt] = false | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   # |   # | ||||||
|   # scan |   # scan | ||||||
|   # |   # | ||||||
|   c = 0 |   c = 0 | ||||||
|   arg = argv.shift |   argv = ARGV | ||||||
|   while arg do |  | ||||||
|     case arg |  | ||||||
|     when /\A--?\z/                      # xinit -- -bpp 24 |  | ||||||
|       newargv.concat argv |  | ||||||
|       break |  | ||||||
| 
 | 
 | ||||||
|  |   while arg = argv.shift | ||||||
|  |     case arg | ||||||
|     when /\A--(.*)/ |     when /\A--(.*)/ | ||||||
|       opt = $1 |       if $1.empty?			# xinit -- -bpp 24 | ||||||
|       if valopts.key? opt  then         # imclean --src +trash | 	break | ||||||
|         return nil if argv.empty? |       end | ||||||
|         valopts[ opt ] = argv.shift | 
 | ||||||
|       elsif boolopts.key? opt then      # ruby --verbose |       opt, val = $1.split('=', 2) | ||||||
|         boolopts[ opt ] = true | 
 | ||||||
|  |       if opt.size == 1 | ||||||
|  | 	argv.unshift arg | ||||||
|  | 	return nil | ||||||
|  |       elsif valopts.key? opt		# imclean --src +trash | ||||||
|  | 	valopts[opt] = val || argv.shift or return nil | ||||||
|  |       elsif boolopts.key? opt		# ruby --verbose | ||||||
|  | 	boolopts[opt] = true | ||||||
|       else |       else | ||||||
|  | 	argv.unshift arg | ||||||
| 	return nil | 	return nil | ||||||
|       end |       end | ||||||
|  | 
 | ||||||
|       c += 1 |       c += 1 | ||||||
| 
 |  | ||||||
|     when /\A-(.+)/ |     when /\A-(.+)/ | ||||||
|       arg = $1 |       opts = $1 | ||||||
|       0.upto( arg.size - 1 ) do |idx| |  | ||||||
|         opt = arg[idx, 1] |  | ||||||
|         if single_opts and single_opts_exp === opt then |  | ||||||
|           boolopts[ opt ] = true        # ruby -h |  | ||||||
|           c += 1 |  | ||||||
| 
 | 
 | ||||||
|         elsif single_colon and single_colon_exp === opt then |       until opts.empty? | ||||||
|           val = arg[ (idx+1)..-1 ] | 	opt = opts.slice!(0, 1) | ||||||
|           if val.empty? then            # ruby -e 'p $:' | 
 | ||||||
|             return nil if argv.empty? | 	if valopts.key? opt | ||||||
|             valopts[ opt ] = argv.shift | 	  val = opts | ||||||
|           else                          # cc -ohello ... | 
 | ||||||
|             valopts[ opt ] = val | 	  if val.empty?			# ruby -e 'p $:' | ||||||
|  | 	    valopts[opt] = argv.shift or return nil | ||||||
|  | 	  else				# cc -ohello ... | ||||||
|  | 	    valopts[opt] = val | ||||||
|  | 	  end | ||||||
|  | 
 | ||||||
|  | 	  c += 1 | ||||||
|  | 	  break | ||||||
|  | 	elsif boolopts.key? opt | ||||||
|  | 	  boolopts[opt] = true		# ruby -h | ||||||
|  | 	  c += 1 | ||||||
|  | 	else | ||||||
|  | 	  argv.unshift arg | ||||||
|  | 	  return nil | ||||||
|  | 	end | ||||||
|       end |       end | ||||||
|           c += 1 |     else | ||||||
| 
 |       argv.unshift arg | ||||||
|           break |       break | ||||||
|           else |  | ||||||
|           return nil |  | ||||||
|           end |  | ||||||
|         end |  | ||||||
| 
 |  | ||||||
|     else                                # ruby test.rb |  | ||||||
|       newargv.push arg |  | ||||||
|       end |  | ||||||
| 
 |  | ||||||
|     arg = argv.shift |  | ||||||
|     end |     end | ||||||
|      |   end | ||||||
|  | 
 | ||||||
|   # |   # | ||||||
|   # set |   # set | ||||||
|   # |   # | ||||||
|   boolopts.each do |opt, val| |   boolopts.each do |opt, val| | ||||||
|     eval "$OPT_#{opt} = val" |     eval "$OPT_#{opt} = val" | ||||||
|     end |  | ||||||
|   valopts.each do |opt, val| |  | ||||||
|     eval "$OPT_#{opt} = #{val == 0 ? 'nil' : 'val'}" |  | ||||||
|   end |   end | ||||||
|   argv.replace newargv |   valopts.each do |opt, val| | ||||||
|  |     eval "$OPT_#{opt} = val" | ||||||
|  |   end | ||||||
| 
 | 
 | ||||||
|   c |   c | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 knu
						knu