Sha256: 1daeafe96fa2ec832fffc9016288793a7d8ab512d4f58c8a7396ac8300c7b288

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 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

14 entries across 14 versions & 1 rubygems

Version Path
eco-helpers-2.7.16 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.15 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.14 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.13 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.12 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.4 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.7.0 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.6.4 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.6.3 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.6.2 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.6.1 lib/eco/cli/scripting/arguments.rb
eco-helpers-2.6.0 lib/eco/cli/scripting/arguments.rb