Class: ParamsParser
- Inherits:
-
Object
- Object
- ParamsParser
- Defined in:
- lib/params_parser.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ ParamsParser
constructor
A new instance of ParamsParser.
- #missing_key(keys) ⇒ Object
- #parse ⇒ Object
- #print_usage ⇒ Object
Constructor Details
#initialize ⇒ ParamsParser
Returns a new instance of ParamsParser.
3 4 5 6 |
# File 'lib/params_parser.rb', line 3 def initialize @options = Hashie::Mash.new parse end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
48 49 50 |
# File 'lib/params_parser.rb', line 48 def @options end |
Instance Method Details
#missing_key(keys) ⇒ Object
42 43 44 45 46 |
# File 'lib/params_parser.rb', line 42 def missing_key(keys) keys.find do |key| !@options.has_key?(key.to_s) end end |
#parse ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/params_parser.rb', line 8 def parse @opts = OptionParser.new do |opts| opts. = "" = [ ['-c', 'connection', 'use connection defined in ~/.mssql'], ['-h', 'host', 'server host'], ['-u', 'username', 'username'], ['-p', 'password', 'password'], ['-d', 'database', 'use database name'], ['-i', 'input_file', 'input file name'], ['-q', 'query', 'run query and exit'] ] .each do |o| opts.on(o[0], "--#{o[1]} #{o[1].upcase}", o[2]) do |value| @options[o[1]] = value end end opts.on_tail("-?", "--help", "show syntax summary") do print_usage exit end end @opts @opts.parse!(ARGV) @opts end |
#print_usage ⇒ Object
35 36 37 38 39 40 |
# File 'lib/params_parser.rb', line 35 def print_usage puts <<-END Usage: #{File.basename($0)} <options> #{@opts.to_s} END end |