Class: Eco::CLI::Scripting::Arguments

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eco/cli/scripting/arguments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ Arguments

Returns a new instance of Arguments.



9
10
11
12
# File 'lib/eco/cli/scripting/arguments.rb', line 9

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/eco/cli/scripting/arguments.rb', line 7

def args
  @args
end

Instance Method Details

#<<(arg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/eco/cli/scripting/arguments.rb', line 24

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

#add(key, with_param: false) ⇒ Object



20
21
22
# File 'lib/eco/cli/scripting/arguments.rb', line 20

def add(key, with_param: false)
  self << Argument.new(key, with_param: with_param)
end

#any_unknown?(exclude: []) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/eco/cli/scripting/arguments.rb', line 70

def any_unknown?(exclude: [])
  unknown(exclude: exclude).length.positive?
end

#each(&block) ⇒ Object



14
15
16
17
18
# File 'lib/eco/cli/scripting/arguments.rb', line 14

def each(&block)
  return to_enum(:each) unless block

  @known.values.each(&block)
end

#keysObject



41
42
43
# File 'lib/eco/cli/scripting/arguments.rb', line 41

def keys
  @known.keys
end

#known?(value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/eco/cli/scripting/arguments.rb', line 37

def known?(value)
  @known.key?(to_key(value))
end

#unknown(exclude: [], all_available: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/eco/cli/scripting/arguments.rb', line 45

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

#unknown_suggestions(str, all_available: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eco/cli/scripting/arguments.rb', line 57

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