Sha256: a4e99dc50cdd91dfbab5e4cdde8e8abf0618e666a3d63af71705f98421c5b3ba

Contents?: true

Size: 1.32 KB

Versions: 17

Compression:

Stored size: 1.32 KB

Contents

# = GetoptLong
#
# Ruby's standard GetoptLong class with an added DSL.
#
#   opts = GetoptLong.new do
#     reqs '--expect', '-x'
#     flag '--help', '-h'
#   end
#
#   opts.each { |opt, arg|
#     ...
#   }
#
# == Authors
#
# * Thomas Sawuer
#
# == Copying
#
# Copyright (c) 2007 Thomas Sawyer
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.

require 'getoptlong'

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

17 entries across 17 versions & 2 rubygems

Version Path
facets-2.8.4 lib/more/facets/getoptlong.rb
facets-2.8.3 lib/more/facets/getoptlong.rb
facets-2.8.2 lib/more/facets/getoptlong.rb
facets-2.8.1 lib/more/facets/getoptlong.rb
facets-2.8.0 lib/more/facets/getoptlong.rb
facets-2.7.0 lib/more/facets/getoptlong.rb
facets-2.6.0 lib/lore/facets/getoptlong.rb
facets-2.4.0 lib/facets/getoptlong.rb
facets-2.4.1 lib/facets/getoptlong.rb
facets-2.4.3 lib/lore/facets/getoptlong.rb
facets-2.4.4 lib/lore/facets/getoptlong.rb
facets-2.4.2 lib/lore/facets/getoptlong.rb
facets-2.5.0 lib/lore/facets/getoptlong.rb
facets-2.4.5 lib/lore/facets/getoptlong.rb
facets-2.5.1 lib/lore/facets/getoptlong.rb
facets-2.5.2 lib/lore/facets/getoptlong.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/lore/facets/getoptlong.rb