Created
February 14, 2014 04:08
-
-
Save jlucasps/8995618 to your computer and use it in GitHub Desktop.
Parser for parameters from terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
# Parser for parameters from terminal | |
# When you run a ruby script like $ ruby my_script.rb RAILS_ENV=production DATABASE=postgresql | |
# Usage in you my_script.rb file: | |
# >> p = ParseParams.new(ARGV, 'path/to/config_file.yml') | |
# >> p.params | |
class ParseParams | |
attr_accessor :params | |
attr_accessor :config_path | |
def initialize(params, config_path = nil) | |
@params = {"RAILS_ENV" => "development"} | |
@config_path = config_path || "#{File.dirname(__FILE__)}/config.yml" | |
if params.any? | |
@params = params.inject({}) do |memo, pair| | |
values = pair.split("=") | |
memo[values.first] = values.last unless values.nil? | |
memo | |
end | |
end | |
end | |
def read_configs | |
begin | |
YAML.load_file(@config_path)[@params["RAILS_ENV"]] | |
rescue StandardError => error | |
puts "Missing config file. Usage: ParseParams.new(ARGV, 'path/to/config_file.yml')" | |
puts error.message | |
raise Exception.new | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment