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

ruby-changes:73584

From: Jeremy <ko1@a...>
Date: Sat, 17 Sep 2022 02:25:46 +0900 (JST)
Subject: [ruby-changes:73584] b07db96744 (master): [ruby/irb] Support --noscript option to not use first non-option argument as script

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

From b07db967441161a84386bcbbb41d990a2f3ad31c Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Sat, 17 Sep 2022 02:25:15 +0900
Subject: [ruby/irb] Support --noscript option to not use first non-option
 argument as script

Also add --script option to turn the option back on.

Previously there wasn't a way to get an interactive IRB session
and access arguments provided on the command line.

Additionally, handle `-` as script as stdin. In Unix-like tools, `-`
means to take standard input instead of a file.  This doesn't
result in exactly the same output for:

```
echo 'p ARGV' > args.rb; irb args.rb a b c
```

and

```
echo 'p ARGV' | irb - a b c
```

Due to how irb handles whether stdin is a tty.

However, this change allows use of `-` as a argument, instead of
giving an unrecognized switch error. This required some small
changes to context.rb (to handle `-` as standard input) and
input-method.rb (to have FileInputMethod accept IO arguments in
addition to strings).

Implements [Feature #15371]

https://github.com/ruby/irb/commit/4192683ba2
---
 lib/irb/context.rb      |  4 ++++
 lib/irb/init.rb         | 16 ++++++++++++----
 lib/irb/input-method.rb |  2 +-
 lib/irb/lc/help-message |  2 ++
 test/irb/test_init.rb   | 45 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index e6c993d423..b74cae1223 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -115,6 +115,10 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L115
         end
         @io = StdioInputMethod.new unless @io
 
+      when '-'
+        @io = FileInputMethod.new($stdin)
+        @irb_name = '-'
+        @irb_path = '-'
       when String
         @io = FileInputMethod.new(input_method)
         @irb_name = File.basename(input_method)
diff --git a/lib/irb/init.rb b/lib/irb/init.rb
index d2baee2017..d9c4353f39 100644
--- a/lib/irb/init.rb
+++ b/lib/irb/init.rb
@@ -289,6 +289,10 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L289
         @CONF[:PROMPT_MODE] = prompt_mode
       when "--noprompt"
         @CONF[:PROMPT_MODE] = :NULL
+      when "--script"
+        noscript = false
+      when "--noscript"
+        noscript = true
       when "--inf-ruby-mode"
         @CONF[:PROMPT_MODE] = :INF_RUBY
       when "--sample-book-mode", "--simple-prompt"
@@ -309,16 +313,20 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L313
         IRB.print_usage
         exit 0
       when "--"
-        if opt = argv.shift
+        if !noscript && (opt = argv.shift)
           @CONF[:SCRIPT] = opt
           $0 = opt
         end
         break
-      when /^-/
+      when /^-./
         fail UnrecognizedSwitch, opt
       else
-        @CONF[:SCRIPT] = opt
-        $0 = opt
+        if noscript
+          argv.unshift(opt)
+        else
+          @CONF[:SCRIPT] = opt
+          $0 = opt
+        end
         break
       end
     end
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index b77fd3207d..eec2daa549 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -137,7 +137,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L137
     # Creates a new input method object
     def initialize(file)
       super
-      @io = IRB::MagicFile.open(file)
+      @io = file.is_a?(IO) ? file : IRB::MagicFile.open(file)
       @external_encoding = @io.external_encoding
     end
     # The file name of this input method, usually given during initialization.
diff --git a/lib/irb/lc/help-message b/lib/irb/lc/help-message
index 3405fa775f..5b23f4c41e 100644
--- a/lib/irb/lc/help-message
+++ b/lib/irb/lc/help-message
@@ -38,6 +38,8 @@ Usage:  irb.rb [options] [programfile] [arguments] https://github.com/ruby/ruby/blob/trunk/lib/irb/lc/help-message#L38
   --sample-book-mode, --simple-prompt
                     Set prompt mode to 'simple'.
   --noprompt        Don't output prompt.
+  --script          Script mode (default, treat first argument as script)
+  --noscript        No script mode (leave arguments in argv)
   --single-irb      Share self with sub-irb.
   --tracer          Show stack trace for each command.
   --back-trace-limit n[=16]
diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb
index d8c7c79263..3293b98d34 100644
--- a/test/irb/test_init.rb
+++ b/test/irb/test_init.rb
@@ -16,6 +16,7 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_init.rb#L16
     def teardown
       ENV.update(@backup_env)
       FileUtils.rm_rf(@tmpdir)
+      IRB.conf.delete(:SCRIPT)
     end
 
     def test_setup_with_argv_preserves_global_argv
@@ -87,6 +88,50 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_init.rb#L88
       IRB.conf[:USE_COLORIZE] = orig_use_colorize
     end
 
+    def test_noscript
+      argv = %w[--noscript -- -f]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_nil IRB.conf[:SCRIPT]
+      assert_equal(['-f'], argv)
+
+      argv = %w[--noscript -- a]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_nil IRB.conf[:SCRIPT]
+      assert_equal(['a'], argv)
+
+      argv = %w[--noscript a]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_nil IRB.conf[:SCRIPT]
+      assert_equal(['a'], argv)
+
+      argv = %w[--script --noscript a]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_nil IRB.conf[:SCRIPT]
+      assert_equal(['a'], argv)
+
+      argv = %w[--noscript --script a]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_equal('a', IRB.conf[:SCRIPT])
+      assert_equal([], argv)
+    end
+
+    def test_dash
+      argv = %w[-]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_equal('-', IRB.conf[:SCRIPT])
+      assert_equal([], argv)
+
+      argv = %w[-- -]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_equal('-', IRB.conf[:SCRIPT])
+      assert_equal([], argv)
+
+      argv = %w[-- - -f]
+      IRB.setup(eval("__FILE__"), argv: argv)
+      assert_equal('-', IRB.conf[:SCRIPT])
+      assert_equal(['-f'], argv)
+    end
+
     private
 
     def with_argv(argv)
-- 
cgit v1.2.1


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

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