From 56e1acf44a9c2a642aa66c09308682c6dd2d40d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Wed, 19 Apr 2023 09:05:31 +0200 Subject: [PATCH] Issue #61 Add form feed to trimmed characters --- README.md | 20 ++++++++++---------- src/Command.php | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3752c1d..eaa4039 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ php-shellcommand -=========== +================ [![GitHub Tests](https://github.com/mikehaertl/php-shellcommand/workflows/Tests/badge.svg)](https://github.com/mikehaertl/php-shellcommand/actions) [![Packagist Version](https://img.shields.io/packagist/v/mikehaertl/php-shellcommand?label=version)](https://packagist.org/packages/mikehaertl/php-shellcommand) @@ -61,7 +61,7 @@ $command->addArg('--name=', "d'Artagnan"); // Add argument with several values // results in --keys key1 key2 -$command->addArg('--keys', array('key1','key2')); +$command->addArg('--keys', ['key1','key2']); ``` ### Pipe Input Into Command @@ -69,7 +69,7 @@ $command->addArg('--keys', array('key1','key2')); From string: ```php setStdIn('{"foo": 0}'); if (!$command->execute()) { echo $command->getError(); @@ -115,19 +115,19 @@ fclose($fh); ```php '/usr/local/bin/mycommand', // Will be passed as environment variables to the command - 'procEnv' => array( + 'procEnv' => [ 'DEMOVAR' => 'demovalue' - ), + ], // Will be passed as options to proc_open() - 'procOptions' => array( + 'procOptions' => [ 'bypass_shell' => true, - ), -)); + ], +]); ``` ## API @@ -185,7 +185,7 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti and `=`, the (optional) `$value` will be separated by a space. The key will get escaped if `$escapeArgs` is `true`. * `$value`: The optional argument value which will get escaped if `$escapeArgs` is `true`. - An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))` + An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', ['val1','val2'])` which will create the option "--exclude 'val1' 'val2'". * `$escape`: If set, this overrides the `$escapeArgs` setting and enforces escaping/no escaping * `setStdIn()`: String or resource to supply to command via standard input. diff --git a/src/Command.php b/src/Command.php index 5846f13..963ced9 100644 --- a/src/Command.php +++ b/src/Command.php @@ -328,30 +328,36 @@ public function addArg($key, $value = null, $escape = null) /** * @param bool $trim whether to `trim()` the return value. The default is `true`. + * @param string $characters the list of characters to trim. The default + * is ` \t\n\r\0\v\f`. * @return string the command output (stdout). Empty if none. */ - public function getOutput($trim = true) + public function getOutput($trim = true, $characters = " \t\n\r\0\v\f") { - return $trim ? trim($this->_stdOut) : $this->_stdOut; + return $trim ? trim($this->_stdOut, $characters) : $this->_stdOut; } /** * @param bool $trim whether to `trim()` the return value. The default is `true`. + * @param string $characters the list of characters to trim. The default + * is ` \t\n\r\0\v\f`. * @return string the error message, either stderr or an internal message. * Empty string if none. */ - public function getError($trim = true) + public function getError($trim = true, $characters = " \t\n\r\0\v\f") { - return $trim ? trim($this->_error) : $this->_error; + return $trim ? trim($this->_error, $characters) : $this->_error; } /** * @param bool $trim whether to `trim()` the return value. The default is `true`. + * @param string $characters the list of characters to trim. The default + * is ` \t\n\r\0\v\f`. * @return string the stderr output. Empty if none. */ - public function getStdErr($trim = true) + public function getStdErr($trim = true, $characters = " \t\n\r\0\v\f") { - return $trim ? trim($this->_stdErr) : $this->_stdErr; + return $trim ? trim($this->_stdErr, $characters) : $this->_stdErr; } /**