Sha256: 5feb6ec740bfce9ea9ce068a31746bd4d6f76afb0bc85463a96c1e5e2d2914a3

Contents?: true

Size: 1.62 KB

Versions: 53

Compression:

Stored size: 1.62 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(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
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
eco-helpers-1.5.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.5.0 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.4.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.19 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.4.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.4.0 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.18 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.17 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.16 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.15 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.14 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.13 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.12 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.11 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.10 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.9 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.8 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.7 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.6 lib/eco/cli/scripting/arguments.rb
eco-helpers-1.3.5 lib/eco/cli/scripting/arguments.rb