module Eco::CLI::Scripting::ArgsHelpers
Public Instance Methods
arg?(key)
click to toggle source
@return [Boolean] if `key` is in the command line.
# File lib/eco/cli/scripting/args_helpers.rb, line 44 def arg?(key) argv.include?(key) end
arg_order?(key1, key2)
click to toggle source
@return [Boolean] if `key1` precedes `key2` in the command line.
# 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
arguments()
click to toggle source
@return [Arguments] supported known arguments.
# File lib/eco/cli/scripting/args_helpers.rb, line 16 def arguments @arguments ||= Arguments.new(argv) end
argv()
click to toggle source
@return [Array<String] the command line arguments.
# File lib/eco/cli/scripting/args_helpers.rb, line 7 def argv @argv || ARGV end
get_arg(key, with_param: false, valid: true)
click to toggle source
@return [String, Boolean] the argument value if `with_param` or a `Boolean` if not.
# 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)
click to toggle source
@return [Integer, nil] the position of `key` in the command line.
# 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)
click to toggle source
@return [String] the filename.
# 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)
click to toggle source
# 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)
click to toggle source
Registers an argument as a known one.
# 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)
click to toggle source
Validation to stop the `script` if among `argv` there's any unknown argument.
# 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
Private Instance Methods
file_exists?(filename)
click to toggle source
# File lib/eco/cli/scripting/args_helpers.rb, line 91 def file_exists?(filename) File.exists?(filename) || File.exists?(File.expand_path(filename)) end