From: TRANS Date: 2007-07-07T21:38:51+09:00 Subject: GetoptLong w/ DSL Hi-- Thought I'd share this little piece of code. It provides the option of using a little DSL for GetoptLong, rather than the argument constants. Eg. GetoptLong.new do flag :help, :h required :name end Instead of: GetoptLong.new([ [ :help, :h, GetoptLong::NO_ARGUMENT ], [ :name, GetoptLong::REQUIRED_ARGUMENT ] ]) Maybe, with a little touching-up, it be a good patch to the standard lib? T. 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