Skip to content

Commit bb021f5

Browse files
committed
Add option to bypass trim() for messages
1 parent add3739 commit bb021f5

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# CHANGELOG
22

3-
## 1.1.1
3+
## 1.2.0
4+
5+
* Add option to return untrimmed output and error
46

5-
WIP
67

78
## 1.1.0
89

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,6 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti
115115
* `getExitCode()`: The exit code.
116116
* `getExecuted()`: Whether the command was successfully executed.
117117
* `execute()`: Executes the command and returns `true` on success, `false` otherwhise.
118+
119+
> **Note:** `getError()`, `getStdErr()` and `getOutput()` return the trimmed output.
120+
> You can pass `false` to these methods if you need any possible line breaks at the end.

src/Command.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* This class represents a shell command.
88
*
99
* @author Michael Härtl <[email protected]>
10-
* @version 1.1.1-dev
10+
* @version 1.2.0-dev
1111
* @license http://www.opensource.org/licenses/MIT
1212
*/
1313
class Command
@@ -232,27 +232,30 @@ public function addArg($key, $value = null, $escape = null)
232232
}
233233

234234
/**
235+
* @param bool $trim whether to `trim()` the return value. The default is `true`.
235236
* @return string the command output (stdout). Empty if none.
236237
*/
237-
public function getOutput()
238+
public function getOutput($trim = true)
238239
{
239-
return $this->_stdOut;
240+
return $trim ? trim($this->_stdOut) : $this->_stdOut;
240241
}
241242

242243
/**
244+
* @param bool $trim whether to `trim()` the return value. The default is `true`.
243245
* @return string the error message, either stderr or internal message. Empty if none.
244246
*/
245-
public function getError()
247+
public function getError($trim = true)
246248
{
247-
return $this->_error;
249+
return $trim ? trim($this->_error) : $this->_error;
248250
}
249251

250252
/**
253+
* @param bool $trim whether to `trim()` the return value. The default is `true`.
251254
* @return string the stderr output. Empty if none.
252255
*/
253-
public function getStdErr()
256+
public function getStdErr($trim = true)
254257
{
255-
return $this->_stdErr;
258+
return $trim ? trim($this->_stdErr) : $this->_stdErr;
256259
}
257260

258261
/**
@@ -288,7 +291,7 @@ public function execute()
288291
if ($this->useExec) {
289292
$execCommand = $this->captureStdErr ? "$command 2>&1" : $command;
290293
exec($execCommand, $output, $this->_exitCode);
291-
$this->_stdOut = trim(implode("\n", $output));
294+
$this->_stdOut = implode("\n", $output);
292295
if ($this->_exitCode!==0) {
293296
$this->_stdErr = $this->_stdOut;
294297
$this->_error = empty($this->_stdErr) ? 'Command failed' : $this->_stdErr;
@@ -303,8 +306,8 @@ public function execute()
303306

304307
if (is_resource($process)) {
305308

306-
$this->_stdOut = trim(stream_get_contents($pipes[1]));
307-
$this->_stdErr = trim(stream_get_contents($pipes[2]));
309+
$this->_stdOut = stream_get_contents($pipes[1]);
310+
$this->_stdErr = stream_get_contents($pipes[2]);
308311
fclose($pipes[1]);
309312
fclose($pipes[2]);
310313

tests/CommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function testCanRunValidCommand()
125125
$this->assertTrue($command->execute());
126126
$this->assertTrue($command->getExecuted());
127127
$this->assertEquals("CommandTest.php", $command->getOutput());
128+
$this->assertEquals("CommandTest.php\n", $command->getOutput(false));
128129
$this->assertEmpty($command->getError());
129130
$this->assertEmpty($command->getStdErr());
130131
$this->assertEquals(0, $command->getExitCode());

0 commit comments

Comments
 (0)