Module: Lich::Common::CLI::CliOptionValidator

Defined in:
documented/common/cli/cli_option_validator.rb

Overview

Extracts and validates optional flag values (e.g. --frontend wizard) from ARGV. Exits the process with a usage message when a flag is present but malformed: missing its value, followed by another flag instead of a value, or holding a value outside an explicit allow-list.

Class Method Summary collapse

Class Method Details

.extract_flag_value(flag, usage:, valid_values: nil) ⇒ String?

Returns the flag's value, or nil if the flag was not supplied.

Parameters:

  • flag (String) —

    flag name including leading dashes, e.g. '--frontend'

  • usage (String) —

    usage line printed alongside any error message

  • valid_values (Array<String>, nil) (defaults to: nil) —

    allowed values; nil skips content validation

Returns:

  • (String, nil) —

    the flag's value, or nil if the flag was not supplied



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'documented/common/cli/cli_option_validator.rb', line 18

def self.extract_flag_value(flag, usage:, valid_values: nil)
  return nil unless ARGV.include?(flag)

  value = ARGV[ARGV.index(flag) + 1]

  if value.nil? || value.start_with?('-')
    $stdout.puts "error: #{flag} requires a value"
    $stdout.puts usage
    exit 1
  end

  reject_invalid_value(flag, value, valid_values: valid_values, usage: usage) if valid_values && !valid_values.include?(value)

  value
end

.reject_invalid_value(flag, value, valid_values:, usage:) ⇒ void

This method returns an undefined value.

Reports a flag value that failed validation and exits. Exposed so callers that test a value with a domain predicate rather than a literal allow-list (e.g. --game-code, checked with LoginHelpers.valid_game_code?) still report the failure the same way this module does.

Parameters:

  • flag (String) —

    flag name including leading dashes, e.g. '--game-code'

  • value (String) —

    the rejected value

  • valid_values (Array<String>) —

    values to list back to the user

  • usage (String) —

    usage line printed alongside the error message



44
45
46
47
48
49
# File 'documented/common/cli/cli_option_validator.rb', line 44

def self.reject_invalid_value(flag, value, valid_values:, usage:)
  $stdout.puts "error: Invalid value for #{flag}: #{value}"
  $stdout.puts "Valid values: #{valid_values.join(', ')}"
  $stdout.puts usage
  exit 1
end

.require_positional(value, name:, usage:) ⇒ String

Validates a required positional argument (e.g. the ACCOUNT in --refresh-characters ACCOUNT). Exits the process with a usage message when the argument is absent, or when it looks like an option rather than a value - --add-character DOUG --game-code DR otherwise reads "--game-code" as the character name and goes on to persist it.

Parameters:

  • value (String, nil) —

    the positional argument as read from ARGV

  • name (String) —

    argument name used in the error message, e.g. 'CHAR_NAME'

  • usage (String) —

    usage line printed alongside any error message

Returns:

  • (String) —

    the validated value



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'documented/common/cli/cli_option_validator.rb', line 61

def self.require_positional(value, name:, usage:)
  if value.nil?
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts usage
    exit 1
  end

  if value.start_with?('-')
    $stdout.puts "error: Expected #{name}, got option '#{value}'"
    $stdout.puts usage
    exit 1
  end

  value
end