module Eco class CLI class Scripting module ArgsHelpers def is_modifier?(value) value&.start_with?("-") end def arguments @arguments ||= Arguments.new end def stop_on_unknown!(exclude: []) if arguments.any_unkown?(exclude: exclude) raise "There are unknown options in your command line arguments: #{arguments.unknown(exclude: exclude)}" end 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) 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 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 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 end end end end