Class: Lich::Common::CLI::CliOptionsRegistry
- Inherits:
-
Object
- Object
- Lich::Common::CLI::CliOptionsRegistry
- Defined in:
- documented/common/cli/cli_options_registry.rb
Overview
Manages command-line options and their configurations.
This class provides methods to define, retrieve, and validate command-line options for the application.
Class Method Summary collapse
-
.all_options ⇒ Hash
Returns a duplicate of all defined options.
-
.get_handler(name) ⇒ Proc?
Retrieves the handler for a specified option.
-
.get_option(name) ⇒ Hash?
Retrieves the configuration for a specified option.
-
.option(name, type: :string, default: nil, deprecated: false, deprecation_message: nil, mutually_exclusive: [], handler: nil) ⇒ void
Defines a command-line option with its configuration.
-
.to_opts_schema ⇒ Hash
Converts the defined options into a schema format.
-
.validate(parsed_opts) ⇒ Array<String>
Validates the parsed options against defined configurations.
Class Method Details
.all_options ⇒ Hash
Returns a duplicate of all defined options.
49 50 51 |
# File 'documented/common/cli/cli_options_registry.rb', line 49 def @options.dup end |
.get_handler(name) ⇒ Proc?
Retrieves the handler for a specified option.
57 58 59 |
# File 'documented/common/cli/cli_options_registry.rb', line 57 def get_handler(name) @handlers[name] end |
.get_option(name) ⇒ Hash?
Retrieves the configuration for a specified option.
42 43 44 |
# File 'documented/common/cli/cli_options_registry.rb', line 42 def get_option(name) @options[name] end |
.option(name, type: :string, default: nil, deprecated: false, deprecation_message: nil, mutually_exclusive: [], handler: nil) ⇒ void
This method returns an undefined value.
Defines a command-line option with its configuration.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'documented/common/cli/cli_options_registry.rb', line 26 def option(name, type: :string, default: nil, deprecated: false, deprecation_message: nil, mutually_exclusive: [], handler: nil) @options[name] = { type: type, default: default, deprecated: deprecated, deprecation_message: , mutually_exclusive: Array(mutually_exclusive) } @handlers[name] = handler if handler end |
.to_opts_schema ⇒ Hash
Converts the defined options into a schema format.
97 98 99 100 101 102 103 104 105 106 |
# File 'documented/common/cli/cli_options_registry.rb', line 97 def to_opts_schema schema = {} @options.each do |name, config| schema[name] = { type: config[:type], default: config[:default] } end schema end |
.validate(parsed_opts) ⇒ Array<String>
Validates the parsed options against defined configurations.
This method checks for mutually exclusive options and deprecation warnings.
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 |
# File 'documented/common/cli/cli_options_registry.rb', line 67 def validate(parsed_opts) errors = [] # Check mutually exclusive options @options.each do |option_name, config| next unless parsed_opts.respond_to?(option_name) && parsed_opts.public_send(option_name) next if config[:mutually_exclusive].empty? config[:mutually_exclusive].each do |exclusive_option| if parsed_opts.respond_to?(exclusive_option) && parsed_opts.public_send(exclusive_option) errors << "Options --#{option_name} and --#{exclusive_option} are mutually exclusive" end end end # Check for deprecation warnings @options.each do |option_name, config| next unless config[:deprecated] next unless parsed_opts.respond_to?(option_name) && parsed_opts.public_send(option_name) = config[:deprecation_message] || "Option --#{option_name} is deprecated and will be removed in a future version" Lich.log "warning: #{}" end errors end |