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

ruby-changes:61738

From: Akinori <ko1@a...>
Date: Tue, 16 Jun 2020 23:26:31 +0900 (JST)
Subject: [ruby-changes:61738] 68e4310344 (master): Improve the document of Shellwords

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

From 68e43103442b95ebfa133e238550159fc3d1197d Mon Sep 17 00:00:00 2001
From: Akinori MUSHA <knu@i...>
Date: Tue, 16 Jun 2020 23:15:51 +0900
Subject: Improve the document of Shellwords

- Improve careless examples
  - Insert `--` before a file name for cat(1)
  - Insert `-e` before a search pattern for grep(1)

- Add a caution about passing an arbitrary argument to a command

diff --git a/lib/shellwords.rb b/lib/shellwords.rb
index 1eaddce..0bc34fa 100644
--- a/lib/shellwords.rb
+++ b/lib/shellwords.rb
@@ -24,28 +24,37 @@ https://github.com/ruby/ruby/blob/trunk/lib/shellwords.rb#L24
 #   argv = "see how they run".shellsplit
 #   argv #=> ["see", "how", "they", "run"]
 #
-# Be careful you don't leave a quote unmatched.
+# They treat quotes as special characters, so an unmatched quote will
+# cause an ArgumentError.
 #
 #   argv = "they all ran after the farmer's wife".shellsplit
 #        #=> ArgumentError: Unmatched double quote: ...
 #
-# In this case, you might want to use Shellwords.escape, or its alias
-# String#shellescape.
+# Shellwords also provides methods that do the opposite.
+# Shellwords.escape, or its alias, String#shellescape, escapes
+# shell metacharacters in a string for use in a command line.
 #
-# This method will escape the String for you to safely use with a Bourne shell.
+#   filename = "special's.txt"
 #
-#   argv = Shellwords.escape("special's.txt")
-#   argv #=> "special\\'s.txt"
-#   system("cat " + argv)
+#   system("cat -- #{filename.shellescape}")
+#   # runs "cat -- special\\'s.txt"
+#
+# Note the '--'.  Without it, cat(1) will treat the following argument
+# as a command line option if it starts with '-'.  It is guaranteed
+# that Shellwords.escape converts a string to a form that a Bourne
+# shell will parse back to the original string, but it is the
+# programmer's responsibility to make sure that passing an arbitrary
+# argument to a command does no harm.
 #
 # Shellwords also comes with a core extension for Array, Array#shelljoin.
 #
-#   argv = %w{ls -lta lib}
-#   system(argv.shelljoin)
+#   dir = "Funny GIFs"
+#   argv = %W[ls -lta -- #{dir}]
+#   system(argv.shelljoin + " | less")
+#   # runs "ls -lta -- Funny\\ GIFs | less"
 #
-# You can use this method to create an escaped string out of an array of tokens
-# separated by a space. In this example we used the literal shortcut for
-# Array.new.
+# You can use this method to build a complete command line out of an
+# array of arguments.
 #
 # === Authors
 # * Wakou Aoyama
@@ -123,7 +132,7 @@ module Shellwords https://github.com/ruby/ruby/blob/trunk/lib/shellwords.rb#L132
   #
   #   # Search files in lib for method definitions
   #   pattern = "^[ \t]*def "
-  #   open("| grep -Ern #{pattern.shellescape} lib") { |grep|
+  #   open("| grep -Ern -e #{pattern.shellescape} lib") { |grep|
   #     grep.each_line { |line|
   #       file, lineno, matched_line = line.split(':', 3)
   #       # ...
-- 
cgit v0.10.2


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

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