Class: Itds::ReplRunner

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

Class Method Summary collapse

Class Method Details

.run(args = []) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/itds/repl.rb', line 102

def self.run(args=[])
  options = {}
  mandatory = [:host]

  opts_parser = OptionParser.new do |opts|
    opts.banner = "Usage: itds [options] [sql]"

    opts.on("-h", "--host HOST", "host or ip") do |h|
      options[:host] = h
    end

    opts.on("-P", "--port [PORT]", Integer, "port number, default is 1433") do |p|
      options[:port] = p
    end

    opts.on("-u", "--user [USER]", "username") do |u|
      options[:username] = u
    end

    opts.on("-d", "--database [DATABASE]", "database name") do |d|
      options[:database] = d
    end

    opts.on("-p", "--password [PASSWORD]", "optional password") do |p|
      options[:password] = p
    end

    opts.on("--timeout [TIMEOUT]", "connect and query timeout, 5 secs by default") do |t|
      options[:timeout] = t
    end

    opts.on_tail("--help", "show this help message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "show version") do
      puts Itds::VERSION
      exit
    end
  end

  if args.empty?
    puts opts_parser.help
    exit
  end

  opts_parser.parse!(args)
  missing = mandatory.select {|p| options[p].nil? }
  unless missing.empty?
    puts "Missing mandatory options: #{missing.join(', ')}"
    exit
  end

  Repl.config(options)
  if args.empty? #interactive
    trap("INT") { Repl.singal }
    Repl.run
  else
    Repl.execute_cmd(args.join(" "))
  end
end