|
31 | 31 |
|
32 | 32 | require 'open3/version'
|
33 | 33 |
|
| 34 | +# \Module \Open3 supports creating child processes |
| 35 | +# with access to their $stdin, $stdout, and $stderr streams. |
| 36 | +# |
| 37 | +# == What's Here |
| 38 | +# |
| 39 | +# Each of these methods executes a given command in a new process or subshell, |
| 40 | +# or multiple commands in new processes and/or subshells: |
| 41 | +# |
| 42 | +# - Each of these methods executes a single command in a process or subshell, |
| 43 | +# accepts a string for input to $stdin, |
| 44 | +# and returns string output from $stdout, $stderr, or both: |
| 45 | +# |
| 46 | +# - Open3.capture2: Executes the command; |
| 47 | +# returns the string from $stdout. |
| 48 | +# - Open3.capture2e: Executes the command; |
| 49 | +# returns the string from merged $stdout and $stderr. |
| 50 | +# - Open3.capture3: Executes the command; |
| 51 | +# returns strings from $stdout and $stderr. |
| 52 | +# |
| 53 | +# - Each of these methods executes a single command in a process or subshell, |
| 54 | +# and returns pipes for $stdin, $stdout, and/or $stderr: |
| 55 | +# |
| 56 | +# - Open3.popen2: Executes the command; |
| 57 | +# returns pipes for $stdin and $stdout. |
| 58 | +# - Open3.popen2e: Executes the command; |
| 59 | +# returns pipes for $stdin and merged $stdout and $stderr. |
| 60 | +# - Open3.popen3: Executes the command; |
| 61 | +# returns pipes for $stdin, $stdout, and $stderr. |
| 62 | +# |
| 63 | +# - Each of these methods executes one or more commands in processes and/or subshells, |
| 64 | +# returns pipes for the first $stdin, the last $stdout, or both: |
| 65 | +# |
| 66 | +# - Open3.pipeline_r: Returns a pipe for the last $stdout. |
| 67 | +# - Open3.pipeline_rw: Returns pipes for the first $stdin and the last $stdout. |
| 68 | +# - Open3.pipeline_w: Returns a pipe for the first $stdin. |
| 69 | +# - Open3.pipeline_start: Does not wait for processes to complete. |
| 70 | +# - Open3.pipeline: Waits for processes to complete. |
| 71 | +# |
| 72 | +# Each of the methods above accepts: |
| 73 | +# |
| 74 | +# - An optional hash of environment variable names and values; |
| 75 | +# see {Execution Environment}[https://docs.ruby-lang.org/en/master/Process.html#Execution+Environment]. |
| 76 | +# - A required string argument that is a +command_line+ or +exe_path+; |
| 77 | +# see {Argument command_line or exe_path}[https://docs.ruby-lang.org/en/master/Process.html#Argument+command_line+or+exe_path]. |
| 78 | +# - An optional hash of execution options; |
| 79 | +# see {Execution Options}[https://docs.ruby-lang.org/en/master/Process.html#Execution+Options]. |
| 80 | +# |
34 | 81 | module Open3
|
35 | 82 |
|
36 | 83 | # :call-seq:
|
@@ -1061,57 +1108,54 @@ def pipeline_start(*cmds, &block)
|
1061 | 1108 | end
|
1062 | 1109 | module_function :pipeline_start
|
1063 | 1110 |
|
1064 |
| - # Open3.pipeline starts a list of commands as a pipeline. |
1065 |
| - # It waits for the completion of the commands. |
1066 |
| - # No pipes are created for stdin of the first command and |
1067 |
| - # stdout of the last command. |
| 1111 | + # :call-seq: |
| 1112 | + # Open3.pipeline([env, ] *cmds, options = {}) -> array_of_statuses |
1068 | 1113 | #
|
1069 |
| - # status_list = Open3.pipeline(cmd1, cmd2, ... [, opts]) |
| 1114 | + # Basically a wrapper for |
| 1115 | + # {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn] |
| 1116 | + # that: |
1070 | 1117 | #
|
1071 |
| - # Each cmd is a string or an array. |
1072 |
| - # If it is an array, the elements are passed to Process.spawn. |
| 1118 | + # - Creates a child process for each of the given +cmds+ |
| 1119 | + # by calling Process.spawn. |
| 1120 | + # - Pipes the +stdout+ from each child to the +stdin+ of the next child, |
| 1121 | + # or, for the last child, to the caller's +stdout+. |
| 1122 | + # - Waits for all child processes to exit. |
| 1123 | + # - Returns an array of Process::Status objects (one for each child). |
1073 | 1124 | #
|
1074 |
| - # cmd: |
1075 |
| - # commandline command line string which is passed to a shell |
1076 |
| - # [env, commandline, opts] command line string which is passed to a shell |
1077 |
| - # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) |
1078 |
| - # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) |
| 1125 | + # A simple example: |
1079 | 1126 | #
|
1080 |
| - # Note that env and opts are optional, as Process.spawn. |
| 1127 | + # Open3.pipeline('ls', 'grep [A-Z]') |
| 1128 | + # # => [#<Process::Status: pid 1343895 exit 0>, #<Process::Status: pid 1343897 exit 0>] |
1081 | 1129 | #
|
1082 |
| - # Example: |
| 1130 | + # Output: |
1083 | 1131 | #
|
1084 |
| - # fname = "/usr/share/man/man1/ruby.1.gz" |
1085 |
| - # p Open3.pipeline(["zcat", fname], "nroff -man", "less") |
1086 |
| - # #=> [#<Process::Status: pid 11817 exit 0>, |
1087 |
| - # # #<Process::Status: pid 11820 exit 0>, |
1088 |
| - # # #<Process::Status: pid 11828 exit 0>] |
| 1132 | + # Gemfile |
| 1133 | + # LICENSE.txt |
| 1134 | + # Rakefile |
| 1135 | + # README.md |
1089 | 1136 | #
|
1090 |
| - # fname = "/usr/share/man/man1/ls.1.gz" |
1091 |
| - # Open3.pipeline(["zcat", fname], "nroff -man", "colcrt") |
| 1137 | + # Like Process.spawn, this method has potential security vulnerabilities |
| 1138 | + # if called with untrusted input; |
| 1139 | + # see {Command Injection}[rdoc-ref:command_injection.rdoc]. |
1092 | 1140 | #
|
1093 |
| - # # convert PDF to PS and send to a printer by lpr |
1094 |
| - # pdf_file = "paper.pdf" |
1095 |
| - # printer = "printer-name" |
1096 |
| - # Open3.pipeline(["pdftops", pdf_file, "-"], |
1097 |
| - # ["lpr", "-P#{printer}"]) |
1098 |
| - # |
1099 |
| - # # count lines |
1100 |
| - # Open3.pipeline("sort", "uniq -c", :in=>"names.txt", :out=>"count") |
1101 |
| - # |
1102 |
| - # # cyclic pipeline |
1103 |
| - # r,w = IO.pipe |
1104 |
| - # w.print "ibase=14\n10\n" |
1105 |
| - # Open3.pipeline("bc", "tee /dev/tty", :in=>r, :out=>w) |
1106 |
| - # #=> 14 |
1107 |
| - # # 18 |
1108 |
| - # # 22 |
1109 |
| - # # 30 |
1110 |
| - # # 42 |
1111 |
| - # # 58 |
1112 |
| - # # 78 |
1113 |
| - # # 106 |
1114 |
| - # # 202 |
| 1141 | + # Unlike Process.spawn, this method waits for the child process to exit |
| 1142 | + # before returning, so the caller need not do so. |
| 1143 | + # |
| 1144 | + # If the first argument is a hash, it becomes leading argument +env+ |
| 1145 | + # in each call to Process.spawn; |
| 1146 | + # see {Execution Environment}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Environment]. |
| 1147 | + # |
| 1148 | + # If the last argument is a hash, it becomes trailing argument +options+ |
| 1149 | + # in each call to Process.spawn' |
| 1150 | + # see {Execution Options}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Options]. |
| 1151 | + # |
| 1152 | + # Each remaining argument in +cmds+ is one of: |
| 1153 | + # |
| 1154 | + # - A +command_line+: a string that begins with a shell reserved word |
| 1155 | + # or special built-in, or contains one or more metacharacters. |
| 1156 | + # - An +exe_path+: the string path to an executable to be called. |
| 1157 | + # - An array containing a +command_line+ or an +exe_path+, |
| 1158 | + # along with zero or more string arguments for the command. |
1115 | 1159 | #
|
1116 | 1160 | def pipeline(*cmds)
|
1117 | 1161 | if Hash === cmds.last
|
|
0 commit comments