Sha256: 0cc1652e3db11c0db6ea134f4039b20e4d3581ef23e6b3533adfc70927e3dd35

Contents?: true

Size: 1.61 KB

Versions: 11

Compression:

Stored size: 1.61 KB

Contents

module Eco
  class CLI
    class Scripting
      class Arguments
        include Enumerable

        attr_reader :args

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

        def each(&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_unknown?(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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
eco-helpers-2.5.10 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.9 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.8 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.7 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.6 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.5 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.4 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.3 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.5.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.9 lib/eco/cli/scripting/arguments.rb