Class: Rubydex::CLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rubydex/cli/command.rb,
lib/rubydex/cli/command/mcp.rb,
lib/rubydex/cli/command/lint.rb,
lib/rubydex/cli/command/query.rb,
lib/rubydex/cli/command/skill.rb,
lib/rubydex/cli/command/console.rb,
lib/rubydex/cli/command/lint/explain.rb

Overview

rdx console — opens an IRB session with a populated graph for the current workspace.

Direct Known Subclasses

Lint::Explain

Defined Under Namespace

Classes: Console, Lint, Mcp, Query, Skill

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

: (Array argv) -> void



93
94
95
# File 'lib/rubydex/cli/command.rb', line 93

def initialize(argv)
  @argv = argv
end

Class Attribute Details

.command_nameObject (readonly)

The subcommand name declared by command. nil for a class that declares none, which is how an abstract intermediate class opts out of being dispatched. : String?



27
28
29
# File 'lib/rubydex/cli/command.rb', line 27

def command_name
  @command_name
end

Class Method Details

.allObject

Every subcommand, in alphabetical order. Loads the command files first, so the answer does not depend on what happens to have been required already. : -> Array



79
80
81
82
83
# File 'lib/rubydex/cli/command.rb', line 79

def all
  Dir.glob(File.expand_path("command/*.rb", __dir__)).sort.each { |file| require(file) }

  declared
end

.arguments(spec = nil) ⇒ Object

Declares the argument spec shown after the command name in rdx help, e.g. "<CYPHER>". Called without an argument, returns the declared spec. : (?String? spec) -> String?



47
48
49
50
51
# File 'lib/rubydex/cli/command.rb', line 47

def arguments(spec = nil)
  return @arguments unless spec

  @arguments = spec #: String?
end

.command(value) ⇒ Object

Declares the subcommand name this class is dispatched under.

Deliberately not called name: that would shadow Class#name, which Ruby uses when it builds error messages, so a NoMethodError on a command would report "an instance of query" instead of naming the class. : (String value) -> void

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/rubydex/cli/command.rb', line 35

def command(value)
  # Deliberately checked against {.declared} rather than {.find}: this runs while a command
  # file is still being loaded, and `find` would glob in every other command file first.
  clash = declared.find { |other| !other.equal?(self) && other.command_name == value }
  raise ArgumentError, "command `#{value}` is already declared by #{clash}" if clash

  @command_name = value #: String?
end

.declaredObject

The subcommands that are currently loaded, in alphabetical order. Loads nothing itself, so it is safe to call while a command file is still being evaluated. : -> Array



72
73
74
# File 'lib/rubydex/cli/command.rb', line 72

def declared
  Command.subclasses.select(&:command_name).sort_by(&:command_name)
end

.find(name) ⇒ Object

The subcommand declared under name, if any. : (String? name) -> singleton(Command)?



87
88
89
# File 'lib/rubydex/cli/command.rb', line 87

def find(name)
  all.find { |command| command.command_name == name }
end

.summary(text = nil) ⇒ Object

Declares the description shown in rdx help. May span several lines, which are aligned under the first when the command list is rendered. Called without an argument, returns the declared description. : (?String? text) -> String?



57
58
59
60
61
# File 'lib/rubydex/cli/command.rb', line 57

def summary(text = nil)
  return @summary unless text

  @summary = text.strip #: String?
end

.usage_formObject

How the command is spelled in a usage line, e.g. "query <CYPHER>". : -> String



65
66
67
# File 'lib/rubydex/cli/command.rb', line 65

def usage_form
  [command_name, arguments].compact.join(" ")
end

Instance Method Details

#runObject

: -> void

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/rubydex/cli/command.rb', line 98

def run
  raise NotImplementedError, "#{self.class} must implement #run"
end