Module: Lich::Main::ArgvOptions::OptionParser

Defined in:
documented/main/argv_options.rb

Overview

Parse ARGV and build @argv_options hash for backward compatibility

Class Method Summary collapse

Class Method Details

.execute ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Mutates ARGV by calling handle_sal_file, which may delete entries

Parses ARGV into an options hash for backward compatibility.

Walks ARGV matching each argument against patterns for recognized CLI flags (--start-scripts, --host, --game, etc.). Early-exit operations (--install, --link-to-sge, --unlink-from-sal, etc.) print to stdout and call exit(). SAL/Gse files and --dark-mode are handled by helper methods. Invalid --fe-version values are tracked and warned about after the loop.

Returns:

  • (Hash) —

    options hash with Symbol keys (e.g., :host, :game, :account); early-exit operations do not return



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
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
# File 'documented/main/argv_options.rb', line 39

def self.execute
  @argv_options = {}
  bad_args = []
  invalid_fe_version = false

  ARGV.each do |arg|
    case arg
    when '--link-to-sge'
      result = Lich.link_to_sge
      $stdout.puts(result ? 'Successfully linked to SGE.' : 'Failed to link to SGE.') if $stdout.isatty
      exit
    when '--unlink-from-sge'
      result = Lich.unlink_from_sge
      $stdout.puts(result ? 'Successfully unlinked from SGE.' : 'Failed to unlink from SGE.') if $stdout.isatty
      exit
    when '--link-to-sal'
      result = Lich.link_to_sal
      $stdout.puts(result ? 'Successfully linked to SAL files.' : 'Failed to link to SAL files.') if $stdout.isatty
      exit
    when '--unlink-from-sal'
      result = Lich.unlink_from_sal
      $stdout.puts(result ? 'Successfully unlinked from SAL files.' : 'Failed to unlink from SAL files.') if $stdout.isatty
      exit
    when '--install'
      if Lich.link_to_sge && Lich.link_to_sal
        $stdout.puts 'Install was successful.'
        Lich.log 'Install was successful.'
      else
        $stdout.puts 'Install failed.'
        Lich.log 'Install failed.'
      end
      exit
    when '--uninstall'
      if Lich.unlink_from_sge && Lich.unlink_from_sal
        $stdout.puts 'Uninstall was successful.'
        Lich.log 'Uninstall was successful.'
      else
        $stdout.puts 'Uninstall failed.'
        Lich.log 'Uninstall failed.'
      end
      exit
    when /^--start-scripts=(.+)$/i
      @argv_options[:start_scripts] = $1
    when /^--reconnect$/i
      @argv_options[:reconnect] = true
    when /^--reconnect-delay=(.+)$/i
      @argv_options[:reconnect_delay] = $1
    when /^--host=(.+):(.+)$/
      @argv_options[:host] = { domain: $1, port: $2.to_i }
    when /^--bind-address=(.+)$/i
      @argv_options[:bind_address] = $1
    when /^--hosts-file=(.+)$/i
      @argv_options[:hosts_file] = $1
    when /^--no-(?:gui|gtk)$/i
      @argv_options[:gui] = false
    when /^--gui$/i
      @argv_options[:gui] = true
    when /^--game=(.+)$/i
      @argv_options[:game] = $1
    when /^--auth-provider=(eaccess|web)$/i
      @argv_options[:auth_provider] = $1.downcase.to_sym
    when /^--game-transport=(direct|websocket)$/i
      # :websocket is opt-in only (Genie5#356 phase 2) -- direct TCP
      # to the game port remains the default; see Lich::Common::GameTransport.
      @argv_options[:game_transport] = $1.downcase.to_sym
    when /^--account=(.+)$/i
      @argv_options[:account] = $1
    when /^--password=(.+)$/i
      @argv_options[:password] = $1
    when /^--character=(.+)$/i
      @argv_options[:character] = $1
    when /^--frontend=(.+)$/i
      @argv_options[:frontend] = $1
    when /\A--fe-version=([A-Za-z0-9._+-]{1,32})\z/i
      # \A..\z, not ^..$: the value is written onto the game socket, so an
      # embedded newline must not let a valid first line slip through.
      @argv_options[:fe_version] = $1
    when /\A--fe-version=/i
      # Without this, a rejected value would silently send the default /VERSION:.
      invalid_fe_version = true
    when /^--frontend-command=(.+)$/i
      @argv_options[:frontend_command] = $1
    when /^--save$/i
      @argv_options[:save] = true
    when /^--pipe$/i
      @argv_options[:pipe] = true
    when /^--sentinel$/i
      @argv_options[:sentinel] = true
    when /^--keep-fe-string$/i
      # Kill switch for Frontend.proxy_handshake: forward the front end's own /FE: line.
      @argv_options[:keep_fe_string] = true
    when /^--frontend-caps=(.*)$/i
      @argv_options[:frontend_caps] = $1
    when /^--wine(?:\-prefix)?=.+$/i
      nil # already used when defining the Wine module
    when /\.sal$|Gse\.~xt$/i
      handle_sal_file(arg)
      bad_args.clear
    when /^--dark-mode=(true|false|on|off)$/i
      handle_dark_mode($1)
    when /^--saga$/i
      $frontend = 'saga'
    else
      bad_args.push(arg)
    end
  end

  # Warn after the loop so the message names the value actually in effect.
  if invalid_fe_version
    warning = 'warning: ignoring invalid --fe-version (allowed: 1-32 of A-Z a-z 0-9 . _ + -); ' \
              "using #{@argv_options[:fe_version] || 'the default'}"
    $stdout.puts warning
    Lich.log warning
  end

  @argv_options
end

.handle_dark_mode(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Converts a dark-mode string value to a strict boolean.

Regex matching on "true"|"on" (case-insensitive) is converted to true or false. This strict conversion ensures the startup theme handler receives a boolean, not a regex match result (Integer or nil).

Parameters:

  • value (String) —

    one of "true", "false", "on", "off" (case-insensitive)



185
186
187
188
# File 'documented/main/argv_options.rb', line 185

def self.handle_dark_mode(value)
  # Regex returns Integer/nil; force strict boolean for startup handling.
  @argv_options[:dark_mode] = !!(value =~ /^(true|on)$/i)
end

.handle_sal_file(arg) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Records a SAL or Gse.~xt file path in argv_options and searches common locations.

Stores the file path under :sal. If it does not exist, searches ARGV for a fully-qualified Windows path (e.g., C:\path\file.sal). If still not found and Wine is defined, remaps the path under Wine::PREFIX/drive_c/.

Parameters:

  • arg (String) —

    a SAL or Gse.~xt file path (relative, absolute, or Windows)



166
167
168
169
170
171
172
173
174
# File 'documented/main/argv_options.rb', line 166

def self.handle_sal_file(arg)
  @argv_options[:sal] = arg
  unless File.exist?(@argv_options[:sal])
    @argv_options[:sal] = $1 if ARGV.join(' ') =~ /([A-Z]:\\.+?\.(?:sal|~xt))/i
  end
  unless File.exist?(@argv_options[:sal])
    @argv_options[:sal] = "#{Wine::PREFIX}/drive_c/#{@argv_options[:sal][3..-1].split('\\').join('/')}" if defined?(Wine)
  end
end