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.
Class Method Summary collapse
-
.all_options ⇒ Hash
Returns a duplicate of all registered 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
Registers a command-line option with its configuration.
-
.to_opts_schema ⇒ Hash
Converts the registered options into a schema format.
-
.validate(parsed_opts) ⇒ Array<String>
Validates the parsed options against the registered options.
Class Method Details
.all_options ⇒ Hash
Returns a duplicate of all registered 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.
58 59 60 |
# File 'documented/common/cli/cli_options_registry.rb', line 58 def get_handler(name) @handlers[name] end |
.get_option(name) ⇒ Hash?
Retrieves the configuration for a specified option.
41 42 43 |
# File 'documented/common/cli/cli_options_registry.rb', line 41 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.
Registers a command-line option with its configuration.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'documented/common/cli/cli_options_registry.rb', line 24 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 registered options into a schema format.
98 99 100 101 102 103 104 105 106 107 |
# File 'documented/common/cli/cli_options_registry.rb', line 98 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 the registered options.
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 |