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: [], all_available: nil) reduce(args.dup - exclude) do |not_known, arg| arg.args_slice(*not_known) end.compact.tap do |list| list.each do |key| next unless block_given? yield(key, unknown_suggestions(key, all_available: all_available)) end end end def unknown_suggestions(str, all_available: nil) return [] if str.nil? str = str.to_s.strip return [] if str.empty? all_available ||= @known.keys spell_checker = DidYouMean::SpellChecker.new( dictionary: all_available ) spell_checker.correct(str) end def any_unknown?(exclude: []) unknown(exclude: exclude).length.positive? 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