~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/getopts.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
#               getopts.rb - 
3
 
#                       $Release Version: $
4
 
#                       $Revision: 11708 $
5
 
#                       $Date: 2007-02-13 08:01:19 +0900 (火, 13  2月 2007) $
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
 
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: getopts is deprecated after Ruby 1.8.1; use optparse instead" if caller[0]
19
 
 
20
 
$RCS_ID=%q$Header$
21
 
 
22
 
# getopts is obsolete. Use GetoptLong.
23
 
 
24
 
def getopts(single_options, *options)
25
 
  boolopts = {}
26
 
  valopts = {}
27
 
 
28
 
  #
29
 
  # set defaults
30
 
  #
31
 
  single_options.scan(/.:?/) do |opt|
32
 
    if opt.size == 1
33
 
      boolopts[opt] = false
34
 
    else
35
 
      valopts[opt[0, 1]] = nil
36
 
    end
37
 
  end if single_options
38
 
 
39
 
  options.each do |arg|
40
 
    opt, val = arg.split(':', 2)
41
 
 
42
 
    if val
43
 
      valopts[opt] = val.empty? ? nil : val
44
 
    else
45
 
      boolopts[opt] = false
46
 
    end
47
 
  end
48
 
 
49
 
  #
50
 
  # scan
51
 
  #
52
 
  c = 0
53
 
  argv = ARGV
54
 
 
55
 
  while arg = argv.shift
56
 
    case arg
57
 
    when /\A--(.*)/
58
 
      if $1.empty?                      # xinit -- -bpp 24
59
 
        break
60
 
      end
61
 
 
62
 
      opt, val = $1.split('=', 2)
63
 
 
64
 
      if opt.size == 1
65
 
        argv.unshift arg
66
 
        return nil
67
 
      elsif valopts.key? opt            # imclean --src +trash
68
 
        valopts[opt] = val || argv.shift or return nil
69
 
      elsif boolopts.key? opt           # ruby --verbose
70
 
        boolopts[opt] = true
71
 
      else
72
 
        argv.unshift arg
73
 
        return nil
74
 
      end
75
 
 
76
 
      c += 1
77
 
    when /\A-(.+)/
78
 
      opts = $1
79
 
 
80
 
      until opts.empty?
81
 
        opt = opts.slice!(0, 1)
82
 
 
83
 
        if valopts.key? opt
84
 
          val = opts
85
 
 
86
 
          if val.empty?                 # ruby -e 'p $:'
87
 
            valopts[opt] = argv.shift or return nil
88
 
          else                          # cc -ohello ...
89
 
            valopts[opt] = val
90
 
          end
91
 
 
92
 
          c += 1
93
 
          break
94
 
        elsif boolopts.key? opt
95
 
          boolopts[opt] = true          # ruby -h
96
 
          c += 1
97
 
        else
98
 
          argv.unshift arg
99
 
          return nil
100
 
        end
101
 
      end
102
 
    else
103
 
      argv.unshift arg
104
 
      break
105
 
    end
106
 
  end
107
 
 
108
 
  #
109
 
  # set
110
 
  #
111
 
  $OPT = {}
112
 
 
113
 
  boolopts.each do |opt, val|
114
 
    $OPT[opt] = val
115
 
 
116
 
    sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
117
 
    eval "$OPT_#{sopt} = val"
118
 
  end
119
 
  valopts.each do |opt, val|
120
 
    $OPT[opt] = val
121
 
 
122
 
    sopt = opt.gsub(/[^A-Za-z0-9_]/, '_')
123
 
    eval "$OPT_#{sopt} = val"
124
 
  end
125
 
 
126
 
  c
127
 
end