Sha256: 55c45265b91e1db9b2cb0ff9f03e9f169c427e23d2cbf46faddaf2a7baaf35d3

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

module GLI
  # Factory for creating an OptionParser based on app configuration and DSL calls
  class OptionParserFactory
    # Create an OptionParserFactory for the given
    # flags, switches, and accepts
    def initialize(flags,switches,accepts)
      @flags = flags
      @switches = switches
      @accepts = accepts
    end

    # Return an option parser to parse the given flags, switches and accepts
    def option_parser
      options = {}
      option_parser = OptionParser.new do |opts|
        self.class.setup_accepts(opts,@accepts)
        self.class.setup_options(opts,@switches,options)
        self.class.setup_options(opts,@flags,options)
      end
      [option_parser,options]
    end

  private

    def self.setup_accepts(opts,accepts)
      accepts.each do |object,block| 
        opts.accept(object) do |arg_as_string| 
          block.call(arg_as_string) 
        end
      end
    end

    def self.setup_options(opts,tokens,options)
      tokens.each do |ignore,token|
        opts.on(*token.arguments_for_option_parser) do |arg|
          [token.name,token.aliases].flatten.compact.each do |name|
            options[name] = arg
          end
        end
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gli-2.1.0 lib/gli/option_parser_factory.rb
gli-2.0.0 lib/gli/option_parser_factory.rb
gli-2.0.0.rc8 lib/gli/option_parser_factory.rb
gli-2.0.0.rc7 lib/gli/option_parser_factory.rb
gli-2.0.0.rc6 lib/gli/option_parser_factory.rb
gli-2.0.0.rc5 lib/gli/option_parser_factory.rb
gli-2.0.0.rc4 lib/gli/option_parser_factory.rb
gli-2.0.0.rc3 lib/gli/option_parser_factory.rb