lib/eco/cli/scripting/args_helpers.rb in eco-helpers-1.5.1 vs lib/eco/cli/scripting/args_helpers.rb in eco-helpers-1.5.2
- old
+ new
@@ -1,16 +1,20 @@
module Eco
class CLI
class Scripting
module ArgsHelpers
+ def argv
+ @argv || ARGV
+ end
+
def is_modifier?(value)
Argument.is_modifier?(value)
end
def arguments
- @arguments ||= Arguments.new
+ @arguments ||= Arguments.new(argv)
end
def stop_on_unknown!(exclude: [], only_options: false)
# validate only those that are options
unknown = arguments.unknown(exclude: exclude)
@@ -21,39 +25,45 @@
unless unknown.empty?
raise "There are unknown options in your command line arguments: #{unknown}"
end
end
+ def get_arg_index(key)
+ return nil if !argv.include?(key)
+ argv.index(key)
+ end
+
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)
+ return nil unless index = get_arg_index(key)
value = true
if with_param
- next_i = ARGV.index(key) + 1
- value = ARGV[next_i]
+ value = argv[index + 1]
#puts "modifier argument: #{value}"
- value = nil if valid && is_modifier?(value)
+ value = nil if valid && is_modifier?(value)
end
return value
end
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
+ 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
+
+ private
+
+ def file_exists?(filename)
+ File.exists?(filename) || File.exists?(File.expand_path(filename))
end
end
end
end