Sha256: 52bde17c4797df4441378a6444cc3a75ce16012df56c4c0f9c8a481a2c95ef92

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

module Kernel
  private
  #
  # CLI is based on Clap library
  # Copyright (c) 2010 Michel Martens
  #
  def cli(*args)
    opts = args.pop
    argv = (args.first || ARGV).dup
    args = []

    # Split option aliases.
    opts = opts.inject({}) do |h,(k,v)|
      k.to_s.split(/\s+/).each{|o| h[o]=v}; h
    end

    # Convert single dash flags into multiple flags.
    argv = argv.inject([]) do |a, v|
      if v[0,1] == '-' && v[1,1] != '-'
        a.concat(v[1..-1].chars.map{|c| "-#{c}"})
      else
        a << v
      end
      a
    end

    while argv.any?
      item = argv.shift
      flag = opts[item]

      if flag
        # Work around lambda semantics in 1.8.7.
        arity = [flag.arity, 0].max

        # Raise if there are not enough parameters
        # available for the flag.
        if argv.size < arity
          raise ArgumentError
        end

        # Call the lambda with N items from argv,
        # where N is the lambda's arity.
        flag.call(*argv.shift(arity))
      else

        # Collect the items that don't correspond to
        # flags.
        args << item
      end
    end

    args
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
facets-glimmer-3.2.0 lib/standard/facets/cli.rb
facets-3.1.0 lib/standard/facets/cli.rb
facets-3.0.0 lib/standard/facets/cli.rb
indexer-0.3.1 lib/indexer/core_ext/kernel/cli.rb
indexer-0.3.0 lib/indexer/core_ext/kernel/cli.rb
indexer-0.2.0 lib/indexer/core_ext/kernel/cli.rb