Sha256: f71efdb405ecb6c933cead89cd198189cb71272cd3e2cd60e8f36cbbe357db2a

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Eco
  class Scripting
    class Arguments
      include Enumerable

      attr_reader :args

      def initialize(args = ARGV)
        @args  = args
        @known = {}
      end

      def each(params: {}, &block)
        return to_enum(:each) unless block
        @known.values.each(&block)
      end

      def add(key, with_param: false)
        self << Argument.new(key, with_param: with_param)
      end

      def <<(arg)
        raise "Expected Argument. Given #{arg.class}" unless arg.is_a?(Argument)
        if karg = @known[arg.key]
          #puts "Found already existent option #{arg.key} (with_param: arg.with_param?)"
          karg.with_param! if arg.with_param?
        else
          #puts "Adding unexistent option #{arg.key}"
          @known[arg.key] = arg
        end
        self
      end

      def known?(value)
        @known.key?(to_key(value))
      end

      def keys
        @known.keys
      end

      def unknown(exclude: [])
        reduce(args.dup - exclude) do |not_known, arg|
          arg.args_slice(*not_known)
        end
      end

      def any_unkown?(exclude: [])
        unknown(exclude: exclude).length > 0
      end

      private

      def to_key(value)
        case value
        when String
          value
        when Argument
          value.key
        else
          "Missuse: only able to transform to key a String or an Argument. Given #{value.class}"
        end
      end


    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eco-helpers-0.8.3 lib/eco/scripting/arguments.rb
eco-helpers-0.8.2 lib/eco/scripting/arguments.rb
eco-helpers-0.8.1 lib/eco/scripting/arguments.rb
eco-helpers-0.7.2 lib/eco/scripting/arguments.rb