diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2022-11-10 14:55:11 -0800 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-11-10 22:55:15 +0000 |
commit | 8fa83fa0b2031ad17f01b5a12b39599398dc6da6 (patch) | |
tree | 99b01439ea80f7b3c3a2d2425def6d53e8bdb747 | |
parent | d5513da01d24fbc4de71975b6a49f0ba3b3be401 (diff) |
[ruby/irb] Transform ls's --grep/-G option to keyword args
(https://github.com/ruby/irb/pull/437)
* Transform ls's --grep/-G option to keyword args
* Make --grep less flexible
* Support -g instead of --grep
* Suppress warnings from symbol aliases
-rw-r--r-- | lib/irb.rb | 1 | ||||
-rw-r--r-- | lib/irb/cmd/ls.rb | 9 | ||||
-rw-r--r-- | test/irb/test_cmd.rb | 38 |
3 files changed, 48 insertions, 0 deletions
diff --git a/lib/irb.rb b/lib/irb.rb index 57ec9ebaeb..04009664ef 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -427,6 +427,7 @@ module IRB @context = Context.new(self, workspace, input_method) @context.main.extend ExtendCommandBundle @context.command_aliases.each do |alias_name, cmd_name| + next if @context.symbol_alias(alias_name) @context.main.install_alias_method(alias_name, cmd_name) end @signal_status = :IN_IRB diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index f4a7348bd1..77cf071783 100644 --- a/lib/irb/cmd/ls.rb +++ b/lib/irb/cmd/ls.rb @@ -9,6 +9,15 @@ module IRB module ExtendCommand class Ls < Nop + def self.transform_args(args) + if match = args&.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\n\z/) + args = match[:args] + "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/" + else + args + end + end + def execute(*arg, grep: nil) o = Output.new(grep: grep) diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index d233cbb9b5..eafa8be382 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -480,6 +480,44 @@ module TestIRB assert_match(/C.methods:\s+m5\n/m, out) end + def test_ls_grep + pend if RUBY_ENGINE == 'truffleruby' + out, err = execute_lines("ls 42\n") + assert_empty err + assert_match(/times/, out) + assert_match(/polar/, out) + + [ + "ls 42, grep: /times/\n", + "ls 42 -g times\n", + "ls 42 -G times\n", + ].each do |line| + out, err = execute_lines(line) + assert_empty err + assert_match(/times/, out) + assert_not_match(/polar/, out) + end + end + + def test_ls_grep_empty + pend if RUBY_ENGINE == 'truffleruby' + out, err = execute_lines("ls\n") + assert_empty err + assert_match(/whereami/, out) + assert_match(/show_source/, out) + + [ + "ls grep: /whereami/\n", + "ls -g whereami\n", + "ls -G whereami\n", + ].each do |line| + out, err = execute_lines(line) + assert_empty err + assert_match(/whereami/, out) + assert_not_match(/show_source/, out) + end + end + def test_ls_with_no_singleton_class out, err = execute_lines( "ls 42", |