ruby/lib/irb/command.rb
Stan Lo 04ba96e619 [ruby/irb] Allow defining custom commands in IRB
(https://github.com/ruby/irb/pull/886)

This is a feature that has been requested for a long time. It is now
possible to define custom commands in IRB.

Example usage:

```ruby
require "irb/command"

class HelloCommand < IRB::Command::Base
  description "Prints hello world"
  category "My commands"
  help_message "It doesn't do more than printing hello world."

  def execute
    puts "Hello world"
  end
end

IRB::Command.register(:hello, HelloCommand)
```

888643467c
2024-04-14 11:01:43 +00:00

29 lines
709 B
Ruby

# frozen_string_literal: true
#
# irb/command.rb - irb command
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
require_relative "command/base"
module IRB # :nodoc:
module Command
@commands = {}
class << self
attr_reader :commands
# Registers a command with the given name.
# Aliasing is intentionally not supported at the moment.
def register(name, command_class)
@commands[name] = [command_class, []]
end
# This API is for IRB's internal use only and may change at any time.
# Please do NOT use it.
def _register_with_aliases(name, command_class, *aliases)
@commands[name] = [command_class, aliases]
end
end
end
end