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

ruby-changes:40991

From: knu <ko1@a...>
Date: Sun, 13 Dec 2015 14:19:25 +0900 (JST)
Subject: [ruby-changes:40991] knu:r53070 (trunk): * lib/shellwords.rb (Shellwords#shellsplit): Document that this

knu	2015-12-13 14:19:03 +0900 (Sun, 13 Dec 2015)

  New Revision: 53070

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53070

  Log:
    * lib/shellwords.rb (Shellwords#shellsplit): Document that this
      method does not treat shell metacharacters as such.

  Modified files:
    trunk/ChangeLog
    trunk/lib/shellwords.rb
    trunk/test/test_shellwords.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53069)
+++ ChangeLog	(revision 53070)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Dec 13 14:16:09 2015  Akinori MUSHA  <knu@i...>
+
+	* lib/shellwords.rb (Shellwords#shellsplit): Document that this
+	  method does not treat shell metacharacters as such.
+
 Sun Dec 13 12:17:43 2015  Eric Wong  <e@8...>
 
 	* lib/shellwords.rb (shellescape): duplicate frozen literal
Index: lib/shellwords.rb
===================================================================
--- lib/shellwords.rb	(revision 53069)
+++ lib/shellwords.rb	(revision 53070)
@@ -64,6 +64,13 @@ module Shellwords https://github.com/ruby/ruby/blob/trunk/lib/shellwords.rb#L64
   #   argv = Shellwords.split('here are "two words"')
   #   argv #=> ["here", "are", "two words"]
   #
+  # Note, however, that this is not a command line parser.  Shell
+  # metacharacters except for the single and double quotes and
+  # backslash are not treated as such.
+  #
+  #   argv = Shellwords.split('ruby my_prog.rb | less')
+  #   argv #=> ["ruby", "my_prog.rb", "|", "less"]
+  #
   # String#shellsplit is a shortcut for this function.
   #
   #   argv = 'here are "two words"'.shellsplit
Index: test/test_shellwords.rb
===================================================================
--- test/test_shellwords.rb	(revision 53069)
+++ test/test_shellwords.rb	(revision 53070)
@@ -6,15 +6,15 @@ class TestShellwords < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/test_shellwords.rb#L6
 
   include Shellwords
 
-  def setup
-    @not_string = Class.new
-    @cmd = "ruby my_prog.rb | less"
-  end
-
+  def test_shellwords
+    cmd1 = "ruby -i'.bak' -pe \"sub /foo/, '\\\\&bar'\" foobar\\ me.txt\n"
+    assert_equal(['ruby', '-i.bak', '-pe', "sub /foo/, '\\&bar'", "foobar me.txt"],
+                 shellwords(cmd1))
 
-  def test_string
-    assert_instance_of(Array, shellwords(@cmd))
-    assert_equal(4, shellwords(@cmd).length)
+    # shellwords does not interpret meta-characters
+    cmd2 = "ruby my_prog.rb | less"
+    assert_equal(['ruby', 'my_prog.rb', '|', 'less'],
+                 shellwords(cmd2))
   end
 
   def test_unmatched_double_quote

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

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