Class: LLM::Repl::Command
- Inherits:
-
Object
- Object
- LLM::Repl::Command
- Defined in:
- lib/llm/repl/command.rb
Overview
The LLM::Repl::Command class is the superclass
of all read-eval-print loop commands. A command has a name, and a
description. This basic version does not implement parameters. A
command is accessible via the / prefix: eg /exit.
Direct Known Subclasses
Defined Under Namespace
Classes: Exit, Parameter, Quit
Class Method Summary collapse
-
.description(description = UNDEFINED) ⇒ String
Set or get a command description.
-
.find_by(input: UNDEFINED, name: UNDEFINED) ⇒ LLM::Repl::Command?
Find a command by a name, or by an input string.
-
.help ⇒ void
Display a formatted help message for this command.
- .inherited(command) ⇒ void
-
.name(name = UNDEFINED) ⇒ String
Set or get a command name.
- .parameter(name, type, description, options = {}) ⇒ void
- .parameters ⇒ Hash
-
.registry ⇒ Array<LLM::Repl::Command]
Array<LLM::Repl::Command].
- .required(names) ⇒ void
Instance Method Summary collapse
-
#call ⇒ Object
This method should be implemented by subclasses.
- #initialize(repl) ⇒ LLM::Repl::Command constructor
- #parameters ⇒ Hash<Symbol, Parameter>
-
#write(str, who: "command(#{self.class.name}): ") ⇒ void
Write a string to the transcript.
Constructor Details
#initialize(repl) ⇒ LLM::Repl::Command
148 149 150 |
# File 'lib/llm/repl/command.rb', line 148 def initialize(repl) @repl = repl end |
Class Method Details
.description(description = UNDEFINED) ⇒ String
Set or get a command description.
106 107 108 109 |
# File 'lib/llm/repl/command.rb', line 106 def self.description(description = UNDEFINED) return @description if description == UNDEFINED @description = description end |
.find_by(input: UNDEFINED, name: UNDEFINED) ⇒ LLM::Repl::Command?
The input string must be prefixed with "/" or it won't be matched. The match is made against the string before the first space - so "/exit foo" will match the "exit" command but "/exitnow" will not.
Find a command by a name, or by an input string.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/llm/repl/command.rb', line 61 def self.find_by(input: UNDEFINED, name: UNDEFINED) if input != UNDEFINED return nil unless input[0] == "/" n, = input.split(" ") registry.find { n[1..] == _1.name } elsif name != UNDEFINED registry.find { name == _1.name } else raise ArgumentError, "provide either an input or a name" end end |
.help ⇒ void
This method returns an undefined value.
Display a formatted help message for this command. Uses a single #write call to output the command name, description, and parameter details.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/llm/repl/command.rb', line 166 def self.help lines = [] lines << "Command: #{name}" lines << "Description: #{description}" unless parameters.empty? lines << "" lines << "Parameters:" parameters.each_value do |param| tag = param.required? ? "(required)" : "(optional)" lines << " #{param.name} [#{param.type}] - #{param.description} #{tag}" end end lines.join("\n") end |
.inherited(command) ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 83 |
# File 'lib/llm/repl/command.rb', line 77 def self.inherited(command) LLM.lock(:inherited) do registry << command command.instance_variable_set(:@parameters, {}) command.define_singleton_method(:inherited) { |command| SINGLETON.inherited(command) } end end |
.name(name = UNDEFINED) ⇒ String
Set or get a command name.
96 97 98 99 |
# File 'lib/llm/repl/command.rb', line 96 def self.name(name = UNDEFINED) return @name if name == UNDEFINED @name = name end |
.parameter(name, type, description, options = {}) ⇒ void
This method returns an undefined value.
117 118 119 120 121 122 123 |
# File 'lib/llm/repl/command.rb', line 117 def self.parameter(name, type, description, = {}) @parameters[name] = Parameter.new( name, type, description, , @parameters.size, nil ) end |
.parameters ⇒ Hash
127 128 129 |
# File 'lib/llm/repl/command.rb', line 127 def self.parameters @parameters end |
.registry ⇒ Array<LLM::Repl::Command]
Returns Array<LLM::Repl::Command].
87 88 89 |
# File 'lib/llm/repl/command.rb', line 87 def self.registry @registry ||= [] end |
.required(names) ⇒ void
This method returns an undefined value.
135 136 137 138 139 140 141 142 143 |
# File 'lib/llm/repl/command.rb', line 135 def self.required(names) names.each do |name| if @parameters.key?(name) @parameters[name].required! else raise LLM::Error, "'#{name}' is not a known parameter" end end end |
Instance Method Details
#call ⇒ Object
This method should be implemented by subclasses.
184 185 186 |
# File 'lib/llm/repl/command.rb', line 184 def call(...) raise NotImplementedError, "#{self.class}#call is not implemented" end |
#parameters ⇒ Hash<Symbol, Parameter>
190 191 192 |
# File 'lib/llm/repl/command.rb', line 190 def parameters self.class.parameters end |
#write(str, who: "command(#{self.class.name}): ") ⇒ void
This method returns an undefined value.
Write a string to the transcript
156 157 158 159 |
# File 'lib/llm/repl/command.rb', line 156 def write(str, who: "command(#{self.class.name}): ") @repl.write(who, Curses::A_BOLD) @repl.write(str) end |