Module: JRubySQL::OptionParser

Extended by:
Messages
Defined in:
lib/jrubysql/option_parser.rb

Class Method Summary collapse

Methods included from Messages

m

Class Method Details

.parse(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jrubysql/option_parser.rb', line 10

def self.parse argv
  {:output => 'cterm'}.tap do |options|
    opts = ::OptionParser.new { |opts|
      opts.banner = 
        [
          "usage: jrubysql [options]",
          "       jrubysql -t DBMS_TYPE -h HOSTNAME [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]",
          "       jrubysql -c CLASSNAME -j JDBC_URL [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]"
        ].join($/)
      opts.separator ''

      opts.on('-t', '--type DBMS_TYPE', 'Database type: mysql/oracle/postgres/sqlserver/sqlite/cassandra') do |v|
        options[:type] = v.downcase.to_sym
      end

      opts.on('-h', '--host HOST', 'DBMS host address') do |v|
        options[:host] = v
      end

      opts.separator ""
    
      opts.on('-c', '--class-name CLASSNAME', 'Class name of the JDBC driver') do |v|
        options[:driver] = v
      end

      opts.on('-j', '--jdbc-url JDBC_URL', 'JDBC URL for the connection') do |v|
        options[:url] = v
      end

      opts.separator ""
    
      opts.on('-u', '--user USERNAME', 'Username') do |v|
        options[:user] = v
      end
    
      opts.on('-p', '--password [PASSWORD]', 'Password') do |v|
        options[:password] = v
      end
    
      opts.on('-d', '--database DATABASE', 'Name of the database (optional)') do |v|
        options[:database] = v
      end

      opts.separator ""

      opts.on('-f', '--filename FILENAME', 'SQL script file') do |v|
        options[:filename] = v
      end

      opts.on('-e', '--execute SQLSCRIPT', 'SQL script') do |v|
        options[:script] = v
      end

      opts.on('-o', '--output OUTPUT_TYPE', 'Output type: cterm|term|csv (default: cterm)') do |v|
        options[:output] = v
      end
    
      opts.separator ""

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

      opts.on_tail('--version', "Show version") do
        puts JRubySQL.name
        exit
      end
    }
    begin
      opts.parse! argv
      if options.has_key?(:password) && options[:password].nil?
        options[:password] = ask_password
      end

      validate options
    rescue SystemExit
      exit 0
    rescue Exception => e
      puts e.to_s
      puts '=' * e.to_s.length
      puts opts
      exit 1
    end
  end#tap
end