[前][次][番号順一覧][スレッド一覧]

ruby-changes:74444

From: Takashi <ko1@a...>
Date: Fri, 11 Nov 2022 07:55:36 +0900 (JST)
Subject: [ruby-changes:74444] 8fa83fa0b2 (master): [ruby/irb] Transform ls's --grep/-G option to keyword args

https://git.ruby-lang.org/ruby.git/commit/?id=8fa83fa0b2

From 8fa83fa0b2031ad17f01b5a12b39599398dc6da6 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Thu, 10 Nov 2022 14:55:11 -0800
Subject: [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
---
 lib/irb.rb           |  1 +
 lib/irb/cmd/ls.rb    |  9 +++++++++
 test/irb/test_cmd.rb | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)

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 https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L427
       @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 https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/ls.rb#L9
 
   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 https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L480
       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",
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]