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



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

def arguments
  @arguments ||= Arguments.new
end

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



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

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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eco/cli/scripting/args_helpers.rb', line 40

def get_file(key, required: false, should_exist: true)
  filename = get_arg(key, with_param: true)
  if !filename
      if required
        puts "you need to specify a file '#{key} file'"
        exit(1)
      end
  elsif  !(File.exists?(filename) || File.exists?(File.expand_path(filename)))
    if should_exist && required
      puts "file doesn't exist #{filename}"
      exit
    end
  end

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

#is_modifier?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eco/cli/scripting/args_helpers.rb', line 14

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