Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support command registration #886

Merged
merged 1 commit into from
Apr 14, 2024
Merged

Support command registration #886

merged 1 commit into from
Apr 14, 2024

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Feb 23, 2024

Overview

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

Example usage:

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)

And the command will appear in the help message:

Help
  help           List all available commands. Use `help <command>` to get information about a specific command.

My commands
  hello          Prints hello world

IRB
  ....

Details

  • All the default command definition stuff is now moved to lib/irb/default_commands.rb so requiring irb/command is very lightweight.
  • 3 Command APIs are introduced
    • Command.register(command_name, command_class) is the official API to register a command. I decided not to accept alias for now as the current aliasing mechanism is messy.
    • Command._register_with_aliases(name, command_class, *aliases) is an internal API for IRB to register its existing commands and their aliases.
    • Command.commands to get all the registered commands
  • Many command handling methods are simplified with the change as well.

I think this is should probably be introduced before #624 because:

  • It already simplifies IRB's command registration and will make future refactor easier
  • I still haven't figured out how to better display helper methods in the help message
  • The community is already familiar with the concept of commands, so I imagine this will be easy to adopt. And with custom commands, it'll help both us and the community understand the role of helper methods.

Closes #513

@st0012 st0012 added the enhancement New feature or request label Feb 23, 2024
lib/irb/default_commands.rb Outdated Show resolved Hide resolved
lib/irb/default_commands.rb Outdated Show resolved Hide resolved
lib/irb/default_commands.rb Outdated Show resolved Hide resolved
@st0012 st0012 requested a review from a team February 25, 2024 07:11
@st0012 st0012 force-pushed the support-command-extension branch from 6defafa to f43f1fb Compare February 25, 2024 07:17
@st0012 st0012 self-assigned this Feb 25, 2024
@st0012 st0012 force-pushed the support-command-extension branch from f43f1fb to f6f50a3 Compare February 25, 2024 16:13
@tompng
Copy link
Member

tompng commented Feb 26, 2024

I think this is basically OK 👍
One concern is that once we release this public customizing API, fixing #592 might be harder than before (to keep public API backward compatibility).

What do you think? I think I'm ready with #824 that fixes #592.

I don't want to make transform_args a public API. (We can restrict only for default commands)
If we use the method name execute(*ruby_args, **ruby_kwargs), execution method for raw string that previously used transform_args should be a different name like execute_raw(string_arg). I think this method should be named execute.

@st0012 st0012 force-pushed the support-command-extension branch 2 times, most recently from 36ee6c5 to b074da4 Compare April 11, 2024 20:14
@st0012
Copy link
Member Author

st0012 commented Apr 11, 2024

@tompng I've rebased the PR and made a few more improvements. Can you give it another look?

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)
```
@st0012 st0012 force-pushed the support-command-extension branch from b074da4 to 06a295d Compare April 12, 2024 16:06
Copy link
Member

@tompng tompng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@tompng tompng merged commit 8886434 into master Apr 14, 2024
57 checks passed
@tompng tompng deleted the support-command-extension branch April 14, 2024 11:01
matzbot pushed a commit to ruby/ruby that referenced this pull request Apr 14, 2024
(ruby/irb#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)
```

ruby/irb@888643467c
tenderlove added a commit to tenderlove/ruby that referenced this pull request Apr 15, 2024
* master: (29 commits)
  Not all `nm`s support the `--help` option
  Emit a performance warning when redefining specially optimized methods
  YJIT: A64: Avoid intermediate register in `opt_and` and friends (ruby#10509)
  [ruby/reline] Remove not implemented method (ruby/reline#680)
  [ruby/reline] Fix vi_to_column which was broken (ruby/reline#679)
  Include more debug information in test_uplus_minus
  [Universal parser] DeVALUE of p->debug_lines and ast->body.script_lines
  Add more assertions in `test_uplus_minus`
  `super{}` doesn't use block
  fix incorrect warning.
  Update default gems list at fc36394 [ci skip]
  [ruby/optparse] bump up to 0.5.0
  show warning for unused block
  Emit `warn` event for duplicated hash keys on ripper
  [ruby/reline] Refactored Default Key Bindings (ruby/reline#678)
  [ruby/reline] Refactor waiting_proc and waiting_operator_proc (ruby/reline#649)
  [pty] Fix missing `or`
  [pty] Fix `ptsname_r` fallback
  [ruby/irb] Allow defining custom commands in IRB (ruby/irb#886)
  [ruby/reline] Support `menu-complete-backward` command for upward navigation (ruby/reline#677)
  ...
@st0012 st0012 changed the title Allow defining custom commands in IRB Support command registration Apr 25, 2024
artur-intech pushed a commit to artur-intech/ruby that referenced this pull request Apr 26, 2024
(ruby/irb#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)
```

ruby/irb@888643467c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Support command extension APIs
2 participants