lib/getopts.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #               getopts.rb - 
   3  #                       $Release Version: $
   4  #                       $Revision: 1.7 $
   5  #                       $Date: 2002/03/13 09:44:58 $
   6  #                       by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
   7  #
   8  # --
   9  # this is obsolete; use getoptlong
  10  #
  11  # 2000-03-21
  12  # modified by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
  13  #
  14  # 2002-03-05
  15  # rewritten by Akinori MUSHA <knu@ruby-lang.org>
  16  #
  17  
  18  $RCS_ID=%q$Header: /src/ruby/lib/getopts.rb,v 1.7 2002/03/13 09:44:58 knu Exp $
  19  
  20  
  21  def getopts(single_options, *options)
  22    boolopts = {}
  23    valopts = {}
  24  
  25    #
  26    # set defaults
  27    #
  28    single_options.scan(/.:?/) do |opt|
  29      if opt.size == 1
  30        boolopts[opt] = false
  31      else
  32        valopts[opt[0, 1]] = nil
  33      end
  34    end if single_options
  35  
  36    options.each do |arg|
  37      opt, val = arg.split(':', 2)
  38  
  39      if val
  40        valopts[opt] = val.empty? ? nil : val
  41      else
  42        boolopts[opt] = false
  43      end
  44    end
  45  
  46    #
  47    # scan
  48    #
  49    c = 0
  50    argv = ARGV
  51  
  52    while arg = argv.shift
  53      case arg
  54      when /\A--(.*)/
  55        if $1.empty?                      # xinit -- -bpp 24
  56          break
  57        end
  58  
  59        opt, val = $1.split('=', 2)
  60  
  61        if opt.size == 1
  62          argv.unshift arg
  63          return nil
  64        elsif valopts.key? opt            # imclean --src +trash
  65          valopts[opt] = val || argv.shift or return nil
  66        elsif boolopts.key? opt           # ruby --verbose
  67          boolopts[opt] = true
  68        else
  69          argv.unshift arg
  70          return nil
  71        end
  72  
  73        c += 1
  74      when /\A-(.+)/
  75        opts = $1
  76  
  77        until opts.empty?
  78          opt = opts.slice!(0, 1)
  79  
  80          if valopts.key? opt
  81            val = opts
  82  
  83            if val.empty?                 # ruby -e 'p $:'
  84              valopts[opt] = argv.shift or return nil
  85            else                          # cc -ohello ...
  86              valopts[opt] = val
  87            end
  88  
  89            c += 1
  90            break
  91          elsif boolopts.key? opt
  92            boolopts[opt] = true          # ruby -h
  93            c += 1
  94          else
  95            argv.unshift arg
  96            return nil
  97          end
  98        end
  99      else
 100        argv.unshift arg
 101        break
 102      end
 103    end
 104  
 105    #
 106    # set
 107    #
 108    boolopts.each do |opt, val|
 109      eval "$OPT_#{opt} = val"
 110    end
 111    valopts.each do |opt, val|
 112      eval "$OPT_#{opt} = val"
 113    end
 114  
 115    c
 116  end