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

#argumentsObject



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

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

#argvObject



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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eco/cli/scripting/args_helpers.rb', line 35

def get_arg(key, with_param: false, valid: true)
  # track what a known option looks like
  arguments.add(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) ⇒ Object



30
31
32
33
# File 'lib/eco/cli/scripting/args_helpers.rb', line 30

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/eco/cli/scripting/args_helpers.rb', line 48

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 '#{key} file'"
    exit(1)
  elsif !file_exists?(filename) && should_exist && required
    puts "This file doesn't exist '#{filename}'"
    exit(1)
  end

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

#is_modifier?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/eco/cli/scripting/args_helpers.rb', line 18

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?
    raise "There are unknown options in your command line arguments: #{unknown}"
  end
end