Sha256: a3f0f37ebefa98b41262480885f6037c657e9d0fed1e83c1b375ee3288f3fb43

Contents?: true

Size: 1.1 KB

Versions: 15

Compression:

Stored size: 1.1 KB

Contents

module Tins
  module GO
    module_function

    # 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.
    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
              v[o] = args.shift or break 1
            else
              v[0] = p
            end
            break
          elsif b.key?(o)
            b[o] = true
          else
            args.unshift a
            break 1
          end
        end and break
      end
      b.merge(v)
    end
  end
end

require 'tins/alias'

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
tins-0.3.14 lib/tins/go.rb
tins-0.3.13 lib/tins/go.rb
tins-0.3.12 lib/tins/go.rb
tins-0.3.11 lib/tins/go.rb
tins-0.3.10 lib/tins/go.rb
tins-0.3.9 lib/tins/go.rb
tins-0.3.8 lib/tins/go.rb
tins-0.3.7 lib/tins/go.rb
tins-0.3.6 lib/tins/go.rb
tins-0.3.5 lib/tins/go.rb
tins-0.3.4 lib/tins/go.rb
tins-0.3.3 lib/tins/go.rb
tins-0.3.2 lib/tins/go.rb
tins-0.3.1 lib/tins/go.rb
tins-0.3.0 lib/tins/go.rb