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.



44
45
46
# File 'lib/eco/cli/scripting/args_helpers.rb', line 44

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

#arg_order?(key1, key2) ⇒ Boolean

Returns if key1 precedes key2 in the command line.

Returns:

  • (Boolean)

    if key1 precedes key2 in the command line.



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

def arg_order?(key1, key2)
  return false unless (k1 = get_arg_index(key1)) && k2 = get_arg_index(key2)
  k1 < k2
end

#argumentsArguments

Returns supported known arguments.

Returns:



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

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.



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

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.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/eco/cli/scripting/args_helpers.rb', line 61

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)
  value = true
  if with_param
    value  = argv[index + 1]
    #puts "modifier argument: #{value}"
    value  = nil if valid && is_modifier?(value)
  end
  return 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.



49
50
51
52
# File 'lib/eco/cli/scripting/args_helpers.rb', line 49

def get_arg_index(key)
  return nil if !arg?(key)
  argv.index(key)
end

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

Returns the filename.

Returns:

  • (String)

    the filename.



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

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

Returns:

  • (Boolean)


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

def is_modifier?(value)
  Argument.is_modifier?(value)
end

#known_argument(key, with_param: false) ⇒ Object

Registers an argument as a known one.



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

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

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

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eco/cli/scripting/args_helpers.rb', line 27

def stop_on_unknown!(exclude: [], only_options: false)
  # validate only those that are options
  unknown = arguments.unknown(exclude: exclude)
  if only_options
    unknown = unknown..select {|arg| is_modifier?(arg)}
  end

  unless unknown.empty?
    msg  = "There are unknown options in your command line arguments:\n"
    msg += "#{unknown}\n"
    msg += "Please, remember that use case specific options should come after the use case in the command line.\n"
    msg += "Use 'ruby main.rb -org [-usecase] --help -options' for more information"
    raise msg
  end
end