|
48 | 48 | # Options](rdoc-ref:Process@Execution+Options). |
49 | 49 | # |
50 | 50 | module Open3 |
| 51 | + type env = Hash[String, String] |
| 52 | + |
| 53 | + # <!-- |
| 54 | + # rdoc-file=lib/open3.rb |
| 55 | + # - Open3.capture2([env, ] command_line, options = {}) -> [stdout_s, status] |
| 56 | + # - Open3.capture2([env, ] exe_path, *args, options = {}) -> [stdout_s, status] |
| 57 | + # --> |
| 58 | + # Basically a wrapper for Open3.popen3 that: |
| 59 | + # |
| 60 | + # * Creates a child process, by calling Open3.popen3 with the given arguments |
| 61 | + # (except for certain entries in hash `options`; see below). |
| 62 | + # * Returns as string `stdout_s` the standard output of the child process. |
| 63 | + # * Returns as `status` a `Process::Status` object that represents the exit |
| 64 | + # status of the child process. |
| 65 | + # |
| 66 | + # Returns the array `[stdout_s, status]`: |
| 67 | + # |
| 68 | + # stdout_s, status = Open3.capture2('echo "Foo"') |
| 69 | + # # => ["Foo\n", #<Process::Status: pid 2326047 exit 0>] |
| 70 | + # |
| 71 | + # Like Process.spawn, this method has potential security vulnerabilities if |
| 72 | + # called with untrusted input; see [Command |
| 73 | + # Injection](rdoc-ref:command_injection.rdoc@Command+Injection). |
| 74 | + # |
| 75 | + # Unlike Process.spawn, this method waits for the child process to exit before |
| 76 | + # returning, so the caller need not do so. |
| 77 | + # |
| 78 | + # If the first argument is a hash, it becomes leading argument `env` in the call |
| 79 | + # to Open3.popen3; see [Execution |
| 80 | + # Environment](rdoc-ref:Process@Execution+Environment). |
| 81 | + # |
| 82 | + # If the last argument is a hash, it becomes trailing argument `options` in the |
| 83 | + # call to Open3.popen3; see [Execution |
| 84 | + # Options](rdoc-ref:Process@Execution+Options). |
| 85 | + # |
| 86 | + # The hash `options` is given; two options have local effect in method |
| 87 | + # Open3.capture2: |
| 88 | + # |
| 89 | + # * If entry `options[:stdin_data]` exists, the entry is removed and its |
| 90 | + # string value is sent to the command's standard input: |
| 91 | + # |
| 92 | + # Open3.capture2('tee', stdin_data: 'Foo') |
| 93 | + # |
| 94 | + # # => ["Foo", #<Process::Status: pid 2326087 exit 0>] |
| 95 | + # |
| 96 | + # * If entry `options[:binmode]` exists, the entry is removed and the internal |
| 97 | + # streams are set to binary mode. |
| 98 | + # |
| 99 | + # The single required argument is one of the following: |
| 100 | + # |
| 101 | + # * `command_line` if it is a string, and if it begins with a shell reserved |
| 102 | + # word or special built-in, or if it contains one or more metacharacters. |
| 103 | + # * `exe_path` otherwise. |
| 104 | + # |
| 105 | + # **Argument `command_line`** |
| 106 | + # |
| 107 | + # String argument `command_line` is a command line to be passed to a shell; it |
| 108 | + # must begin with a shell reserved word, begin with a special built-in, or |
| 109 | + # contain meta characters: |
| 110 | + # |
| 111 | + # Open3.capture2('if true; then echo "Foo"; fi') # Shell reserved word. |
| 112 | + # # => ["Foo\n", #<Process::Status: pid 2326131 exit 0>] |
| 113 | + # Open3.capture2('echo') # Built-in. |
| 114 | + # # => ["\n", #<Process::Status: pid 2326139 exit 0>] |
| 115 | + # Open3.capture2('date > date.tmp') # Contains meta character. |
| 116 | + # # => ["", #<Process::Status: pid 2326174 exit 0>] |
| 117 | + # |
| 118 | + # The command line may also contain arguments and options for the command: |
| 119 | + # |
| 120 | + # Open3.capture2('echo "Foo"') |
| 121 | + # # => ["Foo\n", #<Process::Status: pid 2326183 exit 0>] |
| 122 | + # |
| 123 | + # **Argument `exe_path`** |
| 124 | + # |
| 125 | + # Argument `exe_path` is one of the following: |
| 126 | + # |
| 127 | + # * The string path to an executable to be called. |
| 128 | + # * A 2-element array containing the path to an executable and the string to |
| 129 | + # be used as the name of the executing process. |
| 130 | + # |
| 131 | + # Example: |
| 132 | + # |
| 133 | + # Open3.capture2('/usr/bin/date') |
| 134 | + # # => ["Fri Sep 29 01:00:39 PM CDT 2023\n", #<Process::Status: pid 2326222 exit 0>] |
| 135 | + # |
| 136 | + # Ruby invokes the executable directly, with no shell and no shell expansion: |
| 137 | + # |
| 138 | + # Open3.capture2('doesnt_exist') # Raises Errno::ENOENT |
| 139 | + # |
| 140 | + # If one or more `args` is given, each is an argument or option to be passed to |
| 141 | + # the executable: |
| 142 | + # |
| 143 | + # Open3.capture2('echo', 'C #') |
| 144 | + # # => ["C #\n", #<Process::Status: pid 2326267 exit 0>] |
| 145 | + # Open3.capture2('echo', 'hello', 'world') |
| 146 | + # # => ["hello world\n", #<Process::Status: pid 2326299 exit 0>] |
| 147 | + # |
| 148 | + def self?.capture2: (*String, ?stdin_data: String, ?binmode: boolish) -> [String, Process::Status] |
| 149 | + | (env, *String, ?stdin_data: String, ?binmode: boolish) -> [String, Process::Status] |
| 150 | + |
51 | 151 | # <!-- |
52 | 152 | # rdoc-file=lib/open3.rb |
53 | 153 | # - Open3.capture2e([env, ] command_line, options = {}) -> [stdout_and_stderr_s, status] |
@@ -143,5 +243,6 @@ module Open3 |
143 | 243 | # Open3.capture2e('echo', 'hello', 'world') |
144 | 244 | # # => ["hello world\n", #<Process::Status: pid 2371894 exit 0>] |
145 | 245 | # |
146 | | - def self.capture2e: (*String, ?stdin_data: String, ?binmode: boolish) -> [String, Process::Status] |
| 246 | + def self?.capture2e: (*String, ?stdin_data: String, ?binmode: boolish) -> [String, Process::Status] |
| 247 | + | (env, *String, ?stdin_data: String, ?binmode: boolish) -> [String, Process::Status] |
147 | 248 | end |
0 commit comments