Class: Trinidad::CommandLineParser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLineParser

Returns a new instance of CommandLineParser.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/trinidad/command_line_parser.rb', line 12

def initialize
  @default_options = {
    :port => 3000,
    :environment => 'development',
    :context_path => '/',
    :libs_dir => 'lib',
    :classes_dir => 'classes',
    :ssl_port => 8443,
    :ajp_port => 8009
  }
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



6
7
8
# File 'lib/trinidad/command_line_parser.rb', line 6

def default_options
  @default_options
end

Class Method Details

.parse(argv) ⇒ Object



8
9
10
# File 'lib/trinidad/command_line_parser.rb', line 8

def self.parse(argv)
  CommandLineParser.new.parse!(argv)
end

Instance Method Details

#options_parserObject



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/trinidad/command_line_parser.rb', line 40

def options_parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = 'Trinidad server default options:'
    opts.separator ''

    opts.on('-e', '--env ENVIRONMENT', 'Rails environment', 
        "default: #{default_options[:environment]}") do |v| 
      default_options[:environment] = v
    end

    opts.on('-p', '--port PORT', 'Port to bind to', 
        "default: #{default_options[:port]}") do |v| 
      default_options[:port] = v
    end

    opts.on('-c', '--context CONTEXT_PATH', 'The application context path', 
        "default: #{default_options[:context_path]}") do |v| 
      default_options[:context_path] = v
    end

    opts.on('--lib', '--jars LIBS_DIR', 'Directory containing jars used by the application', 
        "default: #{default_options[:libs_dir]}") do |v| 
      default_options[:libs_dir] = v
    end

    opts.on('--classes', '--classes CLASSES_DIR', 'Directory containing classes used by the application', 
        "default: #{default_options[:classes_dir]}") do |v| 
      default_options[:classes_dir] = v
    end

    opts.on('-s', '--ssl [SSL_PORT]', 'Enable secure socket layout',
        "default port: #{default_options[:ssl_port]}") do |v|
      ssl_port = v.nil? ? default_options.delete(:ssl_port) : v.to_i
      default_options[:ssl] = {:port => ssl_port}
    end

    opts.on('-a', '--ajp [AJP_PORT]', 'Enable ajp connections',
        "default port: #{default_options[:ajp_port]}") do |v|
      ajp_port = v.nil? ? default_options.delete(:ajp_port) : v.to_i
      default_options[:ajp] = {:port => ajp_port} 
    end

    opts.on('-f', '--config [CONFIG_FILE]', 'Configuration file',
        "default: #{default_options[:config]}") do |file|
      default_options[:config] = 'config/trinidad.yml'

      if file
        default_options[:config] = file
      elsif File.exist?('config/tomcat.yml') && !File.exist?(default_options[:config])
        puts "[WARNING] Default configuration file name has been moved to trinidad.yml, tomcat.yml will not be supported in future versions."
        puts "\tYou still can use tomcat.yml passing it as the file name to this option: -f config/tomcat.yml"
        default_options[:config] = 'config/tomcat.yml'
      end
    end

    opts.on('-r', '--rackup [RACKUP_FILE]', 'Rackup configuration file',
        'default: config.ru') do |v|
      default_options[:rackup] = v || 'config.ru'
    end

    opts.on('--public', '--public DIRECTORY', 'Public directory', 'default: public') do |v|
      default_options[:public] = v
    end

    opts.on('-t', '--threadsafe', 'Threadsafe mode') do
      default_options[:jruby_min_runtimes] = 1
      default_options[:jruby_max_runtimes] = 1
    end

    opts.on('-v', '--version', 'display the current version') do
      puts File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')).chomp
      exit
    end

    opts.on('-l', '--load EXTENSION_NAME', 'load options for a given extension') do |name|
      Trinidad::Extensions.configure_options_extensions({name => {}}, opts, default_options)
    end

    opts.on('-h', '--help', 'display the help') do
      puts opts
      exit
    end
  end
end

#parse!(argv) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/trinidad/command_line_parser.rb', line 24

def parse!(argv)
  begin
    options_parser.parse!(argv)
  rescue OptionParser::InvalidOption => e
    p e, options_parser
    exit(1)
  end

  if default_options.has_key?(:config)
    config_options = YAML.load_file(default_options[:config])
    default_options.deep_merge!(config_options.symbolize!)
  end

  default_options
end