Module: Lich::Main::ArgNormalization
- Defined in:
- documented/main/arg_normalization.rb
Constant Summary collapse
- HEADLESS_PATTERN =
Regular expression pattern for matching the --headless argument.
/^--headless(?:=(.+))?$/i.freeze
- DETACHABLE_CLIENT_PREFIX =
'--detachable-client='.freeze
Class Method Summary collapse
-
.normalize!(argv) ⇒ Array<String>
Normalizes command-line arguments for headless mode.
-
.normalize_headless_port(token) ⇒ Integer
Normalizes the headless port token.
Class Method Details
.normalize!(argv) ⇒ Array<String>
Normalizes command-line arguments for headless mode. This method modifies the argv array in place to replace the --headless argument with --without-frontend and appends the detachable client prefix with the specified port.
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 |
# File 'documented/main/arg_normalization.rb', line 25 def self.normalize!(argv) headless_indices = argv.each_index.select { |index| argv[index].match?(HEADLESS_PATTERN) } return argv if headless_indices.empty? raise ArgumentError, '--headless may only be specified once' if headless_indices.length > 1 if argv.any? { |arg| arg.start_with?(DETACHABLE_CLIENT_PREFIX) } raise ArgumentError, '--headless cannot be combined with --detachable-client' end headless_index = headless_indices.first headless_arg = argv[headless_index] inline_match = HEADLESS_PATTERN.match(headless_arg) port_token = inline_match[1] if port_token.nil? next_arg = argv[headless_index + 1] if next_arg.nil? || next_arg.start_with?('--') raise ArgumentError, '--headless requires a port number or auto' end port_token = next_arg argv.delete_at(headless_index + 1) end argv[headless_index] = '--without-frontend' argv.insert(headless_index + 1, "#{DETACHABLE_CLIENT_PREFIX}#{normalize_headless_port(port_token)}") argv end |
.normalize_headless_port(token) ⇒ Integer
Normalizes the headless port token. Converts the token to an integer if it's not 'auto'.
60 61 62 63 64 65 66 67 68 69 |
# File 'documented/main/arg_normalization.rb', line 60 def self.normalize_headless_port(token) return 0 if token.to_s.casecmp('auto').zero? port = Integer(token, 10) return port if port.positive? && port <= 65_535 raise ArgumentError, '--headless requires a port number between 1 and 65535, or auto' rescue ArgumentError raise ArgumentError, '--headless requires a port number between 1 and 65535, or auto' end |