Module: Tins::GO

Defined in:
lib/tins/go.rb

Defined Under Namespace

Modules: EnumerableExtension

Class Method Summary collapse

Class Method Details

.go(s, args = ARGV) ⇒ Object

Parses the argument array args, according to the pattern s, to retrieve the single character command line options from it. If s is ‘xy:’ an option ‘-x’ without an option argument is searched, and an option ‘-y foo’ with an option argument (‘foo’).

An option hash is returned with all found options set to true or the found option argument.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tins/go.rb', line 26

def go(s, args = ARGV)
  b, v = s.scan(/(.)(:?)/).inject([ {}, {} ]) { |t, (o, a)|
    a = a == ':'
    t[a ? 1 : 0][o] = a ? nil : false
    t
  }
  while a = args.shift
    a !~ /\A-(.+)/ and args.unshift a and break
    p = $1
    until p == ''
      o = p.slice!(0, 1)
      if v.key?(o)
        if p == '' then
          a = args.shift or break 1
        else
          a = p
        end
        if v[o].nil?
          a = a.dup
          a.extend EnumerableExtension
          a.push a
          v[o] = a
        else
          v[o].push a
        end
        break
      elsif b.key?(o)
        if b[o] == false
          b[o]= 1
        else
          b[o] += 1
        end
      else
        args.unshift a
        break 1
      end
    end and break
  end
  b.merge(v)
end