Sha256: 1b6de5d68c542071d255454d5033b4e27c6b28c2781a33a5b2ebd26139b68f97

Contents?: true

Size: 965 Bytes

Versions: 10

Compression:

Stored size: 965 Bytes

Contents

require 'getoptlong'

# = GetoptLong
#
# Ruby's standard GetoptLong class with an added DSL.
#
#   opts = GetoptLong.new do
#     reqs '--expect', '-x'
#     flag '--help', '-h'
#   end
#
# See GetoptLong::DSL for details.

class GetoptLong

  alias :init :initialize

  #
  def initialize(*arguments, &block)
    if block_given?
      raise ArgumentError unless arguments.empty?
      arguments = DSL.new(&block).arguments
    end
    init(*arguments)
  end

  # DSL-mode parser.
  class DSL
    attr :arguments

    def initialize(&block)
      @arguments = []
      instance_eval(&block)
    end

    #
    def flag(*opts)
      @arguments << (opts << GetoptLong::NO_ARGUMENT)
    end

    #
    def required(*opts)
      @arguments << (opts <<  GetoptLong::REQUIRED_ARGUMENT)
    end

    #
    alias :reqs :required

    #
    def optional(*opts)
      @arguments << (opts << GetoptLong::OPTIONAL_ARGUMENT)
    end

    #
    alias :opts :optional
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/standard/facets/getoptlong.rb
facets-3.1.0 lib/standard/facets/getoptlong.rb
facets-3.0.0 lib/standard/facets/getoptlong.rb
facets-2.9.3 lib/standard/facets/getoptlong.rb
facets-2.9.2 src/standard/facets/getoptlong.rb
facets-2.9.2 lib/standard/facets/getoptlong.rb
facets-2.9.1 lib/standard/facets/getoptlong.rb
facets-2.9.0 lib/more/facets/getoptlong.rb
facets-2.9.0.pre.2 lib/more/facets/getoptlong.rb
facets-2.9.0.pre.1 lib/more/facets/getoptlong.rb