Sha256: e9874a89efc5617df7421fcdd2dec6f4151f7979b2c5b38f916c0cb2c4bc087f

Contents?: true

Size: 1.62 KB

Versions: 68

Compression:

Stored size: 1.62 KB

Contents

module Tins
  module GO
    module EnumerableExtension
      def push(argument)
        @arguments ||= []
        @arguments.push argument
        self
      end

      def each(&block)
        @arguments.each(&block)
        self
      end
      include Enumerable
    end

    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
              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
  end
end

require 'tins/alias'

Version data entries

68 entries across 66 versions & 7 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/go.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/go.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/go.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/go.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/go.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/go.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/go.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/go.rb
tins-1.9.0 lib/tins/go.rb
tins-1.8.2 lib/tins/go.rb
tins-1.8.1 lib/tins/go.rb
tins-1.8.0 lib/tins/go.rb
tins-1.7.0 lib/tins/go.rb
suzuko-0.1.8 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/go.rb
suzuko-0.1.7 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/go.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/go.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/go.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/go.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/go.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/tins-1.6.0/lib/tins/go.rb