Class: Lich::Common::CLI::CliOptionsRegistry

Inherits:
Object
  • Object
show all
Defined in:
documented/common/cli/cli_options_registry.rb

Overview

Manages command-line options and their configurations.

Examples:

Registering an option

CliOptionsRegistry.option(:verbose, type: :boolean, default: false)

Class Method Summary collapse

Class Method Details

.all_optionsHash

Returns a duplicate of all registered options.

Examples:

Retrieving all options

options = CliOptionsRegistry.all_options

Returns:

  • (Hash)

    A hash of all options and their configurations.



49
50
51
# File 'documented/common/cli/cli_options_registry.rb', line 49

def all_options
  @options.dup
end

.get_handler(name) ⇒ Proc?

Retrieves the handler for a specified option.

Examples:

Getting an option handler

handler = CliOptionsRegistry.get_handler(:verbose)

Parameters:

  • name (Symbol)

    The name of the option whose handler is to be retrieved.

Returns:

  • (Proc, nil)

    The handler for the option or nil if not found.



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.

Examples:

Getting an option configuration

config = CliOptionsRegistry.get_option(:verbose)

Parameters:

  • name (Symbol)

    The name of the option to retrieve.

Returns:

  • (Hash, nil)

    The option configuration or nil if not found.



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.

Examples:

Registering a string option

CliOptionsRegistry.option(:name, type: :string, default: "default_name")

Parameters:

  • name (Symbol)

    The name of the option.

  • type (Symbol) (defaults to: :string)

    The type of the option (default: :string).

  • default (Object) (defaults to: nil)

    The default value for the option.

  • deprecated (Boolean) (defaults to: false)

    Indicates if the option is deprecated (default: false).

  • deprecation_message (String) (defaults to: nil)

    The message to show when the option is used if deprecated.

  • mutually_exclusive (Array<Symbol>) (defaults to: [])

    An array of options that are mutually exclusive to this option.

  • handler (Proc) (defaults to: nil)

    An optional handler for the option.



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: deprecation_message,
    mutually_exclusive: Array(mutually_exclusive)
  }
  @handlers[name] = handler if handler
end

.to_opts_schemaHash

Converts the registered options into a schema format.

Examples:

Getting the options schema

schema = CliOptionsRegistry.to_opts_schema

Returns:

  • (Hash)

    A schema representation of the options.



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.

Examples:

Validating options

errors = CliOptionsRegistry.validate(parsed_options)

Parameters:

  • parsed_opts (Object)

    The parsed options object to validate.

Returns:

  • (Array<String>)

    An array of error messages for invalid 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)

    message = config[:deprecation_message] || "Option --#{option_name} is deprecated and will be removed in a future version"
    Lich.log "warning: #{message}"
  end

  errors
end