Class: Trinidad::CommandLineParser
- Inherits:
-
Object
- Object
- Trinidad::CommandLineParser
- Defined in:
- lib/trinidad/command_line_parser.rb
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
'config/trinidad.{yml,rb}'
Instance Attribute Summary collapse
- #config_file(base_dir = nil) ⇒ Object
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ CommandLineParser
constructor
A new instance of CommandLineParser.
-
#load!(options) ⇒ Object
(also: #load_configuration)
Load the configuration from the given options and return it.
- #options_parser ⇒ Object
-
#parse!(argv) ⇒ Object
Parse the arguments and return the loaded Trinidad configuration.
Constructor Details
#initialize ⇒ CommandLineParser
Returns a new instance of CommandLineParser.
14 15 16 |
# File 'lib/trinidad/command_line_parser.rb', line 14 def initialize @default_options = {} end |
Instance Attribute Details
#config_file(base_dir = nil) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/trinidad/command_line_parser.rb', line 50 def config_file(base_dir = nil) base_dir ||= Dir.pwd if @config_file.nil? # false means do not use no config file Dir.glob(File.join(base_dir, DEFAULT_CONFIG_FILE)).first else @config_file && File.(@config_file, base_dir) end end |
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
12 13 14 |
# File 'lib/trinidad/command_line_parser.rb', line 12 def @default_options end |
Class Method Details
.load(options = {}) ⇒ Object
8 9 10 |
# File 'lib/trinidad/command_line_parser.rb', line 8 def self.load( = {}) CommandLineParser.new.load!() end |
.parse(argv) ⇒ Object
4 5 6 |
# File 'lib/trinidad/command_line_parser.rb', line 4 def self.parse(argv) CommandLineParser.new.parse!(argv) end |
Instance Method Details
#load!(options) ⇒ Object Also known as: load_configuration
Load the configuration from the given options and return it.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/trinidad/command_line_parser.rb', line 31 def load!() config = config_file([:root_dir] || [:web_app_dir]) if config && File.exist?(config) if yaml = (File.extname(config) == '.yml') require 'yaml'; require 'erb' = YAML.load(ERB.new(File.read(config)).result(binding)) end # NOTE: provided options should override configuration values : Trinidad.configure(, ) do load config unless yaml # if not .yml assume it's ruby (.rb) end else Trinidad.configure() end end |
#options_parser ⇒ Object
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 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 164 165 166 167 168 169 170 171 |
# File 'lib/trinidad/command_line_parser.rb', line 61 def require 'optparse' @parser ||= OptionParser.new do |opts| opts. = 'Usage: trinidad [server options]' opts.separator '' opts.on('-d', '--dir ROOT_DIR', 'web application root directory', "default: current working directory") do |dir| [:root_dir] = dir end opts.on('-e', '--env ENVIRONMENT', 'rack (rails) environment', "default: #{default(:environment)}") do |env| [:environment] = env end opts.on('-r', '--rackup [RACKUP_FILE]', 'rackup configuration file', "default: config.ru") do |rackup| [:rackup] = rackup || 'config.ru' end opts.on('--public', '--public PUBLIC_DIR', 'web application public root', "default: #{default(:public)}") do |public| [:public] = public end opts.on('-c', '--context CONTEXT_PATH', 'application context path', "default: #{default(:context_path)}") do |path| [:context_path] = path end opts.on('--monitor', '--monitor MONITOR_FILE', 'monitor for application re-deploys', "default: tmp/restart.txt") do |monitor| [:monitor] = monitor end opts.on('-t', '--threadsafe', 'force thread-safe mode (use single runtime)') do [:jruby_min_runtimes] = 1 [:jruby_max_runtimes] = 1 end opts.on('--runtimes MIN:MAX', 'use given number of min/max jruby runtimes', "default: #{default(:jruby_min_runtimes)}:#{default(:jruby_max_runtimes)}") do |min_max| min, max = min_max.split(':') [:jruby_min_runtimes] = min.to_i if min [:jruby_max_runtimes] = max.to_i if max end opts.on('-f', '--config [CONFIG_FILE]', 'configuration file', "default: #{DEFAULT_CONFIG_FILE}") do |file| self.config_file = file end opts.on('--address', '--address ADDRESS', 'host address', "default: #{default(:address)}") do |address| [:address] = address end opts.on('-p', '--port PORT', 'port to bind to', "default: #{default(:port)}") do |port| [:port] = port end opts.on('-s', '--ssl [SSL_PORT]', 'enable secure socket layout', "default port: 3443") do |port| [:ssl] = { :port => (port || 3443).to_i } end opts.on('-a', '--ajp [AJP_PORT]', 'enable the AJP web protocol', "default port: 8009") do |port| [:ajp] = { :port => (port || 8009).to_i } end opts.on('--java_lib LIB_DIR', '--lib LIB_DIR (deprecated use --java_lib)', 'contains .jar files used by the app', "default: #{default(:java_lib)}") do |lib| [:java_lib] = lib end opts.on('--java_classes CLASSES_DIR', '--classes CLASSES_DIR (deprecated use --java_classes)', 'contains java classes used by the app', "default: #{default_java_classes}") do |classes| [:java_classes] = classes end opts.on('-l', '--load EXTENSION_NAMES', Array, 'load options for extensions') do |ext_names| ext_names.each do |ext| Trinidad::Extensions.({ ext => {} }, opts, ) end end opts.on('--apps_base APPS_BASE_DIR', '--apps APPS_BASE_DIR (deprecated use --apps_base)', 'set applications base directory') do |apps_base| [:apps_base] = apps_base end opts.on('-g', '--log LEVEL', 'set logging level') do |log| [:log] = log end opts.on('-v', '--version', 'show server version') do puts "Trinidad #{Trinidad::VERSION} (Tomcat #{Trinidad::TOMCAT_VERSION})" exit end opts.on('-h', '--help', 'display this help') do puts opts exit end end end |
#parse!(argv) ⇒ Object
Parse the arguments and return the loaded Trinidad configuration.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/trinidad/command_line_parser.rb', line 19 def parse!(argv) begin .parse!(argv) rescue OptionParser::InvalidOption => e p e, exit(1) end load!() end |