Skip to content

Commit 0aadba9

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] RDoc for Open3
1 parent f319192 commit 0aadba9

File tree

1 file changed

+86
-12
lines changed

1 file changed

+86
-12
lines changed

lib/open3.rb

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,12 @@ class << self
490490
#
491491
# - Creates a child process, by calling Open3.popen3 with the given arguments
492492
# (except for certain entries in hash +options+; see below).
493-
# - Returns as strings +stdout+ and +stderr+ the standard output
493+
# - Returns as strings +stdout_s+ and +stderr_s+ the standard output
494494
# and standard error of the child process.
495495
# - Returns as +status+ a <tt>Process::Status</tt> object
496496
# that represents the exit status of the child process.
497497
#
498-
# Returns the array <tt>[stdin_s, stdout_s, status]</tt>:
498+
# Returns the array <tt>[stdout_s, stderr_s, status]</tt>:
499499
#
500500
# stdout_s, stderr_s, status = Open3.capture3('echo "Foo"')
501501
# # => ["Foo\n", "", #<Process::Status: pid 2281954 exit 0>]
@@ -612,11 +612,11 @@ def capture3(*cmd)
612612
#
613613
# - Creates a child process, by calling Open3.popen3 with the given arguments
614614
# (except for certain entries in hash +options+; see below).
615-
# - Returns as string +stdout+ the standard output of the child process.
615+
# - Returns as string +stdout_s+ the standard output of the child process.
616616
# - Returns as +status+ a <tt>Process::Status</tt> object
617617
# that represents the exit status of the child process.
618618
#
619-
# Returns the array <tt>[stdin_s, status]</tt>:
619+
# Returns the array <tt>[stdout_s, status]</tt>:
620620
#
621621
# stdout_s, status = Open3.capture2('echo "Foo"')
622622
# # => ["Foo\n", #<Process::Status: pid 2326047 exit 0>]
@@ -638,6 +638,7 @@ def capture3(*cmd)
638638
# and its string value is sent to the command's standard input:
639639
#
640640
# Open3.capture2('tee', stdin_data: 'Foo')
641+
#
641642
# # => ["Foo", #<Process::Status: pid 2326087 exit 0>]
642643
#
643644
# the internal streams are set to binary mode.
@@ -724,21 +725,94 @@ def capture2(*cmd)
724725
end
725726
module_function :capture2
726727

727-
# Open3.capture2e captures the standard output and the standard error of a command.
728+
# :call-seq:
729+
# Open3.capture2e([env, ] command_line, options = {}) -> [stdout_and_stderr_s, status]
730+
# Open3.capture2e([env, ] exe_path, *args, options = {}) -> [stdout_and_stderr_s, status]
731+
#
732+
# Basically a wrapper for Open3.popen3 that:
733+
#
734+
# - Creates a child process, by calling Open3.popen3 with the given arguments
735+
# (except for certain entries in hash +options+; see below).
736+
# - Returns as string +stdout_and_stderr_s+ the merged standard output
737+
# and standard error of the child process.
738+
# - Returns as +status+ a <tt>Process::Status</tt> object
739+
# that represents the exit status of the child process.
740+
#
741+
# Returns the array <tt>[stdout_and_stderr_s, status]</tt>:
742+
#
743+
# stdout_and_stderr_s, status = Open3.capture2e('echo "Foo"')
744+
# # => ["Foo\n", #<Process::Status: pid 2371692 exit 0>]
745+
#
746+
# Like Process.spawn, this method has potential security vulnerabilities
747+
# if called with untrusted input;
748+
# see {Command Injection}[rdoc-ref:command_injection.rdoc].
728749
#
729-
# stdout_and_stderr_str, status = Open3.capture2e([env,] cmd... [, opts])
750+
# Unlike Process.spawn, this method waits for the child process to exit
751+
# before returning, so the caller need not do so.
730752
#
731-
# The arguments env, cmd and opts are passed to Open3.popen3 except
732-
# <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>. See Process.spawn.
753+
# Argument +options+ is a hash of options for the new process;
754+
# see {Execution Options}[rdoc-ref:Process@Execution+Options].
733755
#
734-
# If <code>opts[:stdin_data]</code> is specified, it is sent to the command's standard input.
756+
# The hash +options+ is passed to method Open3.popen3;
757+
# two options have local effect in method Open3.capture2e:
735758
#
736-
# If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.
759+
# - If entry <tt>options[:stdin_data]</tt> exists, the entry is removed
760+
# and its string value is sent to the command's standard input:
761+
#
762+
# Open3.capture2e('tee', stdin_data: 'Foo')
763+
# # => ["Foo", #<Process::Status: pid 2371732 exit 0>]
764+
#
765+
# the internal streams are set to binary mode.
766+
#
767+
# The single required argument is one of the following:
768+
#
769+
# - +command_line+ if it is a string,
770+
# and if it begins with a shell reserved word or special built-in,
771+
# or if it contains one or more metacharacters.
772+
# - +exe_path+ otherwise.
773+
#
774+
# <b>Argument +command_line+</b>
775+
#
776+
# \String argument +command_line+ is a command line to be passed to a shell;
777+
# it must begin with a shell reserved word, begin with a special built-in,
778+
# or contain meta characters:
779+
#
780+
# Open3.capture2e('if true; then echo "Foo"; fi') # Shell reserved word.
781+
# # => ["Foo\n", #<Process::Status: pid 2371740 exit 0>]
782+
# Open3.capture2e('echo') # Built-in.
783+
# # => ["\n", #<Process::Status: pid 2371774 exit 0>]
784+
# Open3.capture2e('date > date.tmp') # Contains meta character.
785+
# # => ["", #<Process::Status: pid 2371812 exit 0>]
786+
#
787+
# The command line may also contain arguments and options for the command:
788+
#
789+
# Open3.capture2e('echo "Foo"')
790+
# # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>]
791+
#
792+
# <b>Argument +exe_path+</b>
793+
#
794+
# Argument +exe_path+ is one of the following:
795+
#
796+
# - The string path to an executable to be called.
797+
# - A 2-element array containing the path to an executable
798+
# and the string to be used as the name of the executing process.
737799
#
738800
# Example:
739801
#
740-
# # capture make log
741-
# make_log, s = Open3.capture2e("make")
802+
# Open3.capture2e('/usr/bin/date')
803+
# # => ["Sat Sep 30 09:01:46 AM CDT 2023\n", #<Process::Status: pid 2371820 exit 0>]
804+
#
805+
# Ruby invokes the executable directly, with no shell and no shell expansion:
806+
#
807+
# Open3.capture2e('doesnt_exist') # Raises Errno::ENOENT
808+
#
809+
# If one or more +args+ is given, each is an argument or option
810+
# to be passed to the executable:
811+
#
812+
# Open3.capture2e('echo', 'C #')
813+
# # => ["C #\n", #<Process::Status: pid 2371856 exit 0>]
814+
# Open3.capture2e('echo', 'hello', 'world')
815+
# # => ["hello world\n", #<Process::Status: pid 2371894 exit 0>]
742816
#
743817
def capture2e(*cmd)
744818
if Hash === cmd.last

0 commit comments

Comments
 (0)