Skip to content

Instantly share code, notes, and snippets.

@jlucasps
Created February 14, 2014 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlucasps/8995618 to your computer and use it in GitHub Desktop.
Save jlucasps/8995618 to your computer and use it in GitHub Desktop.
Parser for parameters from terminal
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