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
-
.extract_flag_value(flag, usage:, valid_values: nil) ⇒ String?
The flag's value, or nil if the flag was not supplied.
-
.reject_invalid_value(flag, value, valid_values:, usage:) ⇒ void
Reports a flag value that failed validation and exits.
-
.require_positional(value, name:, usage:) ⇒ String
Validates a required positional argument (e.g. the ACCOUNT in
--refresh-characters ACCOUNT).
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.
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.
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.
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 |