Module: Lich::Main::EarlyExit

Defined in:
documented/main/early_exit.rb

Overview

Commands that print to stdout and exit without starting a session.

lich.rbw dispatches these before Lich::GemCheck.verify! and before lib/init.rb requires GTK, so they stay usable on a runtime that cannot load a toolkit. Without that ordering, --help cannot report the very options that make a headless launch work.

The allow-list is deliberately syntactic. It reads ARGV and nothing else. DISPLAY, TTY, and cron state never select a launch mode here, which keeps the guarantee that #1439 introduced.

Membership is limited to commands that need no directory, no log file, and no database. lib/init.rb creates all three after the GTK require, so a command that needs any of them cannot move into this module.

Constant Summary collapse

HELP_FLAGS =

Command-line flags that trigger help output.

See Also:

  • #help?
  • #dispatch!
['-h', '--help'].freeze
HELP_TOPIC_PREFIX =
'--help='
VERSION_FLAGS =

Command-line flags that trigger version information output.

See Also:

  • #version?
  • #dispatch!
['-v', '--version'].freeze

Class Method Summary collapse

Class Method Details

.dispatch!(argv = ARGV) ⇒ void

This method returns an undefined value.

Prints the first selected command and exits. Returns when the arguments select no command handled here.

Parameters:

  • argv (Array<String>) (defaults to: ARGV) —

    command-line arguments



46
47
48
49
50
51
52
53
54
55
56
57
# File 'documented/main/early_exit.rb', line 46

def dispatch!(argv = ARGV)
  argv = Array(argv)
  argv.each do |arg|
    if help?(arg)
      print_help(HelpText.topic_from_argv(argv, arg))
      exit
    elsif version?(arg)
      print_version
      exit
    end
  end
end

.help?(arg) ⇒ Boolean

Parameters:

  • arg (String) —

    one command-line argument

Returns:

  • (Boolean)


67
68
69
# File 'documented/main/early_exit.rb', line 67

def help?(arg)
  HELP_FLAGS.include?(arg) || arg.to_s.start_with?(HELP_TOPIC_PREFIX)
end

This method returns an undefined value.

Parameters:

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

    optional help topic



79
80
81
# File 'documented/main/early_exit.rb', line 79

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

This method returns an undefined value.



84
85
86
87
88
89
90
91
92
93
94
# File 'documented/main/early_exit.rb', line 84

def 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

.requested?(argv = ARGV) ⇒ Boolean

Returns whether the arguments select a command handled here.

Parameters:

  • argv (Array<String>) (defaults to: ARGV) —

    command-line arguments

Returns:

  • (Boolean) —

    whether the arguments select a command handled here



61
62
63
# File 'documented/main/early_exit.rb', line 61

def requested?(argv = ARGV)
  Array(argv).any? { |arg| help?(arg) || version?(arg) }
end

.version?(arg) ⇒ Boolean

Parameters:

  • arg (String) —

    one command-line argument

Returns:

  • (Boolean)


73
74
75
# File 'documented/main/early_exit.rb', line 73

def version?(arg)
  VERSION_FLAGS.include?(arg)
end