Module: Eco::CLI::Scripting::ArgsHelpers

Included in:
Eco::CLI::Scripting
Defined in:
lib/eco/cli/scripting/args_helpers.rb

Instance Method Summary collapse

Instance Method Details

#arg?(key) ⇒ Boolean

Returns if key is in the command line.

Returns:

  • (Boolean)

    if key is in the command line.



55
56
57
# File 'lib/eco/cli/scripting/args_helpers.rb', line 55

def arg?(key)
  argv.include?(key)
end

#arg_order?(key_1, key_2) ⇒ Boolean

Returns if key1 precedes key2 in the command line.

Returns:

  • (Boolean)

    if key1 precedes key2 in the command line.



67
68
69
70
71
72
73
# File 'lib/eco/cli/scripting/args_helpers.rb', line 67

def arg_order?(key_1, key_2)
  k_1 = get_arg_index(key_1)
  k_2 = get_arg_index(key_2)
  return false unless k_1 && k_2

  k_1 < k_2
end

#argumentsArguments

Returns supported known arguments.

Returns:



15
16
17
# File 'lib/eco/cli/scripting/args_helpers.rb', line 15

def arguments
  @arguments ||= Arguments.new(argv)
end

#argvArray<String] the command line arguments.

Returns Array<String] the command line arguments.

Returns:

  • (Array<String] the command line arguments.)

    Array<String] the command line arguments.



6
7
8
# File 'lib/eco/cli/scripting/args_helpers.rb', line 6

def argv
  @argv || ARGV
end

#get_arg(key, with_param: false, valid: true) ⇒ String, Boolean

Returns the argument value if with_param or a Boolean if not.

Returns:

  • (String, Boolean)

    the argument value if with_param or a Boolean if not.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/eco/cli/scripting/args_helpers.rb', line 76

def get_arg(key, with_param: false, valid: true)
  # track what a known option looks like
  known_argument(key, with_param: with_param)
  return nil  unless (index = get_arg_index(key))
  return true unless with_param

  value  = argv[index + 1]
  #puts "modifier argument: #{value}"
  value  = nil if valid && is_modifier?(value)
  value
end

#get_arg_index(key) ⇒ Integer?

Returns the position of key in the command line.

Returns:

  • (Integer, nil)

    the position of key in the command line.



60
61
62
63
64
# File 'lib/eco/cli/scripting/args_helpers.rb', line 60

def get_arg_index(key)
  return unless arg?(key)

  argv.index(key)
end

#get_file(key, required: false, should_exist: true) ⇒ String

Returns the filename.

Returns:

  • (String)

    the filename.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eco/cli/scripting/args_helpers.rb', line 89

def get_file(key, required: false, should_exist: true)
  filename = get_arg(key, with_param: true)
  if !filename && required
    puts "You need to specify a file or folder '#{key} file_or_folder'"
    exit(1)
  elsif !file_exists?(filename) && should_exist && required
    puts "This file/folder doesn't exist '#{filename}'"
    exit(1)
  end

  filename = File.expand_path(filename) if filename && should_exist
  filename
end

#is_modifier?(value) ⇒ Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


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

def is_modifier?(value) # rubocop:disable Naming/PredicateName
  Argument.is_modifier?(value)
end

#known_argument(key, with_param: false) ⇒ Object

Registers an argument as a known one.



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

def known_argument(key, with_param: false)
  arguments.add(key, with_param: with_param)
end

#stop_on_unknown!(exclude: [], only_options: false, all_available: nil) ⇒ Object

Validation to stop the script if among argv there's any unknown argument.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/eco/cli/scripting/args_helpers.rb', line 25

def stop_on_unknown!(exclude: [], only_options: false, all_available: nil)
  # validate only those that are options
  suggestions = {}
  args        = {
    exclude:       exclude,
    all_available: all_available
  }
  unknown = arguments.unknown(**args) do |key, correct|
    suggestions[key] = correct unless correct.empty?
  end
  unknown = unknown.select {|arg| is_modifier?(arg)} if only_options

  return if unknown.empty?

  suggestions_str = suggestions.slice(*unknown).map do |key, correct|
    str      = "Unknown option '#{key}'."
    str_corr = correct.map {|key| "'#{key}'"}
    str     << " Did you mean: #{str_corr.join(', ')}" unless correct.empty?
    str
  end.join("\n  * ")

  msg  = "\nThere are UNKNOWN OPTIONS in your command line arguments !!"
  msg << "\n  * #{suggestions_str}\n"
  msg << "\nUse 'ruby main.rb -org [-usecase] --help -options' for more information"
  msg << "\n  - Please, remember that use case specific options "
  msg << "should come after the use case in the command line.\n"
  raise msg
end