Sha256: ebcb42fb8fe70dfffe4d9e32641aad190e7c86b39249fc701b3c60d38c10f87b

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'raph/parser/base_parser'
require 'raph/parser/flag_parser'
require 'raph/parser/assignment_parser'

module Raph
  module Parser
    # Grouped arguments are arguments that follows
    # a group - a specific argument. In other words
    # flags and assignments represents groups and
    # arguments followed them are grouped arguments.
    #
    # Next example:
    #   '--group1', '1', '2', '--group2', 'three'
    # has two groups (`:group1` and `:group2`) with
    # corresponding grouped arguments (['1', '2'], ['three'])
    class GroupedArgParser < BaseParser
      def initialize
        @flag_parser = FlagParser.new
        @assignment_parser = AssignmentParser.new
      end

      def parse(args)
        groups = {}
        current_group = nil

        args.each do |arg|
          if group? arg
            current_group = to_underscored_sym arg
            groups[current_group] = []
          else
            groups[current_group].push(arg) if current_group
          end
        end
        groups
      end

      def group?(arg)
        @flag_parser.flag?(arg) || @assignment_parser.assignment?(arg)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raph-0.0.3 lib/raph/parser/grouped_arg_parser.rb