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.

This class provides methods to define, retrieve, and validate command-line options for the application.

See Also:

Class Method Summary collapse

Class Method Details

.all_optionsHash

Returns a duplicate of all defined 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.

Parameters:

  • name (Symbol)

    the name of the option to retrieve the handler for

Returns:

  • (Proc, nil)

    the handler for the option or nil if not found



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.

Parameters:

  • name (Symbol)

    the name of the option to retrieve

Returns:

  • (Hash, nil)

    the option configuration or nil if not found



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.

Parameters:

  • name (Symbol)

    the name of the option

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

    the type of the option (default: :string)

  • default (Object, nil) (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, nil) (defaults to: nil)

    message to show when the option is used

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

    options that cannot be used together

  • handler (Proc, nil) (defaults to: nil)

    a handler for the option



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

.to_opts_schemaHash

Converts the defined options into a schema format.

Returns:

  • (Hash)

    a schema representation of the options



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.

Parameters:

  • parsed_opts (OpenStruct)

    the parsed command-line options

Returns:

  • (Array<String>)

    an array of error messages, if any



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