[ruby/open3] [DOC] RDoc for Open3

https://github.com/ruby/open3/commit/8c06964d12
This commit is contained in:
BurdetteLamar 2023-09-28 17:26:05 -05:00 коммит произвёл git
Родитель a2b6427514
Коммит 536f8d901c
1 изменённых файлов: 81 добавлений и 30 удалений

Просмотреть файл

@ -482,44 +482,95 @@ module Open3
private :popen_run
end
# Open3.capture3 captures the standard output and the standard error of a command.
# :call-seq:
# Open3.capture3([env, ] command_line, options = {}) -> [stdout_s, stderr_s, status]
# Open3.capture3([env, ] exe_path, *args, options = {}) -> [stdout_s, stderr_s, status]
#
# stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts])
# Basically a wrapper for Open3.popen3 that:
#
# The arguments env, cmd and opts are passed to Open3.popen3 except
# <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>. See Process.spawn.
# - Creates a child process, by calling Open3.popen3 with the given arguments
# (except for certain entries in hash +options+; see below).
# - Returns as strings +stdout+ and +stderr+ the standard output
# and standard error of the child process.
# - Returns as +status+ a <tt>Process::Status</tt> object
# that represents the exit status of the child process.
#
# If <code>opts[:stdin_data]</code> is specified, it is sent to the command's standard input.
# With no block given, returns the array <tt>[stdin_s, stdout_a, status]</tt>:
#
# If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.
# stdout_s, stderr_s, status = Open3.capture3('echo "Foo"')
# # => ["Foo\n", "", #<Process::Status: pid 2281954 exit 0>]
#
# Examples:
# Like Process.spawn, this method has potential security vulnerabilities
# if called with untrusted input;
# see {Command Injection}[rdoc-ref:command_injection.rdoc].
#
# # dot is a command of graphviz.
# graph = <<'End'
# digraph g {
# a -> b
# }
# End
# drawn_graph, dot_log = Open3.capture3("dot -v", :stdin_data=>graph)
# Unlike Process.spawn, this method waits for the child process to exit
# before returning, so the caller need not do so.
#
# o, e, s = Open3.capture3("echo abc; sort >&2", :stdin_data=>"foo\nbar\nbaz\n")
# p o #=> "abc\n"
# p e #=> "bar\nbaz\nfoo\n"
# p s #=> #<Process::Status: pid 32682 exit 0>
# Argument +options+ is a hash of options for the new process;
# see {Execution Options}[rdoc-ref:Process@Execution+Options].
#
# # generate a thumbnail image using the convert command of ImageMagick.
# # However, if the image is really stored in a file,
# # system("convert", "-thumbnail", "80", "png:#{filename}", "png:-") is better
# # because of reduced memory consumption.
# # But if the image is stored in a DB or generated by the gnuplot Open3.capture2 example,
# # Open3.capture3 should be considered.
# #
# image = File.read("/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png", :binmode=>true)
# thumbnail, err, s = Open3.capture3("convert -thumbnail 80 png:- png:-", :stdin_data=>image, :binmode=>true)
# if s.success?
# STDOUT.binmode; print thumbnail
# end
# The hash +options+ is passed to method Open3.popen3;
# two options have local effect in method Open3.capture3:
#
# - If entry <tt>options[:stdin_data]</tt> exists,
# and its string value is sent to the command's standard input:
#
# Open3.capture3('tee', {:stdin_data => 'Foo'})
# # => ["Foo", "", #<Process::Status: pid 2283204 exit 0>]
#
# - If <tt>options[:binmode]</tt> exists,
# the internal streams are set to binary mode.
#
# The single required argument is one of the following:
#
# - +command_line+ if it is a string,
# and if it begins with a shell reserved word or special built-in,
# or if it contains one or more metacharacters.
# - +exe_path+ otherwise.
#
# <b>Argument +command_line+</b>
#
# \String argument +command_line+ is a command line to be passed to a shell;
# it must begin with a shell reserved word, begin with a special built-in,
# or contain meta characters:
#
# Open3.capture3('if true; then echo "Foo"; fi') # Shell reserved word.
# # => ["Foo\n", "", #<Process::Status: pid 2282025 exit 0>]
# Open3.capture3('echo') # Built-in.
# # => ["\n", "", #<Process::Status: pid 2282092 exit 0>]
# Open3.capture3('date > date.tmp') # Contains meta character.
# # => ["", "", #<Process::Status: pid 2282110 exit 0>]
#
# The command line may also contain arguments and options for the command:
#
# Open3.capture3('echo "Foo"')
# # => ["Foo\n", "", #<Process::Status: pid 2282092 exit 0>]
#
# <b>Argument +exe_path+</b>
#
# Argument +exe_path+ is one of the following:
#
# - The string path to an executable to be called.
# - A 2-element array containing the path to an executable
# and the string to be used as the name of the executing process.
#
# Example:
#
# Open3.capture3('/usr/bin/date')
# # => ["Thu Sep 28 05:03:51 PM CDT 2023\n", "", #<Process::Status: pid 2282300 exit 0>]
#
# Ruby invokes the executable directly, with no shell and no shell expansion:
#
# Open3.capture3('doesnt_exist') # Raises Errno::ENOENT
#
# If one or more +args+ is given, each is an argument or option
# to be passed to the executable:
#
# Open3.capture3('echo', 'C #')
# # => ["C #\n", "", #<Process::Status: pid 2282368 exit 0>]
# Open3.capture3('echo', 'hello', 'world')
# # => ["hello world\n", "", #<Process::Status: pid 2282372 exit 0>]
#
def capture3(*cmd)
if Hash === cmd.last