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

Defined in:
documented/main/argv_options.rb

Overview

Module for parsing command-line options.

This module is responsible for executing the command-line options and managing the associated side effects.

Class Method Summary collapse

Class Method Details

.executeObject



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'documented/main/argv_options.rb', line 27

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

  ARGV.each do |arg|
    case arg
    when '-h', '--help', /^--help=.+$/
      print_help(HelpText.topic_from_argv(ARGV, arg))
      exit
    when '-v', '--version'
      print_version
      exit
    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 /^--hosts-file=(.+)$/i
      @argv_options[:hosts_file] = $1
    when /^--no-gui$/i
      @argv_options[:gui] = false
    when /^--gui$/i
      @argv_options[:gui] = true
    when /^--game=(.+)$/i
      @argv_options[:game] = $1
    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 /^--frontend-command=(.+)$/i
      @argv_options[:frontend_command] = $1
    when /^--save$/i
      @argv_options[:save] = true
    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)
    else
      bad_args.push(arg)
    end
  end

  @argv_options
end

.handle_dark_mode(value) ⇒ void

This method returns an undefined value.

Handles the dark mode setting based on command-line arguments.

This method updates the dark mode setting in @argv_options and applies it to the GTK settings if applicable.

Parameters:

  • value (String)

    the value indicating dark mode preference (e.g., "true", "false")



136
137
138
139
140
141
142
143
# File 'documented/main/argv_options.rb', line 136

def self.handle_dark_mode(value)
  # Regex returns Integer/nil; force strict boolean for persisted settings.
  @argv_options[:dark_mode] = !!(value =~ /^(true|on)$/i)
  if defined?(Gtk)
    @theme_state = Lich.track_dark_mode = @argv_options[:dark_mode]
    Gtk::Settings.default.gtk_application_prefer_dark_theme = true if @theme_state == true
  end
end

.handle_sal_file(arg) ⇒ void

This method returns an undefined value.

Handles the processing of .sal files from command-line arguments.

This method checks if the specified .sal file exists and updates the @argv_options accordingly.

Parameters:

  • arg (String)

    the .sal file argument from ARGV



121
122
123
124
125
126
127
128
129
# File 'documented/main/argv_options.rb', line 121

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

This method returns an undefined value.

Prints help text for the CLI.

This method displays help information based on the provided topic.

Parameters:

  • topic (String, nil) (defaults to: nil)

    the specific help topic to display



150
151
152
# File 'documented/main/argv_options.rb', line 150

def self.print_help(topic = nil)
  puts HelpText.render(topic)
end

This method returns an undefined value.

Prints the version information of the Lich project.

This method outputs the version and copyright information to the console.



158
159
160
161
162
163
164
165
166
167
168
# File 'documented/main/argv_options.rb', line 158

def self.print_version
  puts "The Lich, version #{LICH_VERSION}"
  puts ' (an implementation of the Ruby interpreter by Yukihiro Matsumoto designed to be a \'script engine\' for text-based MUDs)'
  puts ''
  puts '- The Lich program and all material collectively referred to as "The Lich project" is copyright (C) 2005-2006 Murray Miron.'
  puts '- The Gemstone IV and DragonRealms games are copyright (C) Simutronics Corporation.'
  puts '- The Wizard front-end and the StormFront front-end are also copyrighted by the Simutronics Corporation.'
  puts '- Ruby is (C) Yukihiro \'Matz\' Matsumoto.'
  puts ''
  puts 'Thanks to all those who\'ve reported bugs and helped me track down problems on both Windows and Linux.'
end