Sha256: 96177ff3576afec449d4409e8d3d3922709c3f9143c7c22dbd0b9869ac4c7295

Contents?: true

Size: 1.61 KB

Versions: 106

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_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
end

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
eco-helpers-2.4.8 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.7 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.6 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.5 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.4 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.3 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.4.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.3.3 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.3.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.2.5 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.2.4 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.2.3 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.2.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.2.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.12 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.11 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.10 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.9 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.8 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.1.7 lib/eco/cli/scripting/arguments.rb