Class: ParamsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/params_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParamsParser

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

#optionsObject (readonly)

Returns the value of attribute options.



48
49
50
# File 'lib/params_parser.rb', line 48

def options
  @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

#parseObject



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.banner = ""
    available_options = [ 
                         ['-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']
                        ]
    available_options.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


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